这份速查表整理了 Debian 日常维护最常用的命令行操作,并标明 root / sudo 的使用场景,适合快速上手与日常查阅。
使用约定
root 表示当前已切换到 root(提示符 #),sudo 表示普通用户前缀 sudo。
- 文中写成
sudo / root 的命令,普通用户使用 sudo,root 可直接执行。
- 所有命令默认在
/bin/bash 环境执行;如涉及其它 shell 会额外说明。
0. 用户身份与权限管理
0.1 查看身份
0.2 切换到 root
1 2 3 4
| $ sudo -i $ sudo -s $ sudo su - $ su -
|
如需启用 root 密码,可执行 sudo passwd root(注意存在安全风险)。
0.3 以其他用户执行
1 2 3
| $ sudo -u www-data command $ sudo -E VAR=value command $ sudoedit /etc/nginx/nginx.conf
|
1. 帮助系统
1 2 3 4 5 6
| $ command --help $ man command $ info command $ apropos keyword $ type command $ which command
|
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
| 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-mark hold pkg sudo / root apt-mark unhold pkg
sudo / root dpkg -i ./file.deb sudo / root dpkg -L pkg sudo / root dpkg -S /path/file
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
|
3. 文件与目录操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| $ pwd $ ls -lah $ tree -L 2 $ cd -
sudo / root mkdir -p /opt/app/log sudo / root cp -a src/ dst/ sudo / root mv -i a b sudo / root rm -rf /path
$ stat file sudo / root ln -s /real target $ 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
| $ cat file $ tac file $ head -n 20 file $ tail -f /var/log/syslog $ less file $ sed -n '10,20p' file $ diff -u file1 file2
|
3.2 搜索与替换
1 2 3 4 5 6 7 8 9
| $ find /etc -maxdepth 1 -type f $ find . -name "*.log" -size +10M $ locate nginx.conf $ grep -R "pattern" /var/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
|
4. 压缩与归档
1 2 3 4 5 6
| sudo / root tar -czvf backup.tar.gz dir/ sudo / root tar -xzvf backup.tar.gz $ zip -r archive.zip dir/ $ unzip archive.zip -d ./out $ gzip file && gunzip file.gz $ xz -z file && xz -d file.xz
|
5. 进程与系统资源
1 2 3 4 5 6 7 8 9 10 11
| $ top $ htop $ ps aux | grep nginx 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 renice -n -5 -p PID
|
5.1 系统资源监控
1 2 3 4 5 6
| $ free -h $ vmstat 1 5 $ iostat -xz 1 $ mpstat 1 $ uptime $ dmesg -T | tail
|
6. 服务与启动项(systemd)
1 2 3 4 5 6 7 8 9
| 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 reload name sudo / root systemctl status name 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
|
7. 系统信息与内核
1 2 3 4 5 6 7 8 9 10 11 12
| $ uname -a $ lsb_release -a $ hostnamectl $ cat /etc/os-release $ df -h $ du -h --max-depth=1 /var $ lsblk $ blkid sudo / root fdisk -l $ lscpu $ free -m $ sensors
|
8. 网络与防火墙
1 2 3 4 5 6 7 8 9
| $ ip addr show $ ip route $ ip -s link $ ss -ltnp $ ss -putona $ dig example.com +short $ nslookup example.com $ curl -I https://example.com $ wget -c URL
|
8.1 连接排查
1 2 3 4 5
| $ ping -c 4 1.1.1.1 $ traceroute example.com $ mtr -rw example.com $ nc -vz host port sudo / root tcpdump -i eth0 port 80
|
8.2 防火墙(ufw / iptables 示例)
1 2 3 4 5 6 7 8
| sudo / root ufw status sudo / root ufw allow 22/tcp sudo / root ufw delete allow 22/tcp sudo / root ufw enable
sudo / root iptables -L -n -v sudo / root iptables -A INPUT -p tcp --dport 22 -j ACCEPT sudo / root iptables -A INPUT -j DROP
|
9. 存储、挂载与备份
1 2 3 4 5 6 7
| sudo / root mount /dev/sdb1 /mnt/data sudo / root umount /mnt/data $ lsblk -f sudo / root nano /etc/fstab sudo / root mount -a sudo / root rsync -avz src/ dest/ sudo / root rsync -av --delete src/ dest/
|
9.1 LVM(简要)
1 2 3 4
| 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
|
10. 日志与故障排查
1 2 3 4 5 6 7 8 9 10 11
| sudo / root journalctl -xe sudo / root journalctl --since "2025-11-01" $ tail -f /var/log/syslog $ tail -f /var/log/auth.log $ less /var/log/nginx/error.log sudo / root strace -p PID sudo / root lsof -i :80 sudo / root lsof /path/file $ dmesg -T | grep -i error $ last -n 10 $ lastb -n 10
|
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"
|
延伸阅读
建议将本文保存到团队 Wiki 或打印成小册,方便在服务器环境下快速查阅。