bootsz
bootsz

Reputation: 5931

Git error: "Host Key Verification Failed" when connecting to remote repository

I am trying to connect to a remote Git repository that resides on my web server and clone it to my machine.

I am using the following format for my command:

git clone ssh://[email protected]/repository.git

This has worked fine for most of my team members. Usually after running this command Git will prompt for the user's password, and then run the cloning. However, when running on one of my machines I get the following error:

Host key verification failed.

fatal: Could not read from remote repository.

We are not using SSH keys to connect to this repository, so I'm not sure why Git is checking for one on this particular machine.

Upvotes: 582

Views: 1053167

Answers (30)

Muhammad Fauzi Masykur
Muhammad Fauzi Masykur

Reputation: 2242

When the terminal shows:

Are you sure you want to continue connecting (yes/no)?

DO NOT I repeat DO NOT just press Enter.

You MUST TYPE yes first in the terminal, then press Enter.

Upvotes: 31

Souhail Benlhachemi
Souhail Benlhachemi

Reputation: 199

After generating a SSH key pair you need to add your key to the ssh-agent:

1 - start the ssh-agent in the background (this depends on your environment)

$ eval "$(ssh-agent -s)" 
> Agent pid 59566

2 - Add your SSH key to the ssh-agent

ssh-add ~/.ssh/id_rsa

3 - add the SSH private key to your remote git account


source: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

Upvotes: 2

Mykhaylo Adamovych
Mykhaylo Adamovych

Reputation: 20976

You can add the following ssh key entries to your ~/.ssh/known_hosts file to avoid manually verifying GitHub hosts (ref1, ref2)

github.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl
github.com ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEmKSENjQEezOmxkZMy7opKgwFB9nkt5YRrYMjNuG5N87uRgg6CLrbo5wAdT/y6v0mKV0U2w0WZ2YB/++Tpockg=
github.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCj7ndNxQowgcQnjshcLrqPEiiphnt+VTTvDP6mHBL9j1aNUkY4Ue1gvwnGLVlOhGeYrnZaMgRK6+PKCUXaDbC7qtbW8gIkhL7aGCsOr/C56SJMy/BCZfxd1nWzAOxSDPgVsmerOBYfNqltV9/hWCqBywINIR+5dIg6JTJ72pcEpEjcYgXkE2YEFXV1JHnsKgbLWNlhScqb2UmyRkQyytRLtL+38TGxkxCflmO+5Z8CSSNY7GidjMIZ7Q4zMjA2n1nGrlTDkzwDCsw+wqFPGQA179cnfGWOWRVruj16z6XyvxvjJwbz0wQZ75XK5tKSb7FNyeIEs4TT4jk+S4dhPeAUC5y+bDYirYgM4GC7uEnztnZyaVWQ7B381AK4Qdrwt51ZqExKbQpTUNn+EjqoTwvqNj4kqx5QUCI0ThS/YkOxJCXmPUWZbhjpCg56i+2aB6CmK2JGhn57K5mj0MNdBXA4/WnwH6XoPWJzK5Nyu2zB3nAZp+S5hpQs+p1vN1/wsjk=

Upvotes: -1

Lamri Djamal
Lamri Djamal

Reputation: 291

You can use https instead of ssh for git clone or git pull or git push

ex:

git clone https://github.com/user/repo.git

Upvotes: 6

shrikanta mazumder
shrikanta mazumder

Reputation: 383

remove package-lock.json or yarn.lock and then try again

Upvotes: -6

Daniel L. VanDenBosch
Daniel L. VanDenBosch

Reputation: 2734

I just cleared out my known host file and that seemed to do the trick.

Upvotes: 3

Abd Abughazaleh
Abd Abughazaleh

Reputation: 5545

Dashboard > Manage Jenkins > Configure Global Security > Git Host Key Verification Configuration. Then in Host Key Verification Strategy select Accept first connection.

Upvotes: 3

CsAlkemy
CsAlkemy

Reputation: 366

Just type 'yes' and press enter this should work

Upvotes: -3

andrew
andrew

Reputation: 4089

One small addition to Tupy's answer, you may need to add the port number for your repository host:

ssh-keyscan -p 8888 -t rsa domain.example >> ~/.ssh/known_hosts

If you have another machine that does have remote access you can find the port number by viewing ~/.ssh/known_hosts:

[user]$ less ~/.ssh/known_hosts
[domain.example]:8888,[000.00.000.000]:8888 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCi...

Upvotes: 4

Greg Bacon
Greg Bacon

Reputation: 139681

You are connecting via the SSH protocol, as indicated by the ssh:// prefix on your clone URL. Using SSH, every host has a key. Clients remember the host key associated with a particular address and refuse to connect if a host key appears to change. This prevents man in the middle attacks.

The host key for domain.example has changed. If this does not seem fishy to you, remove the old key from your local cache by editing ${HOME}/.ssh/known_hosts to remove the line for domain.example or letting an SSH utility do it for you with

