I deleted the known_hosts file from .ssh folder an

2019-03-25 13:10发布

问题:

I made a mistake and deleted the "known_hosts" file in ".ssh" folder! Now I can't pull or push in BitBucket repositories.

How can I solve it?

回答1:

Try executing:

ssh -T git@bitbucket.org

It would give you a message somethign like:

The authenticity of host 'bitbucket.org (207.223.240.181)' can't be established.
RSA key fingerprint is 97:8c:1b:f2:6f:14:6b:5c:3b:ec:aa:46:46:74:7c:40.
Are you sure you want to continue connecting (yes/no)? yes

Say yes after verifying the host fingerprint (see details in the end).

Then you should get this warning, which you can ignore safely:

Warning: Permanently added 'bitbucket.org,207.223.240.181' (RSA) to the list of known hosts.
conq: logged in as tuxdude.

You can use git or hg to connect to Bitbucket. Shell access is disabled.

Try running the git pull or git push once again and it should work this time.

NOTE: I assume you still have your .ssh/id_rsa file safely which is your private key for SSH authentication.

Verifying Host Fingerprint

As @Pascal_dher pointed out, you can be a victim of man-in-the-middle attack if you do not verify the host's fingerprint (using some out of band approach that you can rely on). For bitbucket, the info is available here.



回答2:

From the "using ssh with BitBucket" page, the next use of ssh should re-create that known_hosts file:

Bitbucket hosts only allow Git and Mercurial to make SSH connections.
The first time you access Bitbucket using the SSH URL, your SSH client checks to see if the Bitbucket host is a known host. If the host is not in your ~/.ssh/known_hosts file SSH warns you that it is adding the Bitbucket host to known hosts:

$ hg clone ssh://hg@bitbucket.org/newuserme/mquotefork testkey
The authenticity of host 'bitbucket.org (207.223.240.182)' can't be established.
RSA key fingerprint is 97:8c:1b:f2:6f:14:6b:5c:3b:ec:aa:46:46:74:7c:40.
Are you sure you want to continue connecting (yes/no)? 

If you view the contents of known hosts is stored you find the actual key is stored in a base64 encoded format:

bitbucket.org,207.223.240.182 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAubiN81eDcafrgMeLzaFPsw2kNvEcqTKl/VqLat/MaB33pZy0y3rJZtnqwR2qOOvbwKZYKiEO1O6VqNEBxKvJJelCq0dTXWT5pbO2gDXC6h6QDXCaHo6pOHGPUy+YBaGQRGuSusMEASYiWunYN0vCAI8QaXnWMXNMdFP3jHAJH0eDsoiGnLPBlBp4TNm6rYI74nMzgz3B9IikW4WVK+dc8KZJZWYjAuORU3jc1c/NPskD2ASinf8v3xnfXeukU0sJ5N6m5E8VLjObPEO+mN2t/FZTMZLiFqPWc/ALSqnMnnhwrNi2rbfg/rd/IpL8Le3pSBne8+seeFVBoGqzHM9yXw==

So simply using an ssh address (or just ssh command) should be enough to trigger that message again.