How to remove permanently added host from list of known hosts?

If something has been added to 'list of known hosts' then in git bash shell under Windows and also under linux, an entry will have been added to the file known_hosts which can be found in .ssh directory below your home directory.

This is a text file and will show entries for any hostname/ip address/key combinations that have already been added.

So cat ~/.ssh/known_hosts

should show the following file

You may see something similar to the following

removelater.com,123.456.789.10 type-of-key charactersRepresentingTheKeyForRemoveLaterHost keep.com,321.654.987.10 ssh-rsa differentSetOfCharactersRepresentingKeyForKeepHost==

The above file has two lines.

Using your favorite editor (e.g. vi ~/.ssh/known_hosts or notepad ~/.ssh/known_hosts), simply delete the complete line which has the reference to the host you want to remove and save the file.

Trying to connect again to the host that you have now removed will once more result in

The authenticity of host 'removelater.com (123.456.789.10)' can't be established.


It means that git has used SSH to sign into the remote host for you, and that you had never connected to that server before, and so it added the server to your list of known hosts. If the server ever changes its identity (e.g. your connection is being intercepted by an attacker) then SSH will refuse to connect to it.

Read this: https://security.stackexchange.com/questions/20706/what-is-the-difference-between-authorized-key-and-known-host-file-for-ssh

There's no need to worry about this though, unless you are paranoid and expecting someone to try to steal your password or your work.


The simplest way to remove just one host from known_hosts is to use:

ssh-keygen -R hostname

Example

ssh-keygen -R 192.168.1.10
ssh-keygen -R abc.lan
ssh-keygen -R domain.com

Tags:

Git

Ssh