目录

CentOS 常见操作命令介绍

1. 系统管理

1.1 查看系统信息

- 查看系统版本

cat /etc/centos-release

<code>lsb_release -a</code>  

- 查看内核版本

uname -r

- 查看系统运行时间

uptime

1.2 系统更新

- 更新系统软件包

yum update

- 更新特定软件包

yum update [package_name]

- 安装新的软件包

yum install [package_name]

- 移除软件包

yum remove [package_name]

1.3 系统关机与重启

- 关机

shutdown -h now

<code>poweroff</code>  

- 重启

shutdown -r now

<code>reboot</code>  

2. 用户管理

2.1 用户操作

- 添加用户

useradd [username]

- 设置用户密码

passwd [username]

- 删除用户

userdel [username]

2.2 用户组操作

- 添加用户组

groupadd [groupname]

- 删除用户组

groupdel [groupname]

- 将用户加入组

usermod -aG [groupname] [username]

3. 文件系统操作

3.1 文件管理

- 查看文件内容

cat [file_name]

<code>less [file_name]</code>  

- 复制文件

cp [source_file] [destination_file]

- 移动文件

mv [source_file] [destination_file]

- 删除文件

rm [file_name]

3.2 目录管理

- 创建目录

mkdir [directory_name]

- 删除目录

rmdir [directory_name]

<code>rm -r [directory_name]</code> (强制删除非空目录)  

- 查看目录内容

ls

<code>ls -l</code> (详细信息)  

3.3 文件权限

- 查看文件权限

ls -l [file_name]

- 修改文件权限

chmod [permissions] [file_name]

示例:

<code>chmod 755 [file_name]</code>  

- 修改文件所有者

chown [owner] [file_name]

- 修改文件所属组

chgrp [group] [file_name]

4. 网络操作

4.1 网络配置

- 查看网络接口信息

ifconfig

<code>ip a</code>  

- 查看路由表

route -n

<code>ip route</code>  

- 配置网络接口 编辑配置文件:

<code>nano /etc/sysconfig/network-scripts/ifcfg-eth0</code>  
示例内容:  
<code>
DEVICE=eth0  
BOOTPROTO=static  
ONBOOT=yes  
IPADDR=192.168.1.100  
NETMASK=255.255.255.0  
GATEWAY=192.168.1.1
</code>  

- 重启网络服务

service network restart

<code>systemctl restart network</code>  

4.2 网络测试

- 测试网络连接

ping [IP_address]

- 查看端口监听情况

netstat -tuln

<code>ss -tuln</code>  

- 查看防火墙状态

firewall-cmd --state

- 允许防火墙端口

firewall-cmd --permanent --add-port=[port_number]/[protocol]

示例:

<code>firewall-cmd --permanent --add-port=80/tcp</code>  

- 重新加载防火墙规则

firewall-cmd --reload

5. 服务管理

5.1 服务操作

- 查看服务状态

systemctl status [service_name]

- 启动服务

systemctl start [service_name]

- 停止服务

systemctl stop [service_name]

- 重启服务

systemctl restart [service_name]

- 设置服务开机自启

systemctl enable [service_name]

- 禁用服务开机自启

systemctl disable [service_name]

6. 日志管理

6.1 查看日志

- 查看系统日志

cat /var/log/messages

- 查看特定服务日志 示例:查看Apache日志

<code>cat /var/log/httpd/error_log</code>  

- 实时查看日志

tail -f [log_file]

示例:

<code>tail -f /var/log/messages</code>  

7. 其他常用命令

7.1 磁盘管理

- 查看磁盘使用情况

df -h

- 查看文件系统挂载信息

mount

- 挂载文件系统

mount [device] [mount_point]

示例:

<code>mount /dev/sdb1 /mnt</code>  

7.2 压缩与解压

- 压缩文件

tar -czvf [archive_name].tar.gz [file_or_directory]

- 解压文件

tar -xzvf [archive_name].tar.gz

- 压缩为zip格式

zip [archive_name].zip [file_or_directory]

- 解压zip文件

unzip [archive_name].zip

7.3 查找文件

- 查找文件

find [path] -name [file_name]

示例:

<code>find / -name example.txt</code>  

- 搜索文件内容

grep [pattern] [file_name]

示例:

<code>grep "error" /var/log/messages</code>  

希望这份说明书能帮助你更好地使用 CentOS 系统!如果需要进一步的帮助,请随时联系管理员。