ssh-keygen -R domain.example

From here, record the updated key either by doing it yourself with

ssh-keyscan -t rsa domain.example >> ~/.ssh/known_hosts

or, equivalently, let ssh do it for you next time you connect with git fetch, git pull, or git push (or even a plain ol’ ssh domain.example) by answering yes when prompted

The authenticity of host 'domain.example (a.b.c.d)' can't be established.
RSA key fingerprint is XX:XX:...:XX.
Are you sure you want to continue connecting (yes/no)?

The reason for this prompt is domain.example is no longer in your known_hosts after deleting it and presumably not in the system’s /etc/ssh/ssh_known_hosts, so ssh has no way to know whether the host on the other end of the connection is really domain.example. (If the wrong key is in /etc, someone with administrative privileges will have to update the system-wide file.)

I strongly encourage you to consider having users authenticate with keys as well. That way, ssh-agent can store key material for convenience (rather than everyone having to enter her password for each connection to the server), and passwords do not go over the network.

Upvotes: 349

EM0
EM0

Reputation: 6337

Check permissions on the known_hosts file as well - both the user's (~/.ssh/known_hosts) and the global one (/etc/ssh/ssh_known_hosts).

In my case the old host was in /etc/ssh/ssh_known_hosts. When I removed it as root with sudo ssh-keygen -f /etc/ssh/ssh_known_hosts -R THE_HOST it changed permissions on that file to 0600, so SSHing to THE_HOST as root worked, but for any other user it failed with "Host key verification failed". The fix was:

sudo chmod 644 /etc/ssh/ssh_known_hosts

Upvotes: 3

Tupy
Tupy

Reputation: 12721

As I answered previously in Cloning git repo causes error - Host key verification failed. fatal: The remote end hung up unexpectedly, add GitHub to the list of known hosts:

ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts

Upvotes: 966

TRUPAL VASAVA
TRUPAL VASAVA

Reputation: 69

Problem: Host key verification failed. fatal: Could not read from remote repository.

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

Solution: I've checked all the settings and also checked the key settings in GitHub. Finally, I changed the Git URL from "[email protected]:palvsv/travelo-moon.git" to "https://github.com/palvsv/travelo-moon.git" in .config file "yourprojectdirectory/.git/config" and it works.

Upvotes: 0

Prometheos II
Prometheos II

Reputation: 374

Alternatively, if you're using MSYS2 terminals (on Windows*) and a passphrase, it might be that the terminal does not prompt the 'Enter passphrase' properly, thus denying access to SSH.

If you're on Windows, you can instead use the Git Bash or Powershell to get the prompt and properly connect. (I'm currently looking for a solution for MSYS.)

*Not sure if relevant.

Upvotes: 0

K D
K D

Reputation: 215

Pushing to Git returning Error Code 403 fatal: HTTP request failed

Check if there is Billing issue. Google Cloud stops uploading files to https://source.cloud.google.com/

I got this problem went away after Payment issue was fixed. But did not change the Keys.

Thanks

Upvotes: 2

Tk421
Tk421

Reputation: 6418

If you are not using a Windows Session to update the code, and you use PortableGit, you need to set the HOMEPATH environment variable before running the git command.

This example fits better for other use case, but I think it is a good of proof-of-concept for this post.

$env:HOMEPATH="\Users\Administrator";C:\path\to\PortableGit\bin\git.exe -C C:\path\to\repository.git pull'

Upvotes: 2

u_pendra
u_pendra

Reputation: 948

A other alternative worked for me, instead of cloning the SSH link

[email protected]:upendra/mycode.git

there is a option to select http link

http://gitlab.company.net:8888/upendra/mycode.git

So I used http link to clone for Visual studio and it worked for me

Upvotes: 2

sunil
sunil

Reputation: 507

If you are in office intranet (otherwise dangerous) which is always protected by firewalls simply have the following lines in your ~/.ssh/config.

Host *
  StrictHostKeyChecking no
  UserKnownHostsFile=/dev/null

Upvotes: 16

TheHowlingHoaschd
TheHowlingHoaschd

Reputation: 706

