克隆git repo会导致错误 - 主机密钥验证失败.致命:远程端意外挂断

Ale*_*ham 46 git ssh-keys

我使用SSH将git repo克隆到我的Web服务器,但每次出现此错误

$git clone git@github.com:aleccunningham/marjoram.git
Cloning into marjoram...
Host key verification failed.
Run Code Online (Sandbox Code Playgroud)

我已经尝试了几乎所有在谷歌搜索中出现的内容,而且我对为什么这不起作用感到茫然.有任何想法吗?

另外,我没有使用詹金斯这样的东西.

小智 148

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

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

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

  • 这在自动化系统设置时非常有用,因为它允许您强制TOFU(首次使用时信任)设置(通常是交互式).修复客户端公钥在这种情况下什么也不做,因为它不是问题. (4认同)
  • 我们怎样才能在Windows中执行此操作? (2认同)

Dev*_*van 15

解决了问题...您需要将ssh公钥添加到您的github帐户.

  1. 验证是否已正确设置ssh密钥.
    1. ssh-keygen
    2. 输入密码(保留默认路径 - ~/.ssh/id_rsa)
  2. 公钥(~/.ssh/id_rsa.pub)添加到github帐户
  3. 试试git clone.有用!


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

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:~$
Run Code Online (Sandbox Code Playgroud)


现在,将公钥添加~/.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)
Run Code Online (Sandbox Code Playgroud)

  • 事实证明我有一个更容易的问题.首先,使用repo工作的HTTPS链接,当我不应该将它指向/ home/public时,我也试图将其克隆到/ home/private.终于有了这样的解脱! (3认同)
  • 客户端上的密钥(ssh-keygen等)不是"主机密钥验证失败"的原因.对于将来阅读此内容的其他人来说,这实际上是评论中@ mooshe的后续问题的答案.Tupy在下面的回答在技术上是对所提问题的正确答案. (3认同)