一聚教程网:一个值得你收藏的教程网站

热门教程

Linux 如何配置 ssh 服务禁止使用弱加密算法

时间:2026-08-10 16:28:49 编辑:袖梨 来源:一聚教程网

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)是否真支持那些算法,否则改完可能连不上:

  1. 查看可用密钥交换算法:ssh -Q kex | grep -E "(group1|group14|sha1)" —— 出现的都要禁用
  2. 查看可用加密算法:ssh -Q cipher | grep -E "(cbc|arcfour|3des|blowfish|cast128)" —— 这些全属淘汰项
  3. 查看可用 MAC 算法:ssh -Q mac | grep -E "(md5|sha1)" —— 只保留带 [email protected] 后缀的(如 [email protected]

在 sshd_config 中顶格写入三类强算法白名单

用 root 编辑 /etc/ssh/sshd_config,在文件末尾顶格添加(不能缩进、不能空格、逗号后不加空格)以下三行:

  1. Ciphers [email protected],[email protected],[email protected],aes256-ctr,aes192-ctr,aes128-ctr
  2. Macs [email protected],[email protected],[email protected]
  3. KexAlgorithms curve25519-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256

注意:如果文件里已有 CiphersMacsKexAlgorithms 行,必须删除旧的,只保留这一组新配置。

同步关闭配套薄弱入口

单改算法不够,攻击者可能绕过:

  1. 强制协议版本:Protocol 2(删掉或注释所有含 Protocol 1 的行)
  2. 停用弱主机密钥:注释或删除所有 HostKey /etc/ssh/ssh_host_rsa_key 行,只保留 ssh_host_ed25519_keyssh_host_ecdsa_key;若无 ed25519 密钥,运行 ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" 生成
  3. 禁用密码登录:PasswordAuthentication no(前提是已配好密钥登录)
  4. 禁止 root 直连:PermitRootLogin no

验证并安全生效

改完别急着重启,分步验证:

  1. 语法检查:sudo sshd -t —— 无任何输出表示配置合法
  2. 重载服务:sudo systemctl reload sshd(不中断现有连接,比 restart 更稳妥)
  3. 本地测试:ssh -vv user@localhost,搜索日志中 kex:cipher:mac: 行,确认出现的全是上面配置的算法,且没有 cbcsha1group1 等字样

不复杂但容易忽略。

热门栏目