Reason seems to be that the public key of the remote host is not stored or different from the stored one. (Be aware of security issues, see Greg Bacon's answer for details.)

I was used to git clone prompting me in this case:

The authenticity of host 'host.net (10.0.0.42)' can't be established.
ECDSA key fingerprint is 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00.
Are you sure you want to continue connecting (yes/no)?

Not sure, why this error is thrown instead. Could be the configuration of your shell or the git SSH command.
Anyhow, you can get the same prompt by running ssh [email protected].

Upvotes: 2

RP-
RP-

Reputation: 5837

The solutions mentioned here are great, the only missing point is, what if your public and private key file names are different than the default ones?

Create a file called "config" under ~/.ssh and add the following contents

Host github.com
    IdentityFile ~/.ssh/github_id_rsa

Replace github_id_rsa with your private key file.

Upvotes: 4

Sandy
Sandy

Reputation: 1054

When the remote server wants to connect to the private repo, it would authenticate via ssh. Create the private-public key pair with ssh-keygen or if you already have the public-private key. copy&paste the public key in the Settings of the private repo.

YourPrivateRepo -> Settings -> Deploy Keys -> Add deploy key -> Paste the public key.

Now the remote server would be able to connect to the private repo.

NOTE: The deploy keys has access only for reading the repo. Need to explicitly allow write access.

Upvotes: 6

Saran
Saran

Reputation: 1393

I had the similar issue, but, using SSH keys. From Tupy's answer, above, I figured out that the issue is with known_hosts file not being present or github.com not being present in the list of known hosts. Here are the steps I followed to resolve it -

  1. mkdir -p ~/.ssh
  2. ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts
  3. ssh-keygen -t rsa -C "user.email"
  4. open the public key with this command $ cat ~/.ssh/id_rsa.pub and copy it.
  5. Add the id_rsa.pub key to SSH keys list on your GitHub profile.

Upvotes: 114

Victor Bruce
Victor Bruce

Reputation: 207

When asked:

Are you sure you want to continue connecting (yes/no)?

Type yes as the response

That is how I solved my issue. But if you try to just hit the enter button, it won't work!

Upvotes: 13

Adiii
Adiii

Reputation: 60074

I was facing the same error inside DockerFile during build time while the image was public. I did little modification in Dockerfile.

 RUN git clone  https://github.com/kacole2/express-node-mongo-skeleton.git /www/nodejs

This would be because using the [email protected]:... syntax ends up > using SSH to clone, and inside the container, your private key is not > available. You'll want to use RUN git clone > https://github.com/edenhill/librdkafka.git instead.

Upvotes: 3

Powderham
Powderham

Reputation: 1640

This is happening because github is not currently in your known hosts.

You should be prompted to add github to your known hosts. If this hasn't happened, you can run ssh -T [email protected] to receive the prompt again.

Upvotes: 81

fyodrs
fyodrs

Reputation: 1

I got this message when I tried to git clone a repo that was not mine. The fix was to fork and then clone.

Upvotes: -6

Julian Knight
Julian Knight

Reputation: 4923

If you are using git for Windows.

  • Open the git GUI.
  • Open the local git repository in git GUI.
  • Add the remote or push if the remote already exists.
  • Answer "yes" to the question about whether you want to continue.

The GUI client adds the key for you to ~/.ssh/known_hosts. This is easier to remember if you don't do it often and also avoids the need to use the git command line (the standard Windows command lines don't have the ssh-keyscan executable.

Upvotes: 5

ghiscoding
ghiscoding

Reputation: 13214

What worked for me was to first add my SSH key of the new computer, I followed these instructions from GitLab - add SSH key. Note that since I'm on Win10, I had to do all these commands in Git Bash on Windows (it didn't work in regular DOS cmd Shell).

Then again in Git Bash, I had to do a git clone of the repo that I had problems with, and in my case I had to clone it to a different name since I already had it locally and didn't want to lose my commits. For example

git clone ssh://git@gitServerUrl/myRepo.git myRepo2

Then I got the prompt to add it to known hosts list, the question might be this one:

Are you sure you want to continue connecting (yes/no)?

I typed "yes" and it finally worked, you should typically get a message similar to this:

Warning: Permanently added '[your repo link]' (ECDSA) to the list of known hosts.

Note: if you are on Windows, make sure that you use Git Bash for all the commands, this did not work in regular cmd shell or powershell, I really had to do this in Git Bash.

Lastly I deleted the second clone repo (myRepo2 in the example) and went back to my first repo and I could finally do all the Git stuff like normal in my favorite editor VSCode.

Upvotes: 8

Code-Apprentice
Code-Apprentice

Reputation: 83567

For me, I just had to type "yes" at the prompt which asks "Are you sure you want to continue connecting (yes/no)?" rather than just pressing Enter.

Upvotes: 40

Jaykumar Patel
Jaykumar Patel

Reputation: 27614

Its means your remote host key was changed (May be host password change),

Your terminal suggested to execute this command as root user

$ ssh-keygen -f "/root/.ssh/known_hosts" -R [www.website.net]

You have to remove that host name from hosts list on your pc/server. Copy that suggested command and execute as a root user.

$ sudo su                                                        // Login as a root user

$ ssh-keygen -f "/root/.ssh/known_hosts" -R [www.website.net]    // Terminal suggested command execute here
Host [www.website.net]:4231 found: line 16 type ECDSA
/root/.ssh/known_hosts updated.
Original contents retained as /root/.ssh/known_hosts.old

$ exit                                                           // Exist from root user

Try Again, Hope this works.

Upvotes: 2

Related Questions