git--no matching host key type found. Their offer: ssh-rsa

4,377 阅读3分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

问题

windows下使用git bash拉代码报错如下

Unable to negotiate with 192.168.133.58 port 29418: no matching host key type found. Their offer: ssh-rsa
fatal: Could not read from remote repository.

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

解决方法

在用户目录下的 .ssh文件夹下新建一个 config 文件,添加:

Host *
HostkeyAlgorithms +ssh-rsa
PubkeyAcceptedKeyTypes +ssh-rsa

原因

最近的 openssh 版本默认弃用了 DSA 密钥,仅依靠 DSA 并不是一个好主意。

作为一种解决方法,如旧版使用的官方文档中所述,我建议将这些行添加到~/.ssh/config文件中:

Host your-remote-host
HostkeyAlgorithms +ssh-dss

其他可能性是使用环境变量GIT_SSH来指定这些选项:

GIT_SSH_COMMAND="ssh -oHostKeyAlgorithms=+ssh-dss" git clone ssh://user@host/path-to-repository

如果.ssh 目录中没有这样的文件,则可以使用名为“config”的空文本文件 还可以在 ssh 行中添加-oHostKeyAlgorithms=+ssh-dss

ssh -oHostKeyAlgorithms=+ssh-dss user@host

扩展

如果想将此安全漏洞包含在单个存储库中,可以通过在这些存储库中运行此命令来将配置选项添加到需要此功能的任何 Git 存储库。(注意:仅适用于 git 版本 >= 2.10,发布于 2016-09-04)

git config core.sshCommand 'ssh -oHostKeyAlgorithms=+ssh-dss'

但是,这仅在设置存储库后才有效。如果您不习惯手动添加遥控器(并且只想克隆),那么您可以像这样运行克隆:

GIT_SSH_COMMAND='ssh -oHostKeyAlgorithms=+ssh-dss' git clone ssh://user@host/path-to-repository

然后运行第一个命令使其永久化。

如果没有最新的 Git,但仍想尽可能地保留漏洞,我建议这样:

export GIT_SSH_COMMAND='ssh -oHostKeyAlgorithms=+ssh-dss'

在某处的文件中,比如说git_ssh_allow_dsa_keys.sh,并source在需要时对其进行 ing。

验证 /etc/ssh/ 中有主机密钥。如果不生成新的,请参阅man ssh-keygen

$ ll /etc/ssh/
total 580
-rw-r--r--. 1 root root     553185 Mar  3  2017 moduli
-rw-r--r--. 1 root root       1874 Mar  3  2017 ssh_config
drwxr-xr-x. 2 root root       4096 Apr 17 17:56 ssh_config.d
-rw-------. 1 root root       3887 Mar  3  2017 sshd_config
-rw-r-----. 1 root ssh_keys    227 Aug 30 15:33 ssh_host_ecdsa_key
-rw-r--r--. 1 root root        162 Aug 30 15:33 ssh_host_ecdsa_key.pub
-rw-r-----. 1 root ssh_keys    387 Aug 30 15:33 ssh_host_ed25519_key
-rw-r--r--. 1 root root         82 Aug 30 15:33 ssh_host_ed25519_key.pub
-rw-r-----. 1 root ssh_keys   1675 Aug 30 15:33 ssh_host_rsa_key
-rw-r--r--. 1 root root        382 Aug 30 15:33 ssh_host_rsa_key.pub

在 /etc/ssh/sshd_config 中验证 HostKey 配置。它应该允许配置 RSA 和 ECDSA。(如果默认情况下所有这些都被注释它也将允许 RSA,请参阅man sshd_configHostKey 部分)。

# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key

对于客户端,只需执行以下操作即可为 ssh(不是问题中的 DSA)创建一个密钥:

ssh-keygen

在此之后,因为有比 ssh-dss(DSA) 更多的选项,所以客户端 openssh (>=v7) 应该使用 RSA 或更好的算法连接。