Linux 生产服务器 SSH 与 nftables 防火墙加固清单:上线前必做的 10 项基线配置
核心提要:新开的公网 Linux 云主机往往在 5 分钟内就会遭遇全球肉鸡扫描与 SSH 暴力破解。本文提供一套直接可抄的 SSH Ed25519 加固与 nftables 最小化放行脚本。
任何暴露在公网的 Linux 主机,安全基线必须在部署业务前的第一小时内完成收敛。以下清单兼顾高安全性与可维护性,适用于 Ubuntu、Debian 与 CentOS/Rocky 全系列发行版。
一、SSH 服务核心安全配置(/etc/ssh/sshd_config)
# 1. 仅允许 Ed25519 等现代公钥认证,彻底关闭密码暴力破解面
PubkeyAuthentication yes
PasswordAuthentication no
ChallengeResponseAuthentication no
PermitEmptyPasswords no
# 2. 限制认证尝试次数与登录宽限时间
MaxAuthTries 3
LoginGraceTime 30
# 3. 如无跳板转发需求,关闭端口转发与 X11
AllowTcpForwarding no
X11Forwarding no
修改后务必先执行 sshd -t 验证语法,且保持当前 SSH 会话不断开,另开一个新终端验证公钥登录成功后再退出原窗口,防止将自己锁在门外。
二、现代 Linux 防火墙 nftables 极简白名单骨架
table inet filter {
chain input {
type filter hook input priority 0; policy drop;
iifname "lo" accept
ct state established,related accept
ip protocol icmp accept
ip6 nexthdr ipv6-icmp accept
tcp dport { 80, 443 } accept
udp dport 443 accept
# SSH 仅放行运维跳板或 WireGuard 内网段
ip saddr 10.66.0.0/24 tcp dport 22 accept
}
}