显式配置强算法可强制启用安全SSH——需先用ssh -Q命令查本机支持项,再在sshd_config中顶格写入Ciphers、MACs、KexAlgorithms三类强算法,同步关闭SSHv1、RSA主机密钥、密码登录和root直连,最后用sshd -t检测并systemctl reload sshd重载服务。

直接在 /etc/ssh/sshd_config 中显式写入强算法列表,就能强制启用安全配置——OpenSSH 不继承默认值,只认你明确列出的项。
查清本机支持的算法
别照搬网上配置,先运行命令确认当前 OpenSSH 编译启用的选项:
-
加密算法:运行
ssh -Q cipher,过滤掉含cbc、arcfour、3des、blowfish、cast128的条目 -
消息认证码(MAC):运行
ssh -Q mac,跳过所有带md5或sha1且无etm@openssh.com后缀的项(例如hmac-sha1不安全,hmac-sha2-256-etm@openssh.com安全) -
密钥交换(KEX):运行
ssh -Q kex,剔除group1-sha1、group14-sha1、diffie-hellman-group1-sha1等弱方式
覆盖核心三类算法配置
用 root 权限编辑 /etc/ssh/sshd_config,删除原有 Ciphers、MACs、KexAlgorithms 行,在文件末尾顶格添加(每行不能缩进、不能换行、逗号后不加空格):
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctrMACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,umac-128-etm@openssh.comKexAlgorithms curve25519-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256
同步关闭高风险配套功能
仅改算法不够,还需切断其他攻击入口:
- 确保只用 SSHv2:
Protocol 2(删掉或注释所有含Protocol 1的行) - 停用 RSA 主机密钥:注释或删除所有
HostKey /etc/ssh/ssh_host_rsa_key行,只保留ed25519和ecdsa类型密钥 - 关闭密码登录(若已配好密钥):
PasswordAuthentication no - 禁止 root 直连:
PermitRootLogin no(如需密钥登录 root,可用prohibit-password)
验证并安全重载服务
保存后按顺序操作,避免中断连接:
- 语法检测:
sudo sshd -t—— 无输出即通过;有报错必须修正 - 重载服务:
sudo systemctl reload sshd(不杀现有会话,比 restart 更稳妥) - 新开终端测试:
ssh -vv user@localhost,查看日志中kex:、cipher:行,确认协商的是你设定的算法











