Clearing the known_hosts ssh file

Clearing the known_hosts ssh file


Sometimes when connecting to a computer with SSH, things can get jumbled up and an error can occur that looks like this:




The purpose of the known_hosts file is for the client to authenticate the server they are connecting to.
This error will occur when the public key the host has changes.To fix this we must remove the key causing the error.

Finding the known_hosts File in Linux/MAC:


The key you used to connected is stored in a file called known_hosts which is located in a hidden file (.ssh) in your home directory. It can be opened in a text editor of your choice with:

  1. vim ~/.ssh/known_hosts
You will notice the file is arranged: computername, ip-address veryLongKey, where each line is its own computer and key.

If you only have one host in your know_host file then removing the entire file is a solution. The file will be recreated the next time you ssh into that computer. Before you remove the file you should back up the contents:

  1.  cp ~/.ssh/known_hosts ~/.ssh/known_hosts.old
  2.  rm ~/.ssh/known_hosts

If you look at the error message you will notice this line:   RSA host key for ras.mydomain.com has changed...This tells which is the offending host, so now you can remove that key by running: 
                                                                                                       ssh-keygen -R HOSTNAME

This will give you the following output when it is successful :
                                                                  

Clearing hosts-file on Windows with PuTTY : 


We need to edit the registry to achieve the goal in windows :

  1. Search for regedit.exe and open it -
                                                                                     
  2. Navigate to HKEY_CURRENT_USER/SOFTWARE/SimonTatham/PuTTy/SshHostKeys -
                                                                     
  1. Right-click the offending key and then click delete