旧版 SSH 客户端连上新版服务端时,密钥交换、主机密钥类型和加密算法可能对不上;
即便改了 sshd_config,Rocky Linux 9 的加密策略仍可能拦住 SHA-1。
SSH 版本与环境
旧 SSH 客户端版本为:OpenSSH_4.3p2, OpenSSL 1.0.1h-fips 5 Jun 2014。
新 SSH 服务端版本为:OpenSSH_8.7p1, OpenSSL 3.2.2 4 Jun 2024,服务端操作系统为 Rocky Linux 9.6。
服务端报错日志
客户端 SSH 连接主机失败,服务端 secure 日志内容如下:
no matching key exchange method found. Their offer: Diffie-hellman-group1-sha1 [preauth]
no matching host key type found. Their offer: ssh-rsa [preauth]
no matching cipher found. Their offer: aes128-cbc, 3des-cbc, aes192-cbc, aes256-cbc [preauth]
这三个报错是分别出现的,解决了一个后在 SSH 后续协商过程中才会出现另一个。
解决服务端报错
在 sshd_config 文件中添加以下内容解决。
KexAlgorithms +diffie-hellman-group1-sha1
HostKeyAlgorithms +ssh-rsa
Ciphers +aes256-cbc
建议使用 root 用户写到 /etc/ssh/sshd_config.d 下的自定义文件中,权限 644,其优先级高于 sshd_config。
客户端报错
但发现仍然无法连接,服务端的日志中只有连接被客户端关闭的记录,没有新的错误日志了,客户端返回报错:
ssh_rsa_verify: cannot handle type rsa-sha2-256
key_verify failed for server_host_key
解决客户端报错
查询得知,Rocky Linux 9 这一代(OpenSSH 8.7 + OpenSSL 3.2.2),默认加密策略关掉了 SHA-1,
只在 sshd_config 里写 HostKeyAlgorithms +ssh-rsa 不够,SHA-1 仍被策略禁用。
旧客户端协商时只会报 ssh-rsa,服务端这把 RSA 主机密钥签不了 SHA-1,还是使用 rsa-sha2-256 把签名发出去,
而客户端解析不了这个类型于是就主动断开了连接,所以在服务端重新打开 ssh-rsa 就行。
服务端命令行输入:
update-crypto-policies --show
update-crypto-policies --set DEFAULT:SHA1
systemctl restart sshd
DEFAULT:SHA1 会重新允许 RSA-SHA1,加密策略生成的 SSH 配置里会带上 ssh-rsa。
要恢复的话重新把策略配置成 DEFAULT 然后重启 sshd 服务即可,已连上的会话不会被这次重启断开。
如果不是 RHEL 系的系统,可以尝试以下方法(我没有验证过):
在服务端的 /etc/ssh/sshd_config 或 /etc/ssh/sshd_config.d/ 里写成下面这样,然后重启 sshd。
这里不要用 +,否则 rsa-sha2-256 仍会排在前面:
HostKeyAlgorithms ssh-rsa,ssh-ed25519,ecdsa-sha2-nistp256
PubkeyAcceptedAlgorithms +ssh-rsa
总结
AI 好用,多用 AI。