常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 查看当前路径
pwd

# 查看目录
ls -lah

# 创建目录
mkdir -p /data/app/logs

# 复制文件
cp file.txt /tmp/

# 复制目录
cp -r /data/app /backup/

# 保留权限、属主和时间复制目录
cp -a /data/app /backup/

# 远程同步目录,适合备份和迁移
rsync -avh --progress /data/app/ user@server:/backup/app/

# 移动或重命名
mv old.txt new.txt

# 删除文件
rm file.txt

# 删除目录,执行前确认路径
rm -rf /data/app/cache

# 查看文件
cat file.txt
less file.txt

# 查看文件前后内容
head -n 50 file.txt
tail -n 50 file.txt

# 实时查看日志
tail -f /var/log/syslog
tail -f /var/log/nginx/access.log

# 搜索文件内容
grep -n "error" app.log
grep -Rni "listen" /etc/nginx/

# 查找文件
find /var/log -name "*.log"
find /data -type f -size +100M

# 查看命令位置
which nginx
command -v docker

# 查看命令帮助
man tar
curl --help

系统信息与时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 查看系统版本
cat /etc/os-release
hostnamectl

# 查看内核版本
uname -a
uname -r

# 查看主机名
hostname

# 修改主机名
hostnamectl set-hostname server-01

# 查看时间和时区
date
timedatectl

# 设置时区
timedatectl set-timezone Asia/Shanghai

# 查看登录用户
w
who
last

# 查看环境变量
env
printenv PATH

压缩解压

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 打包目录
tar -cvf app.tar /data/app

# 打包并 gzip 压缩
tar -czvf app.tar.gz /data/app

# 解压 tar
tar -xvf app.tar

# 解压 tar.gz
tar -xzvf app.tar.gz

# 解压到指定目录
tar -xzvf app.tar.gz -C /opt/

# 查看压缩包内容
tar -tf app.tar.gz

# zip 压缩
zip -r app.zip /data/app

# unzip 解压
unzip app.zip

# unzip 解压到指定目录
unzip app.zip -d /opt/app

权限和用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 查看用户和组
id
whoami

# 修改文件属主
chown user:group file.txt
chown -R www-data:www-data /var/www/html

# 修改权限
chmod 644 file.txt
chmod 755 script.sh

# 给脚本增加执行权限
chmod +x deploy.sh

# 使用 sudo 执行命令
sudo systemctl restart nginx

# 切换用户
su - username
sudo -iu username

进程和服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 查看进程
ps aux
ps aux | grep nginx

# 按端口查进程
lsof -i :80
ss -tlnp | grep :80

# 结束进程
kill <pid>
kill -9 <pid>

# 查看 systemd 服务状态
systemctl status nginx

# 启动、停止、重启服务
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx

# 设置开机自启
systemctl enable nginx
systemctl disable nginx

# 查看服务日志
journalctl -u nginx
journalctl -u nginx -f
journalctl -u nginx --since "1 hour ago"

日志排查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 查看 systemd 启动日志
journalctl -b

# 查看最近 100 行日志
journalctl -n 100

# 实时查看系统日志
journalctl -f

# 按时间过滤日志
journalctl --since "2026-06-15 10:00:00"
journalctl --since "1 hour ago"

# 查看内核日志
dmesg
dmesg -T

# 查看认证日志,Ubuntu / Debian
tail -f /var/log/auth.log

# 查看认证日志,CentOS / RHEL
tail -f /var/log/secure

# 查看 Nginx 日志
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log

磁盘和目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 查看磁盘空间
df -h

# 查看目录占用
du -sh /data/*

# 查看当前目录下文件和目录大小并排序
du -sh * | sort -h

# 查看块设备
lsblk

# 查看挂载
mount
findmnt

# 查看 inode 使用情况
df -ih

# 找大文件
find / -type f -size +1G 2>/dev/null

dd 命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 使用 dd 前先确认磁盘名称,避免写错盘
lsblk
fdisk -l

# 常用参数
# if: 输入文件或设备
# of: 输出文件或设备
# bs: 每次读写块大小
# count: 读写块数量
# status=progress: 显示进度
# conv=fsync: 写入完成前同步到磁盘

# 创建 1G 空文件
dd if=/dev/zero of=test.img bs=1M count=1024 status=progress

# 测试磁盘顺序写入速度
dd if=/dev/zero of=write-test.tmp bs=1G count=1 oflag=direct status=progress
rm write-test.tmp

# 测试磁盘顺序读取速度
dd if=/dev/sda of=/dev/null bs=4M status=progress

# 备份整个磁盘为镜像
sudo dd if=/dev/sda of=/backup/sda.img bs=4M status=progress conv=fsync

# 从镜像恢复到磁盘,高风险:of 写错会覆盖目标磁盘
sudo dd if=/backup/sda.img of=/dev/sda bs=4M status=progress conv=fsync

# 克隆磁盘,高风险:of 是被覆盖的目标盘
sudo dd if=/dev/sda of=/dev/sdb bs=4M status=progress conv=fsync

# 备份 MBR
sudo dd if=/dev/sda of=/backup/sda-mbr.bin bs=512 count=1

# 恢复 MBR,高风险
sudo dd if=/backup/sda-mbr.bin of=/dev/sda bs=512 count=1

# 写入 ISO 到 U 盘,高风险:确认 /dev/sdX 是 U 盘,不要带分区号
sudo dd if=ubuntu.iso of=/dev/sdX bs=4M status=progress conv=fsync

# 强制刷新磁盘缓存
sync

资源监控

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 系统实时监控
top

# 更友好的实时监控,需要先安装
htop

# CPU、内存、负载
vmstat 1

# 磁盘 IO
iostat -xz 1

# 内存使用
free -h

# 查看系统运行时间和负载
uptime

# 查看 CPU 信息
lscpu

# 查看内存信息
cat /proc/meminfo

# 查看占用 CPU 最高的进程
ps aux --sort=-%cpu | head

# 查看占用内存最高的进程
ps aux --sort=-%mem | head

网络监控

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 查看网卡和 IP
ip addr
ip a

# 查看路由
ip route

# 查看监听端口
ss -tlnp
ss -ulnp

# 查看所有 TCP 连接
ss -tan

# 抓包
tcpdump
tcpdump -i eth0
tcpdump -i eth0 port 443
tcpdump -i eth0 host 8.8.8.8
tcpdump -i eth0 -w capture.pcap

# 测试连通性
ping 8.8.8.8

# DNS 查询
dig example.com
nslookup example.com

# HTTP 请求调试
curl -I https://example.com
curl -v https://example.com

# 测试端口连通
nc -vz example.com 443
telnet example.com 443

# 下载文件
wget https://example.com/file.tar.gz
curl -LO https://example.com/file.tar.gz

SSH 和文件传输

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 连接服务器
ssh user@server
ssh -p 2222 user@server

# 使用私钥连接
ssh -i ~/.ssh/id_rsa user@server

# 生成 SSH 密钥
ssh-keygen -t ed25519 -C "[email protected]"

# 复制公钥到服务器
ssh-copy-id user@server
ssh-copy-id -p 2222 user@server

# 本地复制到远程
scp file.txt user@server:/tmp/
scp -r ./app user@server:/data/

# 远程复制到本地
scp user@server:/tmp/file.txt ./

# rsync 增量同步
rsync -avh --progress ./app/ user@server:/data/app/

# SSH 本地端口转发
ssh -L 8080:127.0.0.1:80 user@server

# SSH 远程端口转发
ssh -R 8080:127.0.0.1:80 user@server

防火墙

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# ufw 查看状态
ufw status

# ufw 放行端口
ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp

# ufw 删除规则
ufw delete allow 80/tcp

# ufw 启用和关闭
ufw enable
ufw disable

# firewalld 查看状态
firewall-cmd --state
firewall-cmd --list-all

# firewalld 放行端口
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=443/tcp
firewall-cmd --reload

# firewalld 删除端口
firewall-cmd --permanent --remove-port=80/tcp
firewall-cmd --reload

包管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Debian / Ubuntu 更新软件源
apt update

# Debian / Ubuntu 安装软件
apt install nginx

# Debian / Ubuntu 卸载软件
apt remove nginx
apt purge nginx

# RHEL / CentOS / Fedora 安装软件
dnf install nginx
yum install nginx

# 查看已安装软件
dpkg -l | grep nginx
rpm -qa | grep nginx

定时任务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 编辑当前用户 crontab
crontab -e

# 查看当前用户 crontab
crontab -l

# 删除当前用户 crontab,谨慎执行
crontab -r

# 每 5 分钟执行一次
*/5 * * * * /data/scripts/check.sh

# 每天 3 点执行一次
0 3 * * * /data/scripts/backup.sh

# 查看 cron 服务状态
systemctl status cron
systemctl status crond

# 查看定时任务日志,Ubuntu / Debian
grep CRON /var/log/syslog

# 查看定时任务日志,CentOS / RHEL
tail -f /var/log/cron

文本处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 按列查看
awk '{print $1, $2}' access.log

# 统计访问 IP 次数
awk '{print $1}' access.log | sort | uniq -c | sort -nr | head

# 替换文本
sed 's/old/new/g' file.txt

# 原地替换文本,执行前建议先备份
sed -i.bak 's/old/new/g' file.txt

# 去重
sort file.txt | uniq

# 统计行数
wc -l file.txt

# 统计目录中文件数量
find . -type f | wc -l

# 查看 CSV 前几列
cut -d',' -f1,2 data.csv

经验总结

  1. wireguard、tailscale 等网络类应用尽量不使用 docker 安装,容易出现路由、权限、内核模块相关问题。
  2. 删除、覆盖、迁移数据前先执行 pwdls -lahdf -h 确认路径和磁盘空间。
  3. 排查服务问题优先看三件事:systemctl statusjournalctl -u、端口监听 ss -tlnp
  4. 线上执行 rm -rfchmod -Rchown -R 前先用 echols 验证目标路径。