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
                                                                       


    • Related Articles

    • How To Edit Hosts File (Local Point) In Linux, Windows, Or Mac

      Introduction The hosts file in Windows, Mac, or Linux maps hostnames to IP addresses. For example, an intranet, like a corporate office that has servers for users to access, or it could be domain names for servers on the internet. Editing your hosts ...
    • How to Connect to an SSH Server from Windows, macOS, or Linux

      An SSH client allows you to connect to a remote computer running an SSH server. 1. Windows The most popular and widely recommended solution for connecting to SSH servers is an open source, third-party application called PuTTY. But now with windows 10 ...
    • How to connect to ftp/sftp using filezilla?

      What is filezilla? Filezilla is a free and open source SFTP(secure FTP)/FTP client which is built on modern standards. It is available cross-platform (Mac, Windows and Linux) and is actively maintained. First you will need to download and install the ...
    • [How To]: Reset bulk passwords for users in Zimbra

        How to Reset password for all (any) users in Zimbra. Login to Zimbra SSH. updatedb && locate zmprov In most cases it the location for the bin file of zmprov will be at /opt/zimbra/bin/zmprov Now Fetch Users from the Zimbra Backend.     ...
    • Connect MySQL database from MySQL Workbench

      To directly connect the MySQL server from MySQL Workbench we have to allow the IP address of the system you will be accessing the MySQL Workbench from. As convenient it sounds, it isn't always feasible to do so since the some of us still have a ...