Linux 运维常用命令手册
目录
- 文件与目录操作
- 文件内容查看与编辑
- 用户与权限管理
- 进程管理
- 网络管理
- 磁盘与存储管理
- 系统监控与性能分析
- 软件包管理
- 日志管理
- 定时任务 Crontab
- 压缩与归档
- Shell 脚本基础
- 运维常用技巧
一、文件与目录操作
基础导航
pwd
ls
ls -la
ls -lh
cd /path/to/dir
cd ~
cd -
cd ..
文件与目录的增删改
mkdir mydir
mkdir -p a/b/c
touch file.txt
cp file.txt /tmp/
cp -r dir/ /tmp/
mv file.txt newname.txt
mv file.txt /tmp/
rm file.txt
rm -rf dir/
文件搜索
find / -name "*.log"
find /var -name "*.log" -mtime -7
find . -size +100M
find . -type f -name "nginx*"
which nginx
whereis nginx
locate filename
二、文件内容查看与编辑
查看文件内容
cat file.txt
cat -n file.txt
more file.txt
less file.txt
head -n 20 file.txt
tail -n 50 file.txt
tail -f /var/log/nginx/access.log
文本搜索 grep
grep "error" file.log
grep -i "error" file.log
grep -n "error" file.log
grep -r "error" /var/log/
grep -v "DEBUG" file.log
grep -E "error|warning" file.log
文本处理
awk '{print $1, $3}' file.txt
awk -F: '{print $1}' /etc/passwd
awk '$3 > 1000 {print $1}' /etc/passwd
sed 's/old/new/g' file.txt
sed -i 's/old/new/g' file.txt
sed -n '5,10p' file.txt
sed '/^#/d' nginx.conf
wc -l file.txt
sort file.txt
sort -rn file.txt
uniq -c file.txt
cut -d: -f1 /etc/passwd
Vim 编辑器
vim file.txt
| 模式 | 操作 | 说明 |
|---|
| 普通模式 | i | 进入插入模式 |
| 普通模式 | gg | 跳到文件开头 |
| 普通模式 | G | 跳到文件末尾 |
| 普通模式 | /关键词 | 搜索 |
| 普通模式 | dd | 删除当前行 |
| 普通模式 | yy | 复制当前行 |
| 普通模式 | p | 粘贴 |
| 普通模式 | u | 撤销 |
| 命令模式 | :wq | 保存并退出 |
| 命令模式 | :q! | 强制退出不保存 |
| 命令模式 | :%s/old/new/g | 全文替换 |
三、用户与权限管理
用户管理
useradd username
useradd -m -s /bin/bash tom
passwd username
usermod -aG sudo username
userdel -r username
id username
whoami
w
last
cat /etc/passwd
权限管理
chmod 755 file.sh
chmod +x file.sh
chmod -R 755 /var/www/html
chown user:group file.txt
chown -R nginx:nginx /data/
ls -la
stat file.txt
sudo 权限
sudo command
sudo -i
sudo -s
su - username
visudo
四、进程管理
查看进程
ps aux
ps aux | grep nginx
top
htop
pstree
pidof nginx
pgrep nginx
进程控制
kill PID
kill -9 PID
killall nginx
pkill -f "python script.py"
command &
nohup command &
jobs
fg %1
bg %1
systemd 服务管理(现代Linux必备)
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx
systemctl status nginx
systemctl enable nginx
systemctl disable nginx
systemctl list-units --type=service
五、网络管理
网络状态查看
ip addr
ifconfig
ip route
route -n
ss -tuln
netstat -tuln
ss -tunp
netstat -an | grep ESTABLISHED
ping -c 4 google.com
ping -c 4 -i 0.2 host
traceroute google.com
mtr google.com
curl -I https://example.com
wget https://example.com/file.tar.gz
防火墙管理
firewall-cmd --state
firewall-cmd --list-all
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --remove-port=80/tcp --permanent
firewall-cmd --reload
ufw status
ufw allow 80/tcp
ufw deny 23
ufw enable
iptables -L -n
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
SSH 远程连接
ssh user@192.168.1.100
ssh -p 2222 user@192.168.1.100
ssh -i ~/.ssh/id_rsa user@host
scp file.txt user@host:/path/
scp -r dir/ user@host:/path/
rsync -avz /src/ user@host:/dst/
ssh-keygen -t ed25519 -C "your_email"
ssh-copy-id user@host
六、磁盘与存储管理
df -h
df -Th
du -sh /var/log/
du -sh /*
du -sh * | sort -rh | head
lsblk
fdisk -l
blkid
mount /dev/sdb1 /mnt/data
umount /mnt/data
cat /etc/fstab
smartctl -a /dev/sda
七、系统监控与性能分析
系统信息
uname -a
cat /etc/os-release
hostname
uptime
date
timedatectl
资源监控
top
mpstat -P ALL 1
vmstat 1 5
free -h
cat /proc/meminfo
iostat -x 1
iotop
glances
dstat
系统负载说明
load average: 0.5, 1.2, 1.8
1分钟 5分钟 15分钟
⚠️ 经验法则:负载值 > CPU核心数 × 0.7 就需要关注
八、软件包管理
CentOS / RHEL(yum / dnf)
yum install nginx
yum remove nginx
yum update
yum update nginx
yum search nginx
yum info nginx
yum list installed
Ubuntu / Debian(apt)
apt update
apt install nginx
apt remove nginx
apt autoremove
apt upgrade
apt search nginx
apt show nginx
dpkg -l | grep nginx
九、日志管理
/var/log/syslog
/var/log/messages
/var/log/auth.log
/var/log/nginx/access.log
/var/log/nginx/error.log
journalctl -u nginx
journalctl -u nginx -f
journalctl -n 100
journalctl --since "1 hour ago"
journalctl -p err
tail -f /var/log/nginx/access.log
grep "404" /var/log/nginx/access.log
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -10
十、定时任务 Crontab
crontab -e
crontab -l
crontab -r
crontab -u username -l
Cron 表达式格式
* * * * * command
│ │ │ │ │
│ │ │ │ └─── 星期几 (0-7,0和7都是周日)
│ │ │ └───── 月份 (1-12)
│ │ └─────── 日期 (1-31)
│ └───────── 小时 (0-23)
└─────────── 分钟 (0-59)
常用 Cron 示例
* * * * * /scripts/check.sh
0 2 * * * /scripts/backup.sh
0 8 * * 1 /scripts/report.sh
*/5 * * * * /scripts/monitor.sh
0 3 1 * * /scripts/clean_logs.sh
十一、压缩与归档
tar -czvf backup.tar.gz /data/
tar -xzvf backup.tar.gz
tar -xzvf backup.tar.gz -C /tmp/
tar -tzvf backup.tar.gz
zip -r backup.zip /data/
unzip backup.zip
unzip -l backup.zip
gzip file.txt
gunzip file.txt.gz
bzip2 file.txt
bunzip2 file.txt.bz2
十二、Shell 脚本基础
脚本模板
#!/bin/bash
NAME="World"
DATE=$(date +%Y-%m-%d)
echo "Hello, $NAME! Today is $DATE"
条件判断
if [ -f "/etc/nginx/nginx.conf" ]; then
echo "Nginx配置文件存在"
elif [ -d "/etc/nginx" ]; then
echo "Nginx目录存在但配置文件不在"
else
echo "Nginx未安装"
fi
[ -f file ]
[ -d dir ]
[ -z "$var" ]
[ -n "$var" ]
[ $a -eq $b ]
[ $a -gt $b ]
循环
for i in 1 2 3 4 5; do
echo "第 $i 次"
done
count=0
while [ $count -lt 5 ]; do
echo "count=$count"
((count++))
done
for file in /var/log/*.log; do
echo "处理文件: $file"
done
实用脚本示例
#!/bin/bash
SERVICE="nginx"
if ! systemctl is-active --quiet $SERVICE; then
echo "$(date): $SERVICE 已停止,正在重启..." >> /var/log/service_check.log
systemctl restart $SERVICE
echo "$(date): $SERVICE 重启完成" >> /var/log/service_check.log
else
echo "$(date): $SERVICE 运行正常" >> /var/log/service_check.log
fi
十三、运维常用技巧
历史命令
history
history | grep nginx
!!
!100
Ctrl + R
快捷键
| 快捷键 | 作用 |
|---|
Ctrl + C | 中断当前命令 |
Ctrl + Z | 暂停当前命令 |
Ctrl + D | 退出终端/EOF |
Ctrl + L | 清屏(等同 clear) |
Ctrl + A | 光标移到行首 |
Ctrl + E | 光标移到行尾 |
Ctrl + U | 删除光标前的内容 |
Tab | 自动补全 |
↑ ↓ | 切换历史命令 |
管道与重定向
ps aux | grep nginx | grep -v grep
cat /etc/passwd | awk -F: '{print $1}' | sort
command > file.txt
command >> file.txt
command 2> error.txt
command &> all.txt
command < input.txt
实用组合命令
find /var/log -name "*.log" -mtime +30 -exec rm {} \;
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -10
ss -tunlp | grep :80
watch -n 1 'free -h && echo "---" && df -h'
find /var/www -type f -exec chmod 644 {} \;
find /var/www -type d -exec chmod 755 {} \;
who
w
last | head -20