Linux禁用SSH弱加密算法的核心是显式声明强算法白名单,而非简单禁用:需在sshd_config中顶格配置Ciphers、Macs、KexAlgorithms三类强算法,同步设置Protocol 2、停用RSA主机密钥、PasswordAuthentication no及PermitRootLogin no,并经sshd -t校验后systemctl reload sshd生效。
Linux 上禁用 SSH 弱加密算法,核心不是“禁止使用”,而是显式声明只允许强算法——OpenSSH 会严格按你写的列表协商,未列出的全部被屏蔽。这比删配置、加黑名单更可靠,也符合 NIST SP 800-131A 和 OpenSSH 最新推荐逻辑。
别直接抄网上的配置,先确认你的 OpenSSH 版本(sshd -V)是否真支持那些算法,否则改完可能连不上:
ssh -Q kex | grep -E "(group1|group14|sha1)" —— 出现的都要禁用ssh -Q cipher | grep -E "(cbc|arcfour|3des|blowfish|cast128)" —— 这些全属淘汰项ssh -Q mac | grep -E "(md5|sha1)" —— 只保留带 [email protected] 后缀的(如 [email protected])用 root 编辑 /etc/ssh/sshd_config,在文件末尾顶格添加(不能缩进、不能空格、逗号后不加空格)以下三行:
Ciphers [email protected],[email protected],[email protected],aes256-ctr,aes192-ctr,aes128-ctrMacs [email protected],[email protected],[email protected]KexAlgorithms curve25519-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256注意:如果文件里已有 Ciphers、Macs 或 KexAlgorithms 行,必须删除旧的,只保留这一组新配置。
单改算法不够,攻击者可能绕过:
Protocol 2(删掉或注释所有含 Protocol 1 的行)HostKey /etc/ssh/ssh_host_rsa_key 行,只保留 ssh_host_ed25519_key 和 ssh_host_ecdsa_key;若无 ed25519 密钥,运行 ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" 生成PasswordAuthentication no(前提是已配好密钥登录)PermitRootLogin no
改完别急着重启,分步验证:
sudo sshd -t —— 无任何输出表示配置合法sudo systemctl reload sshd(不中断现有连接,比 restart 更稳妥)ssh -vv user@localhost,搜索日志中 kex:、cipher:、mac: 行,确认出现的全是上面配置的算法,且没有 cbc、sha1、group1 等字样不复杂但容易忽略。