67

I am using SSH to clone a git repo to my web server, but every time I get this error

$git clone git@github.com:aleccunningham/marjoram.git
Cloning into marjoram...
Host key verification failed.

I have tried almost everything that has shown up in Google searches, and I am dumbfounded on why this will not work. Any ideas?

Also, I am not using anything like Jenkins.

Sebastian
  • 7,670
  • 5
  • 38
  • 50
Alec Cunningham
  • 803
  • 1
  • 7
  • 10
  • 1
    So what exactly have you tried? What happens when you run `ssh -T git@github.com`? – Sebastian Mar 05 '13 at 03:13
  • 1
    Assuming your [ssh keys](https://help.github.com/articles/generating-ssh-keys) are properly setup, try running the command again after removing the `known_hosts` at `$HOME/.ssh/known_hosts`. Take a backup of the file before deleting the file. – Devendra D. Chavan Mar 05 '13 at 03:20
  • @Sebastian If I run that, I get "Permission Denied (publickey). I have tried creating a SSH key on github, tried clearing the keys on the server so it generates new ones, making a new one and adding it to the server, and most else. – Alec Cunningham Mar 05 '13 at 03:21
  • @DevendraD.Chavan Where do I find known_hosts on my server? $HOME/.ssh/known_hosts returns no such file or directory – Alec Cunningham Mar 05 '13 at 03:24
  • 2
    The `known_hosts` file is created only when you accept the host while ssh to it. – Devendra D. Chavan Mar 05 '13 at 03:33
  • @mooshe "Permission Denied" means there is something wrong with your ssh key setup. "Host key verification failed." means, there is something wrong with the host key (e.g. it is not in your `known_hosts` file) – Sebastian Mar 05 '13 at 03:35
  • This might be of some [Error: Permission denied (publickey)](https://help.github.com/articles/error-permission-denied-publickey) – Devendra D. Chavan Mar 05 '13 at 03:43
  • I am able to replicate the error on a VM. Not sure why it is not working? It works on my host! `abc@vi1-136:~$ git clone git@github.com:aleccunningham/marjoram.git Cloning into 'marjoram'... Permission denied (publickey). fatal: The remote end hung up unexpectedly`. `ssh -vT git@github.com ` does not work either. – Devendra D. Chavan Mar 05 '13 at 04:09
  • You can work with `git clone https://github.com/aleccunningham/marjoram.git` till the issue is resolved. – Devendra D. Chavan Mar 05 '13 at 04:13
  • Got it working! The reason was quite **trivial**... the public key was not added to the github account! – Devendra D. Chavan Mar 05 '13 at 04:31

4 Answers4

246

The issue could be that Github isn't present in your ~/.ssh/known_hosts file.

Append GitHub to the list of authorized hosts:

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

Alain O'Dea
  • 21,033
  • 1
  • 58
  • 84
Tupy
  • 11,663
  • 4
  • 18
  • 11
  • 4
    This is useful when automating system setup since it allows you to force the TOFU (trust on first use) setup (which is normally interactive). Fixing client-side public keys does nothing in that case because it isn't the problem. – Alain O'Dea Oct 19 '15 at 01:12
  • 2
    How can we do this in windows? – Whitecat Oct 17 '17 at 16:30
  • @Whitecat user proper address after `>>`, like `%userprofile%\.ssh\known_hosts` – TotalAMD Aug 14 '18 at 14:16
  • @Tupy, bingo this did the trick. I ad set up everything correctly otherwise. Thanks. – Jose Quijada Nov 24 '18 at 17:32
  • I had this issue using the windows subsystem for linux, it wouldn't accept without this. – Ken Stipek Apr 17 '19 at 20:38
  • if still doesn't work, try to do this https://stackoverflow.com/questions/25927914/git-error-please-make-sure-you-have-the-correct-access-rights-and-the-reposito in my case it works – rickvian May 16 '20 at 07:46
23

Resolved the issue... you need to add the ssh public key to your github account.

  1. Verify that the ssh keys have been setup correctly.
    1. Run ssh-keygen
    2. Enter the password (keep the default path - ~/.ssh/id_rsa)
  2. Add the public key (~/.ssh/id_rsa.pub) to github account
  3. Try git clone. It works!


Initial status (public key not added to git hub account)

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:~$


Now, add the public key ~/.ssh/id_rsa.pub to the github account (I used 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)
Devendra D. Chavan
  • 8,871
  • 4
  • 31
  • 35
  • 3
    Turns out I had a much easier problem. For one, using the HTTPS link from the repo worked, and I also was trying to clone it to /home/private when I shouldn't been directing it to /home/public. Such a relief when it finally worked! – Alec Cunningham Mar 06 '13 at 15:28
  • 4
    Keys on the client (ssh-keygen, etc.) are not the cause of "Host key verification failed.". For anyone else reading this in the future, this is actually the answer to @mooshe's followup questions in the comments. Tupy's answer below is technically the correct answer to the question as posed. – Alain O'Dea Oct 19 '15 at 01:01
0

Well, from sourceTree I couldn't resolve this issue but I created sshkey from bash and at least it works from git-bash.

https://confluence.atlassian.com/bitbucket/set-up-an-ssh-key-728138079.html

Smart Coder
  • 1,435
  • 19
  • 19
-2

I had the same issue, and the solution is very simple, just change to git bash from cmd or other windows command line tools. Windows sometimes does not work well with git npm dependencies.

shaosh
  • 657
  • 2
  • 8
  • 30