前言
本文整理了 CentOS 系统中最常用的命令,涵盖文件管理、用户管理、进程管理、网络管理、系统管理等各个方面,适合运维工程师和系统管理员日常查阅。
一、文件与目录管理
1.1 基础操作
pwd
cd /home
cd ..
cd ~
cd -
ls
ls -la
ls -lh
ls -lt
ls /path/to/dir
cat file.txt
more file.txt
less file.txt
head -n 10 file.txt
tail -n 10 file.txt
tail -f /var/log/messages
1.2 文件操作
touch file.txt
echo "content" > file.txt
echo "append content" >> file.txt
cp file.txt file_backup.txt
cp -r dir1/ dir2/
mv old_name.txt new_name.txt
mv file.txt /tmp/
rm file.txt
rm -rf directory/
rm -i file.txt
1.3 目录操作
mkdir new_dir
mkdir -p dir1/dir2/dir3
rmdir empty_dir
tree
tree -L 2
tree -d
1.4 文件查找
find /path -name "filename"
find /path -name "*.log"
find /path -iname "FILE*"
find /path -type f
find /path -type d
find /path -size +100M
find /path -size -10M
find /path -mtime -7
find /path -mtime +30
find /path -name "*.log" -delete
find /path -name "*.tmp" -exec rm {} \;
locate filename
updatedb
which python3
whereis python
1.5 文件内容搜索
grep "pattern" file.txt
grep -i "error" /var/log/messages
grep -r "keyword" /path/to/dir
grep -n "pattern" file.txt
grep -v "comment" file.txt
grep -E "error|warning" log.txt
grep "error" log.txt | wc -l
ps aux | grep nginx
二、文件权限与属性
2.1 权限管理
ls -la
chmod 755 file.sh
chmod 644 file.txt
chmod +x script.sh
chmod u+x file.sh
chmod g-w file.txt
chmod o=r file.txt
chmod -R 755 /var/www/html/
2.2 所有者管理
ls -l
chown user file.txt
chown user:group file.txt
chown -R www-data:www-data /var/www/
chgrp group file.txt
2.3 特殊权限
chmod u+s /usr/bin/passwd
chmod g+s /shared/directory
chmod +t /tmp
三、用户与组管理
3.1 用户管理
whoami
id
cat /etc/passwd
cut -d: -f1 /etc/passwd
useradd username
useradd -m -s /bin/bash username
useradd -g groupname username
passwd username
echo "password" | passwd --stdin username
usermod -aG group username
usermod -l newname oldname
usermod -s /bin/zsh username
userdel username
userdel -r username
who
w
last
3.2 组管理
cat /etc/group
getent group
groupadd groupname
groupdel groupname
groupmod -n newname oldname
gpasswd -a username groupname
usermod -aG groupname username
gpasswd -d username groupname
3.3 切换用户
su -
su root
su - username
sudo command
su - username -c "command"
四、进程管理
4.1 进程查看
ps aux
ps -ef
ps aux | grep nginx
top
htop
pstree
pstree -p
pstree -u
ps aux | grep mysql
pgrep mysql
pidof nginx
4.2 进程控制
kill 1234
kill -9 1234
killall nginx
pkill httpd
nohup command &
nohup ./script.sh > log.txt 2>&1 &
jobs
bg %1
fg %1
nice -n 10 command
renice -n 5 -p 1234
4.3 系统资源监控
free -h
free -h -s 2
vmstat 1 5
df -h
du -sh *
du -sh /var/log/*
iostat -x 1
dstat
sar
uptime
cat /proc/loadavg
五、网络管理
5.1 网络配置
ip addr
ip a
ip addr show eth0
ifconfig
ip link show
ip addr add 192.168.1.100/24 dev eth0
ip link set eth0 up
ip route
ip route add default via 192.168.1.1
ip route del default
cat /etc/resolv.conf
echo "nameserver 8.8.8.8" >> /etc/resolv.conf
5.2 网络测试
ping google.com
ping -c 4 192.168.1.1
telnet 192.168.1.1 80
nc -zv 192.168.1.1 22
netstat -tlnp
netstat -tulpn
ss -tulpn
lsof -i :80
traceroute google.com
tracepath google.com
nslookup google.com
dig google.com
host google.com
tcpdump -i eth0
tcpdump -i eth0 port 80
5.3 防火墙管理(firewalld)
systemctl start firewalld
systemctl stop firewalld
systemctl restart firewalld
systemctl enable firewalld
firewall-cmd --state
firewall-cmd --list-all
firewall-cmd --list-ports
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=443/tcp
firewall-cmd --reload
firewall-cmd --permanent --remove-port=80/tcp
firewall-cmd --reload
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
firewall-cmd --list-rich-rules
5.4 端口转发
ssh -L 8080:remote.example.com:80 user@server
ssh -R 8080:localhost:80 user@server
ssh -D 1080 user@server
六、系统管理
6.1 服务管理(systemd)
systemctl start nginx
systemctl start docker
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx
systemctl status nginx
systemctl enable nginx
systemctl disable nginx
systemctl is-enabled nginx
systemctl list-units --type=service
systemctl list-units --all | grep nginx
systemctl --failed
systemctl list-dependencies nginx
6.2 日志管理(journalctl)
journalctl
journalctl -u nginx
journalctl -u docker -f
journalctl -n 100
journalctl --since "1 hour ago"
journalctl --since yesterday
journalctl --since "2026-03-25 00:00:00"
journalctl --until "2026-03-25 12:00:00"
journalctl -k
journalctl -p err
journalctl -p err -b
journalctl -f
journalctl > export.log
journalctl -u nginx > nginx.log
6.3 系统信息查看
cat /etc/redhat-release
cat /etc/os-release
hostnamectl
uname -r
uname -a
uname -m
arch
hostname
hostnamectl set-hostname new-name
uptime
who -b
env
export PATH=$PATH:/new/path
crontab -l
crontab -e
6.4 系统更新与软件管理(yum/dnf)
yum update
yum upgrade
yum check-update
yum install package_name
yum install nginx docker
yum localinstall package.rpm
yum remove package_name
yum erase package_name
yum search keyword
yum info package_name
yum provides nginx
yum list installed
yum list installed | grep docker
yum check-update
yum list updates
yum clean all
yum makecache
yum repolist
yum repolist all
yum-config-manager --add-repo http://repo.url
yum-config-manager --enable epel
yum-config-manager --disable repo
yum history
yum history info 5
yum history undo 5
yum history redo 3
6.5 RPM 包管理
rpm -ivh package.rpm
rpm -Uvh package.rpm
rpm -qa
rpm -qa | grep docker
rpm -q docker
rpm -qi docker
rpm -ql docker
rpm -qf /etc/nginx/nginx.conf
which nginx | xargs rpm -qf
rpm -e package_name
rpm -e --nodeps package
rpm -V package_name
rpm -K package.rpm
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY*
6.6 EPEL 仓库
yum install epel-release
yum repolist | grep epel
yum --enablerepo=epel install htop
yum --disablerepo=epel update
七、磁盘管理
7.1 磁盘使用
df -h
df -i
du -sh /var/log
du -h --max-depth=1 /var/log
find /var/log -type f -size +100M -exec ls -lh {} \;
du -sh * | sort -h
du -sh * | sort -rh | head -10
7.2 磁盘挂载
fdisk -l
lsblk
blkid
mount /dev/sdb1 /mnt
mount -t ntfs /dev/sdb1 /mnt
umount /mnt
umount -l /mnt
mount
df -h
cat /etc/fstab
echo "/dev/sdb1 /mnt xfs defaults 0 0" >> /etc/fstab
7.3 LVM 管理
pvcreate /dev/sdb1
vgcreate vg_name /dev/sdb1
lvcreate -L 10G -n lv_name vg_name
lvcreate -l 100%FREE -n lv_name vg_name
lvextend -L +5G /dev/vg_name/lv_name
lvextend -l +100%FREE /dev/vg_name/lv_name
xfs_growfs /
resize2fs /dev/vg_name/lv_name
vgdisplay
lvdisplay
pvdisplay
八、压缩与解压
8.1 tar 打包
tar -cvf archive.tar file1 file2 dir/
tar -czvf archive.tar.gz file1 file2 dir/
tar -czvf backup.tar.gz /var/www/html/
tar -cjvf archive.tar.bz2 dir/
tar -cJvf archive.tar.xz dir/
tar -xvf archive.tar
tar -xzvf archive.tar.gz
tar -xjvf archive.tar.bz2
tar -xJvf archive.tar.xz
tar -xzvf archive.tar.gz -C /path/to/dir
tar -tzvf archive.tar.gz
8.2 zip 压缩
zip archive.zip file1 file2 dir/
zip -r archive.zip directory/
unzip archive.zip
unzip archive.zip -d /path/to/dir
unzip -l archive.zip
九、文本处理
9.1 文本编辑(vim)
vim file.txt
i
ESC
:
:w
:q
:wq
:q!
u
Ctrl+r
/keyword
?keyword
n
N
:s/old/new/
:s/old/new/g
:%s/old/new/g
:%s/old/new/gc
9.2 文本处理工具
sort file.txt
sort -r file.txt
sort -n file.txt
sort -u file.txt
sort -t: -k2 -n /etc/passwd
uniq file.txt
uniq -c file.txt
sort file.txt | uniq
wc -l file.txt
wc -w file.txt
wc -c file.txt
cut -d: -f1 /etc/passwd
cut -c1-10 file.txt
paste file1.txt file2.txt
diff file1.txt file2.txt
sdiff file1.txt file2.txt
sed 's/old/new/g' file.txt
sed -i 's/old/new/g' file.txt
sed '1,10d' file.txt
sed '/pattern/d' file.txt
awk '{sum+=$1} END {print sum}' file.txt
awk '{print $1, $3}' file.txt
awk -F: '{print $1}' /etc/passwd
十、计划任务
10.1 crontab 定时任务
crontab -e
crontab -l
crontab -r
0 0 * * * /root/backup.sh
*/5 * * * * /root/check.sh
0 9-18 * * 1-5 /root/work.sh
0 0 1 * * /root/monthly.sh
crontab -u username -e
10.2 systemd 定时器
systemctl list-timers
cat > /etc/systemd/system/backup.service <<EOF
[Unit]
Description=Backup Service
[Service]
Type=oneshot
ExecStart=/root/backup.sh
EOF
cat > /etc/systemd/system/backup.timer <<EOF
[Unit]
Description=Run Backup Daily
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
EOF
systemctl enable backup.timer
systemctl start backup.timer
十一、SSH 远程连接
11.1 基本连接
ssh user@192.168.1.100
ssh -p 2222 user@192.168.1.100
ssh -i ~/.ssh/id_rsa user@server
ssh user@server "command"
ssh -L 8080:localhost:80 user@server
ssh -R 8080:localhost:80 user@server
11.2 SSH 密钥管理
ssh-keygen -t rsa -b 4096
ssh-keygen -t ed25519
ssh-copy-id user@server
cat ~/.ssh/id_rsa.pub
cat ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
11.3 SSH 配置
vim ~/.ssh/config
Host server1
HostName 192.168.1.100
User root
Port 22
IdentityFile ~/.ssh/id_rsa
Host server2
HostName example.com
User admin
Port 2222
ssh server1
十二、性能优化
12.1 系统调优
ulimit -a
ulimit -n 65535
ulimit -u 4096
vim /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
net.ipv4.tcp_max_syn_backlog = 8192
vm.swappiness = 10
sysctl -p
12.2 磁盘性能优化
cat /sys/block/sda/queue/scheduler
echo deadline > /sys/block/sda/queue/scheduler
dd if=/dev/zero of=test bs=1M count=1024 oflag=direct
十三、常见故障排查
13.1 磁盘满了解决
find / -type f -size +100M 2>/dev/null | xargs ls -lh
> /var/log/messages
journalctl --vacuum-time=7d
yum clean all
lsof +L1
13.2 内存不足解决
free -h
cat /proc/meminfo
sync; echo 3 > /proc/sys/vm/drop_caches
ps aux --sort=-%mem | head -11
13.3 CPU 占用高
ps aux --sort=-%cpu | head -11
top -o %CPU
nproc
lscpu
13.4 网络问题
ss -s
netstat -an | grep ESTABLISHED | wc -l
ss -ant | awk '{print $1}' | sort | uniq -c | sort -nr
ss -ant | awk '{print $1}' | grep TIME_WAIT | wc -l
vim /etc/sysctl.conf
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
十四、实用技巧
14.1 快捷键
14.2 历史命令
history
history 20
!100
!nginx
Ctrl+R 然后输入关键字
history -c
14.3 管道与重定向
ps aux | grep nginx
cat file.txt | grep error | wc -l
echo "text" > file.txt
command > log.txt 2>&1
echo "text" >> file.txt
mysql -u root -p < backup.sql
cat <<EOF
> line 1
> line 2
> EOF
14.4 后台运行
command &
nohup command &
screen -S session_name
screen -ls
screen -r session_name
Ctrl+A+D
tmux new -s session_name
tmux ls
tmux attach -t session_name
Ctrl+B+D
十五、速查表总结
文件操作
| 操作 |
命令 |
| 复制 |
cp |
| 移动 |
mv |
| 删除 |
rm |
| 查看 |
cat / less / more |
| 查找 |
find / locate |
| 搜索 |
grep |
权限管理
| 操作 |
命令 |
| 修改权限 |
chmod |
| 修改所有者 |
chown |
| 修改组 |
chgrp |
进程管理
| 操作 |
命令 |
| 查看进程 |
ps / top / htop |
| 终止进程 |
kill / killall |
| 后台运行 |
nohup / & |
网络管理
| 操作 |
命令 |
| 查看配置 |
ip addr |
| 查看连接 |
ss / netstat |
| 测试连通 |
ping |
| 防火墙 |
firewall-cmd |
系统管理
| 操作 |
命令 |
| 服务管理 |
systemctl |
| 查看日志 |
journalctl |
| 软件安装 |
yum / dnf |
| 定时任务 |
crontab |
参考资源
十四、实用技巧
14.1 快捷键
14.2 历史命令
history
history 20
!100
!nginx
Ctrl+R 然后输入关键字
history -c
14.3 管道与重定向
ps aux | grep nginx
cat file.txt | grep error | wc -l
echo "text" > file.txt
command > log.txt 2>&1
echo "text" >> file.txt
mysql -u root -p < backup.sql
cat <<EOF
> line 1
> line 2
> EOF
14.4 后台运行
command &
nohup command &
screen -S session_name
screen -ls
screen -r session_name
Ctrl+A+D
tmux new -s session_name
tmux ls
tmux attach -t session_name
Ctrl+B+D
14.5 大括号扩展
touch file{1..10}.txt
touch {a,b,c}.log
cp config.conf{,.bak}
mkdir -p dir1 dir2 dir3
chown user:{user1,user2} file.txt
十五、备份与恢复
15.1 rsync 同步工具
rsync -avz /src/ /dst/
rsync -avz --delete /src/ /dst/
rsync -avz /local/ user@remote:/remote/
rsync -avz user@remote:/remote/ /local/
rsync -avz --ignore-existing /src/ /dst/
rsync -avz --progress /src/ /dst/
rsync -avz --exclude 'log' --exclude '*.tmp' /src/ /dst/
rsync -avz --compress /src/ user@remote:/dst/
rsync -avz --partial /src/ /dst/
15.2 dd 备份
dd if=/dev/sda of=/backup/backup.img bs=4M status=progress
dd if=/backup/backup.img of=/dev/sda bs=4M status=progress
dd if=/dev/zero of=test.img bs=1M count=100
dd if=/dev/sda of=/backup/mbr.bin bs=512 count=1
dd if=/dev/urandom of=/dev/sda bs=4M
dd if=/dev/cdrom of=cd.iso
15.3 tar 备份
tar -czvf backup-$(date +%Y%m%d).tar.gz /path/to/dir
tar -xzvf backup.tar.gz -C /path/to/restore
tar -czvf - /path/to/dir | ssh user@remote "cat > /backup/remote.tar.gz"
tar -tzvf backup.tar.gz
find /path -mtime -1 -print0 | tar -czvf backup.tar.gz --null -T -
十六、时间管理
16.1 时区设置
timedatectl
date
timedatectl list-timezones
timedatectl list-timezones | grep Asia
timedatectl set-timezone Asia/Shanghai
timedatectl set-timezone UTC
hwclock
hwclock -w
hwclock -s
16.2 NTP 时间同步
yum install chrony
systemctl start chronyd
systemctl enable chronyd
chronyc sources -v
chronyc tracking
timedatectl status
chronyc makestep
vim /etc/chrony.conf
server ntp.aliyun.com iburst
server time.cloudflare.com iburst
systemctl restart chronyd
16.3 日期操作
date
date +"%Y-%m-%d %H:%M:%S"
date -s "2026-03-25 18:00:00"
date +"%Y-%m-%d"
date +"%H:%M:%S"
date +"%Y年%m月%d日"
date -d "next monday"
date -d "2 days ago"
date -d "1 month ago" +%Y-%m-%d
date +%s
date -d @1609459200
十七、SELinux 管理
17.1 SELinux 状态
sestatus
getenforce
ls -Z /path/to/file
ps auxZ | grep nginx
getsebool -a
getsebool httpd_can_network_connect
17.2 SELinux 配置
setenforce 0
vim /etc/selinux/config
SELINUX=disabled
setenforce Permissive
setsebool -P httpd_can_network_connect on
setsebool -P ftp_home_dir 1
restorecon -R -v /path/to/dir
chcon -R -t httpd_sys_content_t /var/www/html
17.3 SELinux 故障排查
ausearch -m avc -ts recent
grep AVC /var/log/audit/audit.log
audit2allow -w -a
audit2allow -a -M mymodule
十八、系统启动与 GRUB
18.1 启动项管理
systemctl list-unit-files
systemctl list-unit-files | grep enabled
systemctl list-unit-files | grep disabled
systemctl enable service
systemctl disable service
systemctl --failed
systemctl --type=service --state=running
18.2 运行级别
runlevel
systemctl get-default
systemctl set-default graphical.target
systemctl set-default multi-user.target
systemctl isolate rescue.target
systemctl isolate emergency.target
18.3 GRUB 配置
cat /etc/default/grub
GRUB_DEFAULT=0
GRUB_TIMEOUT=5
GRUB_CMDLINE_LINUX="crashkernel=auto spectre_v2=off"
grub2-mkconfig -o /boot/grub2/grub.cfg
grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg
cat /boot/grub2/grub.cfg | grep menuentry
18.4 内核模块管理
lsmod
cat /proc/modules
modprobe module_name
insmod /path/to/module.ko
modprobe -r module_name
rmmod module_name
modinfo module_name
modprobe --show-depends module_name
十九、高级监控工具
19.1 安装监控工具
yum install -y htop iotop iftop nmap sysstat ncdu
yum install -y glances nmon dstat bcc-tools
19.2 实时监控工具
htop
glances
iotop
iotop -o
iftop -i eth0
iftop -nP
nmon
dstat
dstat -cdngy
19.3 网络监控
iftop
nethogs
ss -s
netstat -an | awk '{print $6}' | sort | uniq -c | sort -nr
tcpdump -i eth0 -nn port 80
tcpdump -i eth0 -w capture.pcap
mtr google.com
tracepath google.com
19.4 性能分析
perf top
perf record -a -g -F 99 sleep 10
slabtop
pmap -x 1234
perf script | ./stackcollapse-perf.pl | ./flamegraph.pl > out.svg
二十、Shell 脚本基础
20.1 变量与参数
name="John"
echo $name
echo ${name}
$0
$1-$9
$#
$@
$*
$?
$$
$!
export VAR=value
echo $PATH
echo $HOME
20.2 条件判断
[ -f file ]
[ -d dir ]
[ -e file ]
[ -r file ]
[ -w file ]
[ -x file ]
[ -s file ]
[ -z "$str" ]
[ -n "$str" ]
[ "$a" = "$b" ]
[ "$a" != "$b" ]
[ $a -eq $b ]
[ $a -ne $b ]
[ $a -gt $b ]
[ $a -ge $b ]
[ $a -lt $b ]
[ $a -le $b ]
[ -f file ] && echo "exists"
[ ! -d dir ] && echo "not exists"
[ -f file ] || [ -f file2 ]
20.3 循环结构
for i in {1..10}; do
echo $i
done
for file in *.txt; do
mv "$file" "${file%.txt}.bak"
done
while [ $count -lt 10 ]; do
echo $count
count=$((count+1))
done
until [ -f "stop.txt" ]; do
echo "waiting..."
sleep 1
done
20.4 函数
function greet() {
echo "Hello, $1!"
}
greet() {
echo "Hello, $1!"
}
greet "World"
check_file() {
if [ -f "$1" ]; then
return 0
else
return 1
fi
}
20.5 一行脚本技巧
for f in *.jpg; do mv "$f" "prefix_$f"; done
find . -type f -exec chmod 644 {} \;
cp important.conf{,.bak}
find . -type f -exec sed -i 's/old/new/g' {} \;
find . -type f | wc -l
find . -name "*.tmp" -mtime +7 -delete
mkdir -p dir1/{dir2,dir3}/{subdir1,subdir2}
for i in {1..10}; do wget http://example.com/file$i; done
二十一、故障排查专题
21.1 系统无法启动
rd.break
switch_root
mount -o remount,rw /sysroot
chroot /sysroot
passwd
exit
touch /.autorelabel
exit
reboot
21.2 忘记 root 密码
1. 重启系统
2. GRUB 菜单按 e
3. linux16 行末尾添加:rd.break
4. 按 Ctrl+x 启动
5. 执行上面的重置密码步骤
21.3 磁盘已满(100%)
du -sh /* 2>/dev/null | sort -hr | head -10
for file in /var/log/*; do > $file; done
journalctl --vacuum-size=500M
journalctl --vacuum-time=7d
yum clean all
rm -rf /var/cache/yum/*
find /backup -name "*.tar.gz" -mtime +30 -delete
rm -rf /tmp/*
rm -rf /var/tmp/*
21.4 内存泄漏排查
free -h
cat /proc/meminfo | grep Slab
ps aux --sort=-%mem | head -20
valgrind --leak-check=full --show-leak-kinds=all ./program
sync; echo 3 > /proc/sys/vm/drop_caches
21.5 CPU 100% 排查
top -o %CPU
ps aux --sort=-%cpu | head -20
ps -eLf | head
perf top
perf record -g -p <PID>
taskset -c 0,1 command
二十二、常用组合命令
find /var/log -name "*.log" -mtime +7 -delete
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -10
ss -ant | awk '{print $1}' | sort | uniq -c | sort -rn
ps aux | grep zombie | awk '{print $2}' | xargs kill -9
tail -f /var/log/messages | grep --line-buffered ERROR
dd if=/dev/zero of=test.img bs=1M count=100
lscpu | grep "^CPU(s):"
nproc
getconf LONG_BIT
uname -m
openssl rand -base64 12
tr -dc A-Za-z0-9 < /dev/urandom | head -c 12
find . -name "*.txt" -exec iconv -f GBK -t UTF-8 {} -o {}.utf8 \;
inotifywait -m -r /path/to/watch