复盘背景
把公网 VPS 跑起来后的第一天,/var/log/auth.log 里几乎每分钟都会出现来自世界各地的 Failed password for root 爆破日志。以前图省事直接照着网上教程改 /etc/ssh/sshd_config,结果踩过两次坑:一次是 Ubuntu 云主机重装后忘了 /etc/ssh/sshd_config.d/50-cloud-init.conf 会覆盖主配置里的 PasswordAuthentication no,以为关掉了密码登录,其实门还敞开着;另一次是在 Ubuntu 24.04 上直接改了 Port 并重启 ssh.service,却没留意 ssh.socket 还在监听 22 端口。
把这篇旧笔记重构成一套完整的上线加固 SOP,目标只有一个:一次配对、零锁死风险、可命令验证。
前置条件与输入参数
- 本地终端:macOS / Linux(OpenSSH 8.0+)或 Windows 11(内置 OpenSSH Client)。
- 目标主机:Ubuntu 22.04 / 24.04 或 Debian 12,具备一个已创建且拥有
sudo权限的非 root 运维账号(示例使用ops)。 - 示例参数:
- 服务端公网地址:
203.0.113.10或bastion.example.com - 自定义 SSH 端口:
22222 - 本地密钥文件:
~/.ssh/id_ed25519_prod
- 服务端公网地址:
操作步骤(SOP)
1. 在本地生成 Ed25519 密钥对并配置 Host 别名
ssh-keygen -t ed25519 -a 64 -C "ops@example.com" -f ~/.ssh/id_ed25519_prodchmod 600 ~/.ssh/id_ed25519_prodchmod 644 ~/.ssh/id_ed25519_prod.pub生成时务必设置高强度 passphrase,避免私钥文件意外泄露后被直接使用。接着在 ~/.ssh/config 中添加别名,减少后续命令拼写失误:
Host prod-web HostName bastion.example.com User ops Port 22 IdentityFile ~/.ssh/id_ed25519_prod IdentitiesOnly yes ServerAliveInterval 60 ServerAliveCountMax 32. 上传公钥并锁定 authorized_keys 权限
ssh-copy-id -i ~/.ssh/id_ed25519_prod.pub -p 22 ops@bastion.example.com如果目标环境未提供 ssh-copy-id(例如部分救援控制台),手动登录服务器后执行:
mkdir -p ~/.ssh && chmod 700 ~/.sshcat >> ~/.ssh/authorized_keys <<'EOF'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExamplePublicKeyMaterialHere ops@example.comEOFchmod 600 ~/.ssh/authorized_keys在禁用密码前,先用新参数显式测试密钥登录:
ssh -o PubkeyAuthentication=yes -o PasswordAuthentication=no -i ~/.ssh/id_ed25519_prod ops@bastion.example.com3. 通过 drop-in 文件写入加固配置(防止被 cloud-init 覆盖)
现代 Debian / Ubuntu 在 /etc/ssh/sshd_config 顶部通过 Include /etc/ssh/sshd_config.d/*.conf 加载子配置,OpenSSH 遵循**先匹配生效(first-match-wins)**原则。因此要把加固文件放在数字更小、优先加载的位置,或直接检查并覆盖 50-cloud-init.conf。
先排查现有覆盖项:
sudo ls -la /etc/ssh/sshd_config.d/sudo grep -rnE "PasswordAuthentication|PermitRootLogin|PubkeyAuthentication" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/创建高优先级配置 /etc/ssh/sshd_config.d/01-hardening.conf:
sudo tee /etc/ssh/sshd_config.d/01-hardening.conf > /dev/null <<'EOF'Port 22222PermitRootLogin noPasswordAuthentication noKbdInteractiveAuthentication noPermitEmptyPasswords noPubkeyAuthentication yesAuthenticationMethods publickeyMaxAuthTries 3LoginGraceTime 30AllowUsers opsX11Forwarding noAllowAgentForwarding noEOF检查语法是否合法:
sudo sshd -t若提示端口或权限语法错误,先修正后再继续,命令无输出即代表语法通过。
4. 防火墙放行新端口并双终端安全重载
在切端口前先放行防火墙,并保持当前已连接的终端窗口不关闭:
sudo ufw allow 22222/tcp comment "SSH custom port"sudo ufw status numbered若是 Ubuntu 24.04 使用 ssh.socket(systemd socket activation),需同步覆盖监听端口或切回 ssh.service:
sudo systemctl status ssh.socketsudo systemctl disable --now ssh.socketsudo systemctl enable --now ssh.servicesudo systemctl reload ssh此时不要断开原终端,另开一个本地终端,把 ~/.ssh/config 中的 Port 改为 22222,然后执行:
ssh prod-web确认新会话成功进入服务器后,再删掉旧的 22 端口防火墙规则并启用 Fail2Ban:
sudo ufw delete allow 22/tcpsudo apt-get update && sudo apt-get install -y fail2bansudo tee /etc/fail2ban/jail.d/sshd.local > /dev/null <<'EOF'[sshd]enabled = trueport = 22222maxretry = 4findtime = 10mbantime = 1hEOFsudo systemctl enable --now fail2ban真实踩坑复盘与排查记录
-
现象:
sshd_config改了PasswordAuthentication no,密码仍然能登录- 定位:执行
sudo sshd -T | grep passwordauthentication发现输出仍是yes。顺着/etc/ssh/sshd_config.d/排查,发现云厂商镜像自带50-cloud-init.conf且在主配置顶部被优先Include。 - 解决:将加固配置命名为
01-hardening.conf抢占 first-match,并用sshd -T核验最终运行时生效值。
- 定位:执行
-
现象:Ubuntu 24.04 修改端口后重启
ssh,22222 端口死活连不上- 定位:用
sudo ss -tulpn | grep ssh发现监听仍在*:22(由 systemdssh.socket托管)。 - 解决:停用
ssh.socket改用传统ssh.service,或通过systemctl edit ssh.socket覆写ListenStream=22222。
- 定位:用
-
现象:公钥已经复制上去,客户端仍回退到密码提示
- 定位:服务端
ssh -vvv日志出现Authentication refused: bad ownership or modes for directory /home/ops/.ssh,或目标机器开启了 SELinux 导致上下文标签错乱。 - 解决:严格执行
chmod 700 ~/.ssh与chmod 600 ~/.ssh/authorized_keys;RHEL/AlmaLinux 系列额外执行restorecon -Rv ~/.ssh。
- 定位:服务端
验收清单
执行以下命令逐项核验,全部满足才算加固完成:
# 1. 验证 sshd 最终生效配置(非仅仅看文件表面)sudo sshd -T | grep -E "port |permitrootlogin|passwordauthentication|pubkeyauthentication|kbdinteractiveauthentication"
# 2. 验证实际监听端口sudo ss -tulpn | grep 22222
# 3. 从本地确认禁止密码回退(应直接报 Permission denied (publickey))ssh -o PubkeyAuthentication=no -o PasswordAuthentication=yes -p 22222 ops@bastion.example.com
# 4. 确认 Fail2Ban jail 正常运转sudo fail2ban-client status sshd预期结果:sshd -T 输出 permitrootlogin no、passwordauthentication no、pubkeyauthentication yes;新端口 22222 处于 LISTEN;禁用公钥测试被服务端直接拒绝;fail2ban-client status sshd 返回激活状态。
延伸阅读
如果这篇文章对你有帮助,欢迎分享给更多人!
部分信息可能已经过时


