无法与 XX.XXX.XX.XX 协商:找不到匹配的主机密钥类型。他们的报价:ssh-dss

Unable to negotiate with XX.XXX.XX.XX: no matching host key type found. Their offer: ssh-dss

我正在尝试在我的虚拟主机上创建一个 git 存储库并将其克隆到我的计算机上。这是我所做的:

  1. 我在远程服务器上创建了一个存储库。
  2. 我生成了一个密钥对:ssh-keygen -t dsa
  3. 我将我的密钥添加到 ssh-agent。
  4. 我复制到服务器 public 输入 ~/.ssh.

然后,在尝试 运行 命令 git clone ssh://user@host/path-to-repository 后,我得到一个错误:

Unable to negotiate with XX.XXX.XX.XX: no matching host key type found. Their offer: ssh-dss
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.

这是什么意思?

最近的 openssh 版本默认弃用了 DSA 密钥。您应该建议您的 GIT 提供商添加一些合理的主机密钥。只依赖 DSA 不是一个好主意。

作为解决方法,您需要告诉 ssh 客户端您想要接受 DSA 主机密钥,如 official documentation for legacy usage 中所述。您的可能性很小,但我建议将这些行添加到您的 ~/.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 行中添加 -oHostKeyAlgorithms=+ssh-dss

ssh -oHostKeyAlgorithms=+ssh-dss user@host

对我来说这很有效:(添加到 .ssh\config

Host *
HostkeyAlgorithms +ssh-dss
PubkeyAcceptedKeyTypes +ssh-dss

如果您想将此安全漏洞包含在单个存储库中,您可以通过 运行 在这些存储库中使用此命令向任何需要此的 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 编辑它。

你要么按照上面的方法,要么按照这个方法

在 .ssh 目录中创建配置文件并添加这些行。

host xxx.xxx
 Hostname xxx.xxx
 IdentityFile ~/.ssh/id_rsa
 User xxx
 KexAlgorithms +diffie-hellman-group1-sha1

我想与服务器端的解决方案进行一些协作。所以,服务器说它不支持 DSA,这是因为 openssh client does not activate it by default:

OpenSSH 7.0 and greater similarly disable the ssh-dss (DSA) public key algorithm. It too is weak and we recommend against its use.

因此,要在服务器端解决此问题,我应该激活其他密钥算法,如 RSA 或 ECDSA。我刚刚在局域网中的服务器上遇到了这个问题。 我建议如下:

更新openssh:

yum update openssh-server

如果有 sshd_config.rpmnew.

,则在 sshd_config 中合并新配置

验证 /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_config HostKey 部分)。

# 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 或更好的算法连接。

Here another good article.

这是我回答的第一个问题,欢迎提出建议:D .

在我的 bitbucket 案例中,以下方法有效。

Host yourhost(ex: bitbucket.com)
    User git
    PubkeyAcceptedAlgorithms +ssh-rsa
    HostkeyAlgorithms +ssh-rsa