克隆 git 仓库时出现错误 - 主机密钥验证失败。致命错误:远程端意外挂断。

68
我正在使用SSH将git仓库克隆到我的Web服务器,但每次都会出现以下错误。
$git clone git@github.com:aleccunningham/marjoram.git
Cloning into marjoram...
Host key verification failed.

我尝试了几乎所有在Google搜索中出现的方法,但为什么这个不起作用我感到困惑。有什么思路吗?
此外,我没有使用类似Jenkins的工具。

1
你尝试过什么?运行ssh -T git@github.com时发生了什么? - Sebastian
1
假设您的ssh密钥已正确设置,请在删除$HOME/.ssh/known_hosts中的known_hosts后再次尝试运行命令。在删除文件之前备份该文件。 - Devendra D. Chavan
2
known_hosts 文件只有在你通过 ssh 连接到主机并接受该主机时才会被创建。 - Devendra D. Chavan
@mooshe,“Permission Denied”表示您的ssh密钥设置有问题。“Host key verification failed.”则表示主机密钥有问题(例如,它不在您的known_hosts文件中)。 - Sebastian
在问题解决之前,您可以使用 git clone https://github.com/aleccunningham/marjoram.git 进行工作。 - Devendra D. Chavan
显示剩余5条评论
4个回答

247

问题可能是 Github 没有在你的 ~/.ssh/known_hosts 文件中。

将 GitHub 添加到授权主机列表:

ssh-keyscan -H github.com >> ~/.ssh/known_hosts


4
自动化系统设置时,这很有用,因为它允许您强制执行TOFU(首次使用时信任)设置(通常需要交互)。在这种情况下,修复客户端公钥是无效的,因为它并不是问题所在。 - Alain O'Dea
2
我们如何在Windows中实现这个? - Whitecat
>> 后面为 @Whitecat 用户提供正确的地址,例如 %userprofile%\.ssh\known_hosts - TotalAMD
@Tupy,太棒了,这解决了问题。否则我已经正确地设置了一切。谢谢。 - Jose Quijada
我在使用 Windows 的 Linux 子系统时遇到了这个问题,如果没有这个,它就不能接受。 - Ken Stipek
如果仍然无法正常工作,请尝试执行以下操作:https://dev59.com/bV8e5IYBdhLWcg3wPYlA 在我的情况下,它可以正常工作。 - rickvian

23

解决了问题...您需要将ssh公钥添加到您的GitHub账户中。

  1. 验证ssh密钥是否已正确设置。
    1. 运行ssh-keygen
    2. 输入密码(保留默认路径-~/.ssh/id_rsa
  2. 添加公钥(~/.ssh/id_rsa.pub)到github账户
  3. 尝试git clone。它工作了!


初始状态(公钥未添加到GitHub帐户)

foo@bn18-251:~$ rm -rf test
foo@bn18-251:~$ ls
foo@bn18-251:~$ git clone git@github.com:devendra-d-chavan/test.git
Cloning into 'test'...
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
foo@bn18-251:~$
现在,将公钥~/.ssh/id_rsa.pub添加到github帐户中(我使用了cat ~/.ssh/id_rsa.pub命令)。
foo@bn18-251:~$ ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/home/foo/.ssh/id_rsa): 
Created directory '/home/foo/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/foo/.ssh/id_rsa.
Your public key has been saved in /home/foo/.ssh/id_rsa.pub.
The key fingerprint is:
xxxxx
The key's randomart image is:
+--[ RSA 2048]----+
xxxxx
+-----------------+
foo@bn18-251:~$ cat ./.ssh/id_rsa.pub 
xxxxx
foo@bn18-251:~$ git clone git@github.com:devendra-d-chavan/test.git
Cloning into 'test'...
The authenticity of host 'github.com (207.97.227.239)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,207.97.227.239' (RSA) to the list of known hosts.
Enter passphrase for key '/home/foo/.ssh/id_rsa': 
warning: You appear to have cloned an empty repository.
foo@bn18-251:~$ ls
test
foo@bn18-251:~/test$ git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)

3
结果证明我遇到的问题非常简单。首先,使用仓库中的HTTPS链接可以正常工作,并且我尝试将其克隆到/home/private,但应该是指向/home/public。当它最终工作时,我感到非常欣慰! - Alec Cunningham
4
客户端上的密钥(ssh-keygen等)并不是“主机密钥验证失败”的原因。对于未来阅读此评论的其他人而言,这实际上是回答@mooshe在评论中的后续问题。下面Tupy的答案在技术上是对所提出问题的正确答案。 - Alain O'Dea

0

-2

我曾经遇到过同样的问题,解决方法非常简单,只需要从cmd或其他Windows命令行工具切换到git bash即可。Windows有时无法很好地处理git npm依赖项。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接