Debian系统常用命令

整理 Debian 常用的系统更新、网络、SSH、防火墙、DNS、Swap、systemd 与排障命令,并标注高风险操作。

本文是一份面向 Debian 服务器的日常命令速查表,覆盖软件管理、文件操作、网络、SSH、防火墙、DNS、Swap、systemd 和常见排障。涉及网络、SSH、防火墙、磁盘或重装系统的命令,执行前请先备份并保留另一条可用的登录会话。

一、目录与文件操作

查看当前目录和文件

pwd
ls -lah

进入与返回目录

cd /path/to/directory
cd ..
cd -
  • cd ..:返回上一级目录。
  • cd -:返回刚才所在的目录。

查找命令所在位置

command -v curl
which curl

清屏

clear

解压常见归档文件

# .tar.gz / .tgz
mkdir -p /path/to/output
tar -xzvf archive.tar.gz -C /path/to/output

# .tar
mkdir -p /path/to/output
tar -xvf archive.tar -C /path/to/output

解压未知来源的压缩包前,建议先查看文件列表:

tar -tzf archive.tar.gz

二、软件包管理

更新软件源和系统

apt update
apt upgrade -y

安装常用工具

apt install -y curl wget socat htop dnsutils iproute2

安装单个软件包

apt install -y curl
apt install -y socat
apt install -y wget

查看软件是否已安装

dpkg -l | grep '<软件名>'
apt list --installed 2>/dev/null | grep '<软件名>'

三、程序前台与后台运行

前台运行

chmod +x ./程序名
./程序名

临时放到后台

./程序名 &

仅使用 & 时,退出SSH后程序可能终止。需要长期运行时,优先创建systemd服务;临时任务可使用:

nohup ./程序名 > program.log 2>&1 &

查看后台任务

jobs -l
ps aux | grep '[程]序名'

四、系统状态与重启

查看系统信息

uname -a
cat /etc/os-release
uptime
free -h
df -h

重启系统

systemctl reboot

执行后SSH会立即断开,请确认业务和文件已经保存。

五、BBR拥塞控制

先确认内核支持BBR:

sysctl net.ipv4.tcp_available_congestion_control

使用独立配置文件,避免反复向 /etc/sysctl.conf 追加重复内容:

cat >/etc/sysctl.d/99-bbr.conf <<'EOF'
net.core.default_qdisc=fq
net.ipv4.tcp_congestion_control=bbr
EOF

sysctl --system

检查是否生效:

sysctl net.core.default_qdisc
sysctl net.ipv4.tcp_congestion_control
lsmod | grep tcp_bbr

预期拥塞控制输出为:

net.ipv4.tcp_congestion_control = bbr

部分内核会将BBR直接编译进内核,此时 lsmod 没有结果也不代表BBR未生效,应以 sysctl 输出为准。

六、端口、进程与网络接口

查看监听端口

ss -tulpn

只查看指定端口:

ss -lntup | grep ':5022 '

老系统也可能使用:

netstat -tulpn

如果没有 netstat:

apt install -y net-tools

查看网卡和地址

ip link
ip addr
ip route

临时添加IP地址

ip addr add <IP地址/前缀长度> dev eth0

例如:

ip addr add 192.0.2.10/24 dev eth0

该设置重启后会丢失。持久化配置应写入服务器实际使用的网络管理系统,不能在未确认网卡名和网关时直接照抄。

七、Debian网络接口配置

传统 ifupdown 配置文件:

/etc/network/interfaces

DHCP示例:

auto eth0
iface eth0 inet dhcp

修改前先备份:

cp -a /etc/network/interfaces /etc/network/interfaces.bak.$(date +%F-%H%M%S)

检查无误后重启网络服务:

systemctl restart networking

远程服务器重启网络可能导致SSH断开。操作前应准备VNC、串口或云厂商控制台。

八、SSH配置与安全重载

SSH服务端配置文件:

/etc/ssh/sshd_config

修改前备份并检查语法:

cp -a /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$(date +%F-%H%M%S)
sshd -t

语法正确后优先平滑重载:

systemctl reload ssh
systemctl status ssh --no-pager

Debian服务名通常是 ssh,不是 sshd。修改端口前应先放行新端口,并保留当前SSH连接验证新连接成功。

九、UFW防火墙

安装和查看状态

apt update
apt install -y ufw
ufw status verbose

先放行SSH,再启用防火墙

ufw allow 5022/tcp
ufw enable
ufw status numbered

开放或拒绝端口

# 开放TCP端口
ufw allow 5022/tcp

# 删除对应的开放规则
ufw delete allow 5022/tcp

# 明确拒绝端口
ufw deny 5022/tcp

不要在远程连接依赖5022端口时直接执行 ufw deny 5022,否则可能把自己锁在服务器外。

只允许指定来源访问DNS

ufw allow from <可信来源IP> to any port 53 proto tcp
ufw allow from <可信来源IP> to any port 53 proto udp
ufw deny in 53

规则顺序很重要。执行后检查:

ufw status numbered

十、IPv6启用与禁用

临时检查IPv6状态

sysctl net.ipv6.conf.all.disable_ipv6
ip -6 addr

禁用IPv6

cat >/etc/sysctl.d/99-disable-ipv6.conf <<'EOF'
net.ipv6.conf.all.disable_ipv6=1
net.ipv6.conf.default.disable_ipv6=1
EOF

sysctl -p /etc/sysctl.d/99-disable-ipv6.conf

恢复IPv6

rm -f /etc/sysctl.d/99-disable-ipv6.conf
sysctl -w net.ipv6.conf.all.disable_ipv6=0
sysctl -w net.ipv6.conf.default.disable_ipv6=0

如果云平台、网卡配置或引导参数同时禁用了IPv6,还需要逐层恢复。不要直接执行来源不明的“一键恢复IPv6”脚本。

十一、DNS配置与检查

检查DNS解析

getent hosts example.com
nslookup example.com

安装 nslookup:

apt install -y dnsutils

使用本机DNS服务

只有在 127.0.0.1:53 已有正常工作的DNS服务时,才能这样配置:

cp -a /etc/resolv.conf /etc/resolv.conf.bak.$(date +%F-%H%M%S)
printf 'nameserver 127.0.0.1\n' >/etc/resolv.conf

确认本地53端口确实监听:

ss -lunpt | grep ':53 '

锁定文件会阻止DHCP、systemd-resolved或NetworkManager更新DNS,谨慎使用:

chattr +i /etc/resolv.conf

需要修改时先解锁:

chattr -i /etc/resolv.conf

安装和管理dnsmasq

apt update
apt install -y dnsmasq
systemctl enable --now dnsmasq
systemctl restart dnsmasq
systemctl status dnsmasq --no-pager

如果启动失败,优先检查53端口冲突:

ss -lunpt | grep ':53 '
journalctl -u dnsmasq -n 100 --no-pager

安装AdGuard Home服务

进入AdGuard Home程序目录后执行:

sudo ./AdGuardHome -s install

常用管理命令:

systemctl start AdGuardHome
systemctl stop AdGuardHome
systemctl restart AdGuardHome
systemctl status AdGuardHome --no-pager

重启SNI Proxy

systemctl restart sniproxy
systemctl status sniproxy --no-pager

十二、添加Swap

创建1GiB Swap文件:

fallocate -l 1G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
swapon --show

确认 /etc/fstab 中没有重复项,再设置开机挂载:

grep -q '^/swapfile ' /etc/fstab || printf '/swapfile none swap sw 0 0\n' >>/etc/fstab

检查:

free -h
swapon --show

十三、systemd服务管理

通用命令:

systemctl start <服务名>
systemctl stop <服务名>
systemctl restart <服务名>
systemctl reload <服务名>
systemctl enable <服务名>
systemctl disable <服务名>
systemctl status <服务名> --no-pager

启用并立即启动:

systemctl enable --now <服务名>

查看服务日志:

journalctl -u <服务名> -n 100 --no-pager
journalctl -u <服务名> -f

十四、磁盘与日志排障

查看根目录各目录占用

du -xhd1 / 2>/dev/null | sort -h

查看磁盘和inode

df -h
df -ih

查看失败服务和内核日志

systemctl --failed
journalctl -p err -n 100 --no-pager
dmesg -T | tail -n 100

关于禁用rsyslog

一般不建议为了节省资源直接禁用系统日志,否则故障发生后可能没有证据。确实使用journald替代并确认日志持久化后,才考虑:

systemctl stop rsyslog
systemctl disable rsyslog

恢复:

systemctl enable --now rsyslog

十五、第三方脚本的安全执行方式

不要直接执行未经检查的:

bash <(curl ...)
bash <(wget ...)

更安全的做法是先下载、检查来源和内容,再运行:

curl -fL 'https://example.com/script.sh' -o /tmp/script.sh
sha256sum /tmp/script.sh
less /tmp/script.sh
chmod 700 /tmp/script.sh
bash /tmp/script.sh

短链接、HTTP明文下载、“破解”脚本、证书绕过口令和来源不明的内核优化脚本不应直接用于生产服务器。

十六、重装系统前的注意事项

网络重装会清空或覆盖系统,执行前必须备份数据,并确认云厂商救援控制台可用。密码、SSH端口和系统版本应使用占位参数,不要把真实凭据写进公开文章或Shell历史:

bash InstallNET.sh \
  -d <Debian版本> \
  -v 64 \
  -p '<强随机密码>' \
  -port '<SSH端口>'

建议优先使用云厂商官方重装功能,并使用SSH密钥登录。

十七、特定软件卸载示例

删除软件前先确认目录和服务确实属于目标程序。

卸载NyanPass

systemctl disable --now nyanpass
rm -rf /opt/nyanpass
rm -f /etc/systemd/system/nyanpass.service
systemctl daemon-reload

如果程序自带卸载脚本,优先使用:

bash /opt/nyanpass.uninstall.sh

卸载哪吒监控Agent

cd /opt/nezha/agent/
./nezha-agent service uninstall

删除程序目录

以下操作不可恢复,执行前务必核对路径:

rm -rf /etc/xiandan

十八、OpenWrt / iStoreOS附录

以下命令用于OpenWrt/iStoreOS,不适用于标准Debian:

opkg update
opkg install htop
opkg install /tmp/example.ipk

重启dnsmasq和防火墙:

/etc/init.d/dnsmasq restart
/etc/init.d/firewall restart

十九、macOS充电信息附录

以下命令仅适用于macOS:

system_profiler SPPowerDataType | grep 'Wattage' -C 5

二十、最后的安全检查

修改网络、DNS、SSH或防火墙后,建议依次检查:

ip addr
ip route
ss -tulpn
ufw status verbose
sshd -t
systemctl --failed

不要关闭当前SSH会话,先从新的终端窗口确认可以重新连接,再退出旧会话。