Unable to negotiate with 127.0.0.1 port 8226: no matching host key type found.

554 阅读1分钟

遇到了问题

MacOS 从 12.6 升级到 13.2 之后,拉取代码发现远程仓库无法连接,并报错如下:

➜ git pull --rebase
Unable to negotiate with 127.0.0.1 port 8226: no matching host key type found. Their offer: ssh-rsa,ssh-dss
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

出现问题的原因:OpenSSH 更新嘞,默认关闭了 ssh-rsa 算法。

出现问题的原因

MacOS13 使用的 OpenSSH 客户端为 9.0p1:

➜ ssh -V                                    
OpenSSH_9.0p1, LibreSSL 3.3.6

OpenSSH 8.7 版本开始宣布禁用 ssh-rsa 加密签名,因为 ssh-rsa 使用的 sha1 算法现在已不够安全了:

image.png

所以导致使用 $ ssh-keygen -t rsa -C "your_email@example.com" 生成的密钥无法使用。

解决办法

正常解决办法就是使用更安全加密算法来生成密钥:

$ ssh-keygen -t ed25519 -C "your_email@example.com"

如果系统不存在 Ed25519 算法,也可以使用 4096 算法:

$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

最后在服务端把老的 SSH Public Key 删除,添加新的解决了。

如果你只想使用 ssh-rsa 算法,或者像我司的 Gerrit 服务器只支持 DSS 和 RSA 算法——DSS 已经被主流代码仓库平台禁用,RSA 基本上也不支持(比如 GitHub 在 2021.11.2 就不再支持老的 RSA 算法)。

可以通过配置文件,强行指定加密类型使用 ssh-rsa,在 ~/.ssh 目录下,新建 config 文件:

Host internal.cardopt.com
    HostKeyAlgorithms +ssh-rsa
    PubkeyAcceptedKeyTypes +ssh-rsa

注意 Host 最好不要设置成全部匹配,这样会影响本机和其他的仓库的连接。