OpenSSH client (8.8+) connecting to a server that only offers legacy host key algorithmsSSH
错误信息
Unable to negotiate with 127.0.0.1 port 22: no matching host key type found. Their offer: ssh-rsa
也可能输出为
- no matching host key type found. Their offer: ssh-rsa,ssh-dss
- Unable to negotiate with 127.0.0.1 port 22: no matching key exchange method found. Their offer: diffie-hellman-group14-sha1
主机和端口以及服务器提供的算法会有所不同。密钥交换的提示为“no matching key exchange method found”,加密算法的提示为“no matching cipher found”。
含义
客户端和服务器没有共同的主机密钥算法,因此连接在算法协商阶段就停止了——早于主机验证和登录。“Their offer”列出的是服务器提出的算法。OpenSSH 8.8 及以上版本默认禁用 `ssh-rsa`(使用 SHA-1 的 RSA)和 `ssh-dss`(DSA);只提供这些算法的服务器通常是老旧的专用设备、路由器、NAS 设备或未打补丁的系统。密钥交换方面的变体(“no matching key exchange method found”)形式相同,解决办法也相同。
原因
老旧的嵌入式 SSH 服务器
常见交换机、防火墙、打印机和存储设备上运行的常常是 dropbear 或旧版 OpenSSH,只有 RSA/SHA-1 主机密钥。
没有现代主机密钥的服务器
偶见很久以前安装的系统可能只有 RSA 主机密钥,而且 sshd 版本太旧,无法用 `rsa-sha2-256/512` 签名。
客户端配置限制过严
偶见`~/.ssh/config` 或 `/etc/ssh/ssh_config.d` 中只列出 Ed25519 的 `HostKeyAlgorithms` 行,会拒绝只有 ECDSA 或 RSA 密钥的服务器。
修复方法
- 1.
只对该主机允许旧算法
用 `Host` 块把 SHA-1 限制在需要它的设备上。切勿把 `+ssh-rsa` 放在 `Host *` 下。
text # ~/.ssh/config - re-enable the legacy algorithm for that one device only Host old-switch.lan HostKeyAlgorithms +ssh-rsa PubkeyAcceptedAlgorithms +ssh-rsa - 2.
为服务器配置现代主机密钥
更新设备的固件或 OpenSSH,并生成 Ed25519/ECDSA 主机密钥,就不再需要例外配置。
bash # On the server: generate modern host keys and let sshd offer them ssh-keygen -A # creates missing ed25519/ecdsa host keys sshd -T | grep -E "^hostkeyalgorithms|^hostkey " # what the server will offer