linux ssh安全加固的核心是禁用密码登录并强制使用 SSH 密钥对认证。需修改 sshd_config 禁用 PasswordAuthentication、ChallengeResponseAuthentication 和 UsePAM,禁用 root 直连与空密码,淘汰弱 加密 算法,并确保 ed25519 密钥配置到位。

Linux SSH 安全加固的核心不是“禁止弱密码”本身,而是 绕过密码认证环节——直接禁用密码登录,强制使用 SSH 密钥对认证。这是当前最有效、最主流的防护方式,能从根本上杜绝暴力破解和弱密码风险。
禁用密码登录(关键一步)
编辑 SSH 服务 配置文件:
- 运行
sudo nano /etc/ssh/sshd_config - 确保以下三行设置为 no:
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no
保存后执行 sudo systemctl restart sshd 生效。注意:操作前务必确认已成功配置密钥登录并可正常登录,否则可能被锁在服务器外。
禁用 root 直接登录与空密码
即使保留密码认证(不推荐),也必须关闭高危入口:
-
PermitRootLogin no—— 禁止 root 账户直接远程登录 -
PermitEmptyPasswords no—— 明确禁止空密码登录
这两项属于基础防御,配合密钥认证使用效果更佳。
禁用弱加密算法(提升协议层安全)
旧版 SSH 默认启用已被证明不安全的算法,需主动剔除。在 sshd_config 中添加或修改:
KexAlgorithms curve25519-sha256,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com
macs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com
上述配置淘汰了 3DES、AES-128-CBC、MD5/HMAC-MD5、diffie-hellman-group1-sha1 等已知弱算法,适配 OpenSSH 7.5+ 版本。
密钥登录配置要到位
禁用密码的前提是密钥登录必须可靠可用:
- 客户端生成密钥对:
ssh-keygen -t ed25519 -C "your_email@example.com" - 上传公钥到服务器:
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server_ip - 测试新连接:
ssh -i ~/.ssh/id_ed25519 user@server_ip,确认无密码即可登录
建议使用 ed25519 类型密钥,比 RSA 更快更安全;避免使用默认路径以外的私钥时遗漏 -i 参数。