这份速查表整理了 Debian / Linux 日常维护最常用的命令行操作,并标明 root / sudo 的使用场景。 它按“实际想做什么”来分类:找文件、查端口、看日志、修权限、排网络、管理服务、备份同步、定位性能问题时,可以先看索引,再跳到对应章节复制修改。
快速索引 按任务查命令
我想做什么
常用命令
查看当前用户和权限
whoami、id、groups、sudo -l
切换 root 或提权执行
sudo -i、sudo command、sudoedit、su -
查看命令帮助
command --help、man、info、type、which
目录跳转和查看
pwd、cd、ls -lah、tree、stat
创建、复制、移动、删除
mkdir -p、cp -a、mv -i、rm -i、rm -rf
查看文本和日志
cat、less、head、tail -f、journalctl
搜索文件或内容
find、locate、grep、rg、awk、sed
批量处理文件
find ... -exec、xargs、rename、for
压缩和解压
tar、zip、unzip、gzip、xz、zstd
安装和卸载软件
apt、apt-get、dpkg、apt-cache、apt-mark
查看系统信息
uname、hostnamectl、lsb_release、lscpu、free
查看磁盘空间
df -h、du -h --max-depth=1、lsblk、blkid
挂载磁盘
mount、umount、findmnt、/etc/fstab
管理用户和组
adduser、useradd、passwd、usermod、groupadd
修改权限和所有者
chmod、chown、chgrp、setfacl、getfacl
查看进程
ps aux、top、htop、pgrep、pidof
杀进程
kill、pkill、killall、systemctl stop
管理服务
systemctl、journalctl -u、systemctl list-units
定时任务
crontab、systemctl list-timers、at
网络地址和路由
ip addr、ip route、ip link、resolvectl
查看端口占用
ss -ltnp、lsof -i :80、fuser
测试连通性
ping、curl -I、nc -vz、traceroute、mtr
DNS 排查
dig、nslookup、host、resolvectl query
下载文件
curl -LO、wget -c、aria2c
SSH 登录和传文件
ssh、scp、sftp、rsync
防火墙
ufw、nft、iptables
查看系统日志
journalctl -xe、tail -f /var/log/syslog、dmesg -T
性能排查
free、vmstat、iostat、iotop、sar、strace
Docker 常用操作
docker ps、docker logs、docker exec、docker compose
Git 基础操作
git status、git add、git commit、git pull、git push
分类目录
使用约定
root 表示当前已切换到 root(提示符 #),sudo 表示普通用户前缀 sudo。
文中写成 sudo / root 的命令,普通用户使用 sudo,root 可直接执行。
所有命令默认在 /bin/bash 环境执行;如涉及其它 shell 会额外说明。
命令里的 user、host、PORT、/path 都是占位符,执行前替换成自己的用户名、主机、端口和路径。
高危命令先确认 下面这些命令很有用,但写错目标会造成严重数据损坏:
1 2 3 4 5 6 sudo / root rm -rf /path sudo / root mkfs.ext4 /dev/sdb1 sudo / root dd if =a of=b sudo / root chown -R user:group / sudo / root chmod -R 777 /path sudo / root iptables -F
建议执行前先确认:
1 2 3 4 $ pwd $ ls -lah /target $ find /target -name "*.log" -print $ find /target -name "*.log" -delete
常用组合键 1 2 3 4 5 6 7 8 9 Ctrl + C Ctrl + D Ctrl + A Ctrl + E Ctrl + U Ctrl + K Ctrl + W Ctrl + R Tab
0. 用户身份与权限管理 0.1 查看身份 1 2 3 4 5 6 7 $ whoami $ id $ groups $ logname $ who $ w $ sudo -l
0.2 切换到 root 1 2 3 4 $ sudo -i $ sudo -s $ sudo su - $ su -
如需启用 root 密码,可执行 sudo passwd root(注意存在安全风险)。
0.3 以其他用户执行 1 2 3 4 5 $ sudo -u www-data command $ sudo -iu postgres psql $ sudo -H -u appuser bash $ sudo -E VAR=value command $ sudoedit /etc/nginx/nginx.conf
0.4 用户与组管理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 sudo / root adduser alice sudo / root useradd -m -s /bin/bash bob sudo / root passwd alice sudo / root passwd -l alice sudo / root passwd -u alice sudo / root usermod -aG sudo alice sudo / root usermod -aG docker alice sudo / root gpasswd -d alice docker sudo / root userdel alice sudo / root userdel -r alice $ getent passwd alice $ getent group sudo $ compgen -u $ compgen -g
0.5 权限细节 1 2 3 4 5 6 7 8 9 10 11 12 13 14 $ ls -lah file $ namei -l /var/www/site/index.html sudo / root chmod 644 file sudo / root chmod 600 secret.key sudo / root chmod 755 script.sh sudo / root chmod +x script.sh sudo / root chmod -R u=rwX,g=rX,o= dir sudo / root chown user:group file sudo / root chown -R www-data:www-data /var/www/site $ getfacl file sudo / root setfacl -m u:alice:rw file sudo / root setfacl -m g:dev:rX dir sudo / root setfacl -b file
1. 帮助系统 1 2 3 4 5 6 7 8 9 10 11 $ command --help $ man command $ info command $ apropos keyword $ whatis command $ type command $ type -a command $ which command $ command -v command $ whereis command $ readlink -f /path/link
man 内常用按键:
1 2 3 4 5 6 /keyword 搜索关键词 n 下一个匹配 N 上一个匹配 g 回到开头 G 到末尾 q 退出
2. 软件包管理(APT / dpkg)
适用于 Debian 10/11/12;apt 面向交互,apt-get 更适合脚本。
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 sudo / root apt update sudo / root apt upgrade sudo / root apt full-upgrade sudo / root apt install pkg1 pkg2 sudo / root apt install pkg=version sudo / root apt remove pkg sudo / root apt purge pkg sudo / root apt autoremove sudo / root apt list --upgradable sudo / root apt search keyword sudo / root apt show pkg sudo / root apt policy pkg sudo / root apt list --installed sudo / root apt-mark hold pkg sudo / root apt-mark unhold pkg $ apt-mark showhold $ apt-cache madison pkg sudo / root dpkg -i ./file.deb sudo / root apt -f install sudo / root dpkg -l | grep pkg sudo / root dpkg -L pkg sudo / root dpkg -S /path/file sudo / root dpkg-reconfigure tzdata sudo / root apt clean && apt autoclean
搜索未安装包的文件可使用 apt-file:
1 2 3 sudo / root apt install apt-file sudo / root apt-file update $ apt-file search bin/nmap
2.1 软件源与缓存 1 2 3 4 5 6 $ cat /etc/apt/sources.list $ ls -lah /etc/apt/sources.list.d/ sudo / root cp -a /etc/apt/sources.list /etc/apt/sources.list.bak sudo / root apt update sudo / root apt clean sudo / root apt autoclean
2.2 apt-get 脚本写法 1 2 3 4 sudo / root apt-get update sudo / root apt-get install -y nginx curl sudo / root apt-get remove -y nginx sudo / root apt-get autoremove -y
脚本里建议加上失败即停止,例如:
1 2 3 4 5 #!/usr/bin/env bash set -euo pipefail apt-get update apt-get install -y curl vim
3. 文件与目录操作 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 $ pwd $ ls -lah $ ls -ltr $ ls -lhS $ ls -d */ $ tree -L 2 $ cd ~ $ cd - $ pushd /var/log $ popd sudo / root mkdir -p /opt/app/log sudo / root mkdir -p app/{bin,conf,logs} sudo / root cp -a src/ dst/ sudo / root cp -i a b sudo / root mv -i a b sudo / root rm -i file sudo / root rm -rf /path $ stat file $ file file $ du -sh dir $ realpath file $ readlink link sudo / root ln -s /real target sudo / root ln -sfn /new/release current $ touch file sudo / root chmod 640 file sudo / root chmod -R u=rwX,g=rX dir sudo / root chown user:group file sudo / root chgrp group file
3.1 文本查阅 1 2 3 4 5 6 7 8 9 10 $ cat file $ tac file $ nl file $ head -n 20 file $ tail -n 50 file $ tail -f /var/log/syslog $ tail -F app.log $ less file $ sed -n '10,20p' file $ diff -u file1 file2
less 常用按键:
1 2 3 4 5 6 7 Space / f 下一页 b 上一页 g 开头 G 结尾 /keyword 搜索 n / N 下一个 / 上一个匹配 q 退出
3.2 搜索与替换 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 $ find /etc -maxdepth 1 -type f $ find . -name "*.log" -size +10M $ find . -iname "*.jpg" $ find /var -type f -mtime +7 $ find . -type f -empty $ find . -perm 777 $ locate nginx.conf $ grep -R "pattern" /var/log $ grep -n "ERROR" app.log $ grep -i "error" app.log $ grep -v "DEBUG" app.log $ grep -E "error|failed|timeout" app.log $ grep -C 3 "ERROR" app.log $ grep -R --binary-files=without-match \ --color "ERROR" /var/log/nginx $ sed -i 's/foo/bar/g' file $ awk '{print $1,$NF}' file $ xargs -0 rm < list.txt
如果安装了 ripgrep,rg 通常更快:
1 2 3 4 $ rg "ERROR" /var/log $ rg -n "server_name" /etc/nginx $ rg -i "timeout|refused" . $ rg --files
3.3 sort、uniq、cut、tr、wc 1 2 3 4 5 6 7 8 9 10 $ wc -l file $ wc -c file $ cut -d: -f1 /etc/passwd $ sort file $ sort -n nums.txt $ sort -hr sizes.txt $ uniq file $ sort file | uniq -c | sort -nr $ tr 'a-z' 'A-Z' < file $ tr -d '\r' < win.txt > unix.txt
3.4 JSON 与结构化文本 1 2 3 4 5 sudo / root apt install jq $ jq . data.json $ jq '.name' data.json $ jq -r '.items[].url' data.json $ curl -s https://api.github.com | jq .
4. 压缩与归档 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 sudo / root tar -cvf backup.tar dir / sudo / root tar -xvf backup.tar sudo / root tar -czvf backup.tar.gz dir / sudo / root tar -xzvf backup.tar.gz sudo / root tar -cjvf backup.tar.bz2 dir / sudo / root tar -xjvf backup.tar.bz2 sudo / root tar -cJvf backup.tar.xz dir / sudo / root tar -xJvf backup.tar.xz sudo / root tar -tzvf backup.tar.gz sudo / root tar --exclude=node_modules -czf app.tar.gz app/ $ zip -r archive.zip dir / $ unzip archive.zip -d ./out $ unzip -l archive.zip $ gzip file $ gzip -k file $ gunzip file.gz $ xz -z file $ xz -d file.xz $ zstd file $ unzstd file.zst $ sha256sum file $ sha256sum -c SHA256SUMS
5. 进程与系统资源 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 $ top $ htop $ ps aux | grep nginx $ ps -ef $ pgrep nginx $ pgrep -a nginx $ pstree -ap sudo / root systemctl status nginx sudo / root systemctl restart nginx sudo / root systemctl enable nginx $ pidof process sudo / root kill -SIGTERM PID sudo / root kill -9 PID sudo / root pkill nginx sudo / root pkill -f "python app.py" sudo / root killall nginx sudo / root renice -n -5 -p PID
5.1 后台运行与作业控制 1 2 3 4 5 6 $ command & $ jobs $ fg %1 $ bg %1 $ nohup command > app.log 2>&1 & $ disown -h %1
5.2 系统资源监控 1 2 3 4 5 6 7 8 9 10 $ free -h $ vmstat 1 5 $ iostat -xz 1 $ mpstat 1 $ pidstat 1 $ pidstat -d 1 $ ps aux --sort =-%mem | head $ ps aux --sort =-%cpu | head $ uptime $ dmesg -T | tail
负载粗略理解:
1 2 3 load average 接近 CPU 核心数:系统比较忙 明显超过 CPU 核心数:可能排队严重 CPU 不高但负载高:常见于 IO 等待或不可中断睡眠
6. 服务与启动项(systemd) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 sudo / root systemctl list-units --type =service sudo / root systemctl enable name sudo / root systemctl disable name sudo / root systemctl start name sudo / root systemctl stop name sudo / root systemctl restart name sudo / root systemctl reload name sudo / root systemctl status name sudo / root systemctl is-enabled name sudo / root systemctl is-active name sudo / root systemctl list-unit-files --type =service sudo / root systemctl --failed sudo / root systemctl cat name sudo / root systemctl daemon-reload sudo / root journalctl -u name -f sudo / root journalctl -u name --since "1 hour ago"
6.1 定时任务 1 2 3 4 5 $ crontab -e $ sudo crontab -e $ crontab -l sudo / root systemctl list-timers sudo / root systemctl status timer-name.timer
cron 时间格式:
1 2 * * * * * command 分 时 日 月 周
示例:
1 2 3 4 5 # 每天 03:30 执行备份 30 3 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1 # 每 5 分钟执行一次 */5 * * * * /usr/local/bin/check.sh
6.2 at 一次性任务 1 2 3 4 5 sudo / root apt install at sudo / root systemctl enable --now atd $ echo "touch /tmp/at-test" | at now + 5 minutes $ atq $ atrm JOB_ID
7. 系统信息与内核 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 $ uname -a $ uname -r $ lsb_release -a $ hostnamectl $ cat /etc/os-release $ uptime $ who -b $ df -h $ df -i $ du -h --max-depth=1 /var $ lsblk $ lsblk -f $ blkid sudo / root fdisk -l $ lscpu $ nproc $ free -m $ free -h $ sensors
7.1 时间、时区与主机名 1 2 3 4 5 6 7 8 9 $ date $ timedatectl $ timedatectl list-timezones | grep Shanghai sudo / root timedatectl set-timezone Asia/Shanghai sudo / root timedatectl set-ntp true $ hostname sudo / root hostnamectl set-hostname server-01 $ cat /etc/hostname $ cat /etc/hosts
7.2 环境变量 1 2 3 4 5 6 7 $ env $ printenv PATH $ echo "$PATH " $ export APP_ENV=production $ unset APP_ENV $ echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bashrc $ source ~/.bashrc
常见配置文件:
1 2 3 4 /etc/profile 全局 login shell ~/.profile 用户 login shell ~/.bashrc 用户交互式 bash /etc/environment 全局环境变量
8. 网络与防火墙 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 $ ip addr show $ ip a $ ip link $ ip route $ ip route get 8.8.8.8 $ ip -s link $ ss -ltnp $ ss -lunp $ ss -tunap $ ss -s $ ss -putona $ dig example.com +short $ dig @1.1.1.1 example.com +short $ nslookup example.com $ host example.com $ resolvectl status $ resolvectl query example.com $ curl -I https://example.com $ curl -L https://example.com $ curl -o file.html https://example.com $ curl -w "\nHTTP %{http_code} in %{time_total}s\n" -o /dev/null -s https://example.com $ wget -c URL
8.1 连接排查 1 2 3 4 5 6 7 8 9 $ ping -c 4 1.1.1.1 $ traceroute example.com $ mtr -rw example.com $ nc -vz host port sudo / root lsof -i :80 sudo / root fuser -v 80/tcp sudo / root tcpdump -i eth0 port 80 sudo / root tcpdump -i eth0 -nn -s0 -w capture.pcap sudo / root tcpdump -r capture.pcap
8.2 curl 常用写法 1 2 3 4 5 6 7 8 $ curl -I https://example.com $ curl -Ik https://example.com $ curl -LO https://example.com/file.tar.gz $ curl --connect-timeout 5 https://example.com $ curl -X POST https://api.example.com/login \ -H "Content-Type: application/json" \ -d '{"username":"alice","password":"secret"}'
8.3 防火墙(ufw / iptables / nftables 示例) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 sudo / root ufw status sudo / root ufw status verbose sudo / root ufw default deny incoming sudo / root ufw default allow outgoing sudo / root ufw allow 22/tcp sudo / root ufw allow 80/tcp sudo / root ufw allow 443/tcp sudo / root ufw allow from 203.0.113.5 to any port 22 proto tcp sudo / root ufw delete allow 22/tcp sudo / root ufw enable sudo / root ufw disable sudo / root iptables -L -n -v sudo / root iptables -S sudo / root iptables -A INPUT -p tcp --dport 22 -j ACCEPT sudo / root iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT sudo / root iptables -A INPUT -j DROP sudo / root nft list ruleset sudo / root systemctl status nftables
启用防火墙前务必确认 SSH 端口已放行,否则可能把自己锁在服务器外面。
9. 存储、挂载与备份 1 2 3 4 5 6 7 8 9 10 11 sudo / root mount /dev/sdb1 /mnt/data sudo / root umount /mnt/data sudo / root umount -l /mnt/data $ lsblk -f $ blkid /dev/sdb1 $ findmnt sudo / root sudoedit /etc/fstab sudo / root mount -a sudo / root rsync -avz src/ dest/ sudo / root rsync -av --delete src/ dest/ sudo / root rsync -avn --delete src/ dest/
9.1 磁盘空间排查 1 2 3 4 5 6 7 $ df -h $ df -i $ du -sh /var/log $ du -h --max-depth=1 /var | sort -hr sudo / root find / -xdev -type f -size +500M -print sudo / root du -ah /var | sort -rh | head -n 30 sudo / root lsof +L1
lsof +L1 很适合排查“明明删了日志,磁盘空间还没回来”的情况。
9.2 /etc/fstab 开机挂载 先查 UUID:
1 2 $ lsblk -f $ blkid /dev/sdb1
备份并编辑:
1 2 sudo / root cp -a /etc/fstab /etc/fstab.bak sudo / root sudoedit /etc/fstab
示例:
1 UUID=xxxx-xxxx /mnt/data ext4 defaults,nofail 0 2 # 用 UUID 将 ext4 分区挂载到 /mnt/data
验证:
1 2 sudo / root mount -a $ findmnt /mnt/data
9.3 swap 1 2 3 4 5 6 $ swapon --show $ free -h sudo / root fallocate -l 2G /swapfile sudo / root chmod 600 /swapfile sudo / root mkswap /swapfile sudo / root swapon /swapfile
写入 /etc/fstab:
1 /swapfile none swap sw 0 0 # 开机自动启用 /swapfile
9.4 LVM(简要) 1 2 3 4 5 6 7 8 9 10 sudo / root pvcreate /dev/sdb sudo / root vgcreate vgdata /dev/sdb sudo / root lvcreate -L 20G -n lvbackup vgdata sudo / root mkfs.ext4 /dev/vgdata/lvbackup sudo / root mkdir -p /mnt/backup sudo / root mount /dev/vgdata/lvbackup /mnt/backup $ pvs $ vgs $ lvs
10. 日志与故障排查 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 sudo / root journalctl -xe sudo / root journalctl -f sudo / root journalctl -p err sudo / root journalctl --since "today" sudo / root journalctl --since "2025-11-01" sudo / root journalctl --disk-usage sudo / root journalctl --vacuum-time=7d sudo / root journalctl --vacuum-size=1G $ tail -f /var/log/syslog $ tail -f /var/log/auth.log $ tail -f /var/log/kern.log $ less /var/log/nginx/error.log sudo / root strace -p PID sudo / root strace -f -p PID sudo / root strace -o trace.log command sudo / root lsof -i :80 sudo / root lsof /path/file sudo / root lsof -p PID $ dmesg -T | grep -i error $ dmesg -T | grep -i "error\|fail\|oom\|killed" $ last -n 10 $ lastb -n 10
10.1 常见排错流程 服务打不开:
1 2 3 4 sudo / root systemctl status service sudo / root journalctl -u service -n 100 --no-pager sudo / root ss -ltnp sudo / root service-binary -t
端口不通:
1 2 3 4 5 6 $ ss -ltnp $ curl -I http://127.0.0.1:PORT $ nc -vz 127.0.0.1 PORT $ ip route sudo / root ufw status verbose sudo / root tcpdump -i any port PORT
磁盘满:
1 2 3 4 5 $ df -h $ df -i sudo / root du -xh / | sort -rh | head -n 30 sudo / root lsof +L1 sudo / root journalctl --disk-usage
内存或 OOM:
1 2 3 4 $ free -h $ ps aux --sort =-%mem | head $ dmesg -T | grep -i "out of memory\|oom" sudo / root journalctl -k | grep -i oom
11. 安全与权限建议
使用 sudo -i 获取 root 会话,完毕后及时 exit。
编辑系统文件优先 sudoedit,避免直接 sudo nano 遗留 root 所有权。
chmod / chown 操作时,先用 -R 谨慎范围,推荐测试后再递归。
配置 SSH:sudo nano /etc/ssh/sshd_config → 禁止 root 远程登录、使用密钥认证。
定期审计 sudo 日志:sudo cat /var/log/auth.log | grep sudo.
12. 常见小脚本 1 2 3 4 5 6 7 8 9 10 11 sudo / root bash -c 'tar -czf /backup/www-$(date +%F).tar.gz /var/www' sudo / root find /backup -name "www-*.tar.gz" -mtime +7 -delete sudo / root du -h /var | sort -hr | head -n 20 url="https://tz.202221.xyz" code=$(curl -ks -o /dev/null -w "%{http_code}" "$url " ) printf "HTTP %s -> %s\n" "$code " "$url "
13. Shell 效率、管道与脚本片段 13.1 历史命令与快捷执行 1 2 3 4 5 6 7 8 $ history $ history | grep ssh $ !123 $ !! $ sudo !! $ fc $ clear $ reset
13.2 别名 alias 1 2 3 4 5 6 $ alias $ alias ll='ls -lah' $ alias gs='git status' $ unalias ll $ echo "alias ll='ls -lah'" >> ~/.bashrc $ source ~/.bashrc
常用别名示例:
1 2 3 4 5 alias ll='ls -lah' alias la='ls -A' alias grep='grep --color=auto' alias ports='ss -ltnp' alias untar='tar -xzvf'
13.3 管道与重定向 1 2 3 4 5 6 7 8 $ command > file $ command >> file $ command 2> err.log $ command > out.log 2>&1 $ command | less $ command | tee out.log $ command | tee -a out.log $ command >/dev/null 2>&1
13.4 命令连接符 1 2 3 4 $ make && make install $ command || echo "failed" $ command1; command2 $ (cd /tmp && ls )
13.5 输入输出与 here document 1 2 3 4 5 6 7 8 9 $ cat > note.txt $ cat <<'EOF' hello world EOF $ sudo tee /etc/motd >/dev/null <<'EOF' Welcome to this server. EOF
sudo echo "x" > /root/file 常常失败,因为重定向由当前 shell 完成。需要写 root 文件时,用 sudo tee 更稳。
13.6 循环和批量处理 1 2 3 4 5 $ for f in *.log ; do echo "$f " ; done $ for f in *.jpg; do mv "$f " "${f%.jpg} .jpeg" ; done $ while read -r line; do echo "$line " ; done < list.txt $ find . -type f -name "*.php" -exec chmod 644 {} \; $ find . -type d -exec chmod 755 {} \;
批量改名如果系统有 rename:
1 2 $ rename 's/\.jpeg$/.jpg/' *.jpeg $ rename 'y/A-Z/a-z/' *
13.7 脚本模板 1 2 3 4 5 6 7 8 #!/usr/bin/env bash set -euo pipefail main () { echo "hello" } main "$@ "
带日志和错误提示:
1 2 3 4 5 6 7 8 9 10 11 12 #!/usr/bin/env bash set -euo pipefail log () { printf '[%s] %s\n' "$(date '+%F %T') " "$*" } trap 'log "failed at line $LINENO"' ERR log "start" log "done"
13.8 tmux / screen 1 2 3 4 5 sudo / root apt install tmux $ tmux new -s work $ tmux ls $ tmux attach -t work $ tmux kill-session -t work
tmux 默认快捷键前缀是 Ctrl+b:
1 2 3 4 5 Ctrl+b d 离开会话,程序继续运行 Ctrl+b c 新建窗口 Ctrl+b n / p 下一个 / 上一个窗口 Ctrl+b % 左右分屏 Ctrl+b " 上下分屏
14. SSH、密钥与远程同步 14.1 SSH 登录 1 2 3 4 5 $ ssh user@host $ ssh user@host -p 2222 $ ssh -i ~/.ssh/id_ed25519 user@host $ ssh -v user@host $ ssh -vvv user@host
14.2 生成 SSH 密钥 1 2 3 4 $ ssh-keygen -t ed25519 -C "your_email@example.com" $ ssh-keygen -t rsa -b 4096 -C "your_email@example.com" $ ls -lah ~/.ssh $ cat ~/.ssh/id_ed25519.pub
权限建议:
1 2 3 4 $ chmod 700 ~/.ssh $ chmod 600 ~/.ssh/id_ed25519 $ chmod 644 ~/.ssh/id_ed25519.pub $ chmod 600 ~/.ssh/authorized_keys
14.3 复制公钥到服务器 1 2 $ ssh-copy-id user@host $ ssh-copy-id -p 2222 user@host
没有 ssh-copy-id 时可手动追加:
1 $ cat ~/.ssh/id_ed25519.pub | ssh user@host 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys'
14.4 SSH 配置文件 ~/.ssh/config 示例:
1 2 3 4 5 6 7 Host my-vps # 自定义连接别名 HostName 203.0.113.10 # 真实服务器 IP 或域名 User root # 默认登录用户 Port 22 # 默认 SSH 端口 IdentityFile ~/.ssh/id_ed25519 # 默认使用的私钥 ServerAliveInterval 30 # 每 30 秒发送保活包 ServerAliveCountMax 3 # 连续 3 次无响应后断开
使用:
1 2 $ ssh my-vps $ scp file.txt my-vps:/root/
14.5 scp / sftp 传文件 1 2 3 4 5 $ scp file.txt user@host:/tmp/ $ scp user@host:/tmp/file.txt ./ $ scp -P 2222 file.txt user@host:/tmp/ $ scp -r ./dir user@host:/tmp/ $ sftp user@host
14.6 SSH 隧道 本地端口转发:
1 2 $ ssh -L 8080:127.0.0.1:80 user@host
远程端口转发:
1 2 $ ssh -R 9000:127.0.0.1:3000 user@host
SOCKS 代理:
14.7 ssh-agent 1 2 3 4 $ eval "$(ssh-agent -s) " $ ssh-add ~/.ssh/id_ed25519 $ ssh-add -l $ ssh-add -D
15. Docker / Compose 常用命令 15.1 镜像与容器 1 2 3 4 5 6 7 8 9 10 11 12 $ docker version $ docker info $ docker images $ docker pull nginx:latest $ docker ps $ docker ps -a $ docker run --name web -d -p 8080:80 nginx $ docker stop web $ docker start web $ docker restart web $ docker rm web $ docker rmi nginx:latest
15.2 日志、进入容器、复制文件 1 2 3 4 5 6 $ docker logs web $ docker logs -f --tail =100 web $ docker exec -it web bash $ docker exec -it web sh $ docker cp web:/etc/nginx/nginx.conf ./nginx.conf $ docker cp ./index.html web:/usr/share/nginx/html/index.html
15.3 网络、卷与资源 1 2 3 4 5 6 $ docker network ls $ docker network inspect bridge $ docker volume ls $ docker volume inspect volume_name $ docker stats $ docker inspect web
15.4 清理 1 2 3 4 5 6 $ docker system df $ docker container prune $ docker image prune $ docker volume prune $ docker system prune $ docker system prune -a
15.5 Docker Compose 1 2 3 4 5 6 7 8 9 $ docker compose up -d $ docker compose down $ docker compose ps $ docker compose logs -f $ docker compose logs -f service $ docker compose restart service $ docker compose pull $ docker compose up -d --build $ docker compose exec service bash
16. Git 常用命令 16.1 基础流程 1 2 3 4 5 6 $ git status $ git add file $ git add . $ git commit -m "message" $ git pull $ git push
16.2 查看历史和差异 1 2 3 4 5 $ git log --oneline --graph --decorate --all $ git diff $ git diff --staged $ git show HEAD $ git blame file
16.3 分支 1 2 3 4 5 6 $ git branch $ git branch new-feature $ git switch new-feature $ git switch -c new-feature $ git merge main $ git branch -d old-feature
16.4 远程仓库 1 2 3 4 $ git remote -v $ git remote add origin git@github.com:user/repo.git $ git remote set-url origin git@github.com:user/repo.git $ git fetch --all --prune
16.5 撤销常见操作 1 2 3 4 5 6 $ git restore file $ git restore --staged file $ git commit --amend $ git revert COMMIT $ git reset --soft HEAD~1 $ git reset --mixed HEAD~1
git reset --hard 会丢弃修改,使用前要非常确定。
17. Nginx / Web 服务常用排查 17.1 Nginx 服务 1 2 3 4 5 sudo / root nginx -t sudo / root systemctl reload nginx sudo / root systemctl restart nginx sudo / root systemctl status nginx sudo / root journalctl -u nginx -n 100 --no-pager
查配置:
1 2 3 sudo / root nginx -T | less sudo / root grep -R "server_name" /etc/nginx sudo / root grep -R "listen" /etc/nginx
查日志:
1 2 3 4 $ tail -f /var/log/nginx/access.log $ tail -f /var/log/nginx/error.log $ awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head $ awk '$9 >= 500 {print $0}' /var/log/nginx/access.log | tail
17.2 HTTP 状态和 TLS 1 2 3 4 5 $ curl -I http://example.com $ curl -Ik https://example.com $ curl -L -I https://example.com $ openssl s_client -connect example.com:443 -servername example.com $ echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -dates -issuer -subject
17.3 常见 Web 问题定位 502 / 504:
1 2 3 4 sudo / root systemctl status php*-fpm sudo / root ss -ltnp $ tail -f /var/log/nginx/error.log $ curl -I http://127.0.0.1:BACKEND_PORT
证书问题:
1 2 3 $ date $ curl -Iv https://domain.com $ openssl x509 -in cert.pem -noout -dates -subject -issuer
权限问题:
1 2 3 4 5 $ namei -l /var/www/site/index.html $ ls -lah /var/www/site sudo / root chown -R www-data:www-data /var/www/site sudo / root find /var/www/site -type d -exec chmod 755 {} \; sudo / root find /var/www/site -type f -exec chmod 644 {} \;
18. 常见场景速查 18.1 新服务器初始化 1 2 3 4 5 6 7 8 9 10 sudo / root apt update sudo / root apt full-upgrade -y sudo / root apt install -y curl wget vim git htop tmux unzip zip ca-certificates gnupg lsb-release sudo / root timedatectl set-timezone Asia/Shanghai sudo / root adduser deploy sudo / root usermod -aG sudo deploy sudo / root ufw allow 22/tcp sudo / root ufw allow 80/tcp sudo / root ufw allow 443/tcp sudo / root ufw enable
18.2 快速判断服务器是否正常 1 2 3 4 5 6 $ uptime $ free -h $ df -h $ ss -ltnp sudo / root systemctl --failed sudo / root journalctl -p err --since "1 hour ago"
18.3 找出最占空间的目录和文件 1 2 3 4 5 $ df -h sudo / root du -xh / --max-depth=1 | sort -hr sudo / root du -xh /var --max-depth=1 | sort -hr sudo / root find /var -type f -size +200M -print sudo / root lsof +L1
18.4 查某个端口为什么访问不了 1 2 3 4 5 sudo / root ss -ltnp | grep ':PORT' $ curl -I http://127.0.0.1:PORT $ nc -vz SERVER_IP PORT sudo / root ufw status verbose sudo / root journalctl -u service --since "30 minutes ago"
18.5 查某个服务为什么启动失败 1 2 3 4 sudo / root systemctl status service --no-pager sudo / root journalctl -u service -n 200 --no-pager sudo / root systemctl cat service sudo / root systemctl daemon-reload
18.6 安全清理旧日志 1 2 3 sudo / root find /var/log/myapp -type f -name "*.log" -mtime +14 -print sudo / root find /var/log/myapp -type f -name "*.log" -mtime +14 -delete sudo / root journalctl --vacuum-time=14d
18.7 备份网站目录 1 2 3 sudo / root mkdir -p /backup sudo / root tar -czf /backup/www-$(date +%F).tar.gz /var/www sudo / root find /backup -name "www-*.tar.gz" -mtime +7 -delete
18.8 同步网站到远程服务器 1 $ rsync -avz --delete -e ssh ./public/ user@host:/var/www/site/
18.9 查看访问量最高的 IP 和 URL 1 2 3 $ awk '{print $1}' access.log | sort | uniq -c | sort -nr | head -n 20 $ awk '{print $7}' access.log | sort | uniq -c | sort -nr | head -n 20 $ awk '$9 >= 500 {print $7}' access.log | sort | uniq -c | sort -nr | head -n 20
18.10 快速生成随机密码 1 2 $ openssl rand -base64 24 $ tr -dc 'A-Za-z0-9' </dev/urandom | head -c 24; echo
延伸阅读
建议将本文保存到团队 Wiki 或打印成小册,方便在服务器环境下快速查阅。