August 26, 2013

Use SSH-keygen to remove a hostname from known_hosts

If you use SSH on a regular basis I'm sure you've received the warning that the "REMOTE HOST IDENTIFICATION HAS CHANGED". This occurs any time the SSH key for the host you are trying to connect to does not match the key saved in your knownhosts file (The knownhosts file is in the .ssh directory within your home directory by default).

Please note that this is a warning that should be taken seriously because it could indicate that your connect to your SSH server is compromised. Often though I recieve this warning after I have regenerated an SSH key or reinstall the operating system.

If you are sure that nothing malicious has happened to your connection it is a simple enough task to open the known_hosts file in whatever text editor you prefer and remove the offending key. I've always found this process rather clunky and as it turns out there is a much easier way to remove the offending key using ssh-keygen.

To remove the key just use: ssh-keygen -R OFFENDINGHOST (replace OFFENDINGHOST with the IP address or domain name of the host in question):

Tags: Linux SSH