外观
排查 sshd 配置
看最终生效的值
bash
sshd -T | grep -iE "pubkeyauthentication|passwordauthentication|permitrootlogin|port"sshd -T 输出的是合并所有 include 之后的最终配置。RHEL 9+ 和 Debian 12 都会从 /etc/ssh/sshd_config.d/*.conf 加载额外片段,直接看 sshd_config 主文件容易被骗。
改配置
也放到 sshd_config.d/ 里,不动主文件:
bash
cat > /etc/ssh/sshd_config.d/50-hardening.conf <<'EOF2'
PasswordAuthentication no
PermitRootLogin prohibit-password
PubkeyAuthentication yes
EOF2
sshd -t # 语法检查,没输出就是对的
systemctl reload sshd注意文件名的数字前缀决定加载顺序,同一个选项第一次出现的值生效,后面的会被忽略。RHEL 系自带一个 50-redhat.conf,自己的片段用更小的数字(如 10-)才能覆盖它。
关密码登录前
先确认密钥登录已经能用——开一个新终端测,别关当前这个。锁死了只能走 VNC / 救援模式。
改端口
改了 Port 记得:
- firewalld 放行新端口
- SELinux 登记:
semanage port -a -t ssh_port_t -p tcp 2222 - 云厂商安全组
漏一个就进不去。改完同样新开终端验证。
看谁在登录
bash
journalctl -u sshd -n 100
last -n 20
lastb -n 20 # 失败的尝试公网机器 lastb 通常一片红,这是正常的。装个 fail2ban 或者只允许密钥登录就够了。