我们使用ssh登录一些系统是可能因为算法不匹配而失败,本人以前多次遇到都是临时解决,最近系统的了解分享给读者。
SSH是一种协商的过程,客户端和服务器都会提供一系列支持的密钥交换算法,然后选择一个最强的共同算法来建立加密通道。如果客户端和服务器没有任何共同的算法,那么就无法协商,也就无法连接。有时候匹配不上的原因可能是:
- 你的SSH客户端或服务器的版本太旧,不支持一些新的算法。
- 你的SSH客户端或服务器的配置太严格,限制了一些算法的使用。
- 你的SSH客户端或服务器的配置有误,导致一些算法被禁用或忽略。
主动指定参数可以让你的SSH客户端在连接时提供或排除一些算法,从而增加匹配的可能性。但这也可能降低安全性,因为一些算法可能已经被认为是不安全的或过时的。所以最好的办法是升级你的SSH客户端和服务器,让它们支持更多更安全的算法。
参考:
(1) ssh unable to negotiate - no matching key exchange method found. https://unix.stackexchange.com/questions/402746/ssh-unable-to-negotiate-no-matching-key-exchange-method-found.
(2) Technical Tip: SSH key exchange troubleshooting - Fortinet. https://community.fortinet.com/t5/FortiGate/Technical-Tip-SSH-key-exchange-troubleshooting/ta-p/194388.
(3) Changing the Key Exchange Algorithm used by OpenSSH Server. https://unix.stackexchange.com/questions/685545/changing-the-key-exchange-algorithm-used-by-openssh-server.
要查看默认使用的算法和所有可支持的算法,有几种方法:
- 你可以用ssh -Q参数来列出你的SSH客户端支持的算法,比如ssh -Q cipher列出支持的加密算法,ssh -Q mac列出支持的MAC算法,ssh -Q kex列出支持的密钥交换算法。
- 你可以用ssh -vv参数来连接一个SSH服务器,然后在输出中查看客户端和服务器提供的算法列表,比如local client KEXINIT proposal是客户端提供的密钥交换算法列表,peer server KEXINIT proposal是服务器提供的密钥交换算法列表²。
- 你可以用nmap --script ssh2-enum-algos命令来扫描一个SSH服务器,然后查看它支持的算法列表。
参考:
(1) ssh - How can I find a list of MACs, Ciphers, and KexAlgorithms that my .... https://superuser.com/questions/868998/how-can-i-find-a-list-of-macs-ciphers-and-kexalgorithms-that-my-openssh-client.
(2) ssh client to show server-supported algorithms - Stack Overflow. https://stackoverflow.com/questions/59506608/ssh-client-to-show-server-supported-algorithms.
(3) How can I list MACs, Ciphers and KexAlogrithms supported by my ssh server?. https://serverfault.com/questions/735176/how-can-i-list-macs-ciphers-and-kexalogrithms-supported-by-my-ssh-server.
(4) ssh - OpenSSH default/preferred ciphers, hash, etc for SSH2 .... https://security.stackexchange.com/questions/25662/openssh-default-preferred-ciphers-hash-etc-for-ssh2.
指定算法:
可以在你的SSH客户端的配置文件中指定你想要支持的算法列表,或者在SSH命令中用-oKexAlgorithms参数指定。但是这样做可能会降低你的安全性,因为一些算法可能有已知的漏洞或弱点。我建议你只使用那些被认为是安全和有效的算法,或者升级你的SSH客户端和服务器,让它们自动选择最佳的算法。
-oKexAlgorithms是一个SSH命令的选项,用于指定要使用的密钥交换算法。密钥交换算法是用于在SSH连接中建立共享密钥的方法。你可以用逗号分隔多个算法,SSH客户端会按照顺序提供给远程主机。如果你在算法前面加上+号,表示在默认的算法列表后面追加你指定的算法。如果你在算法前面加上-号,表示从默认的算法列表中删除你指定的算法。
比如:远程主机只支持diffie-hellman-group14-sha1这个算法,而你的SSH客户端默认不支持。你可以尝试以下方法解决这个问题:
- 在你的~/.ssh/config文件中添加一行:KexAlgorithms +diffie-hellman-group14-sha1,这样你的SSH客户端就会在连接时提供这个算法。
- 或者在你的SSH命令中添加一个参数:-oKexAlgorithms=+diffie-hellman-group14-sha1,这样你就可以临时使用这个算法。
参考:
(1) ssh unable to negotiate - no matching key exchange method found. https://unix.stackexchange.com/questions/402746/ssh-unable-to-negotiate-no-matching-key-exchange-method-found.
(2) ssh - Issue with cloning git repository - Stack Overflow. https://stackoverflow.com/questions/33910364/issue-with-cloning-git-repository.
(3) no matching key exchange method found. Their offer: diffie-hellman .... https://community.cisco.com/t5/network-management/no-matching-key-exchange-method-found-their-offer-diffie-hellman/td-p/4410107.
其他相关的ssh参数:
-q: 用于抑制大部分警告和诊断信息的输出
-o StrictHostKeyChecking=no: 用于跳过主机密钥检查,不会提示是否接受新的主机密钥