Clearing the known_hosts SSH File | Research Computing (2024)

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

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
5c:9b:16:56:a6:cd:11:10:3a:cd:1b:a2:91:cd:e5:1c.
Please contact your system administrator.
Add correct host key in /home/user/.ssh/known_hosts to get rid of this message.
Offending key in /home/user/.ssh/known_hosts:1
RSA host key for ras.mydomain.com has changed and you have requested strict checking.
Host key verification failed.

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.

Linux / Mac / Windows with MobaXterm

Finding the known_hosts File:

After you have connected to a computer using ssh, 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:

[abc123@computer ~]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 have a smaller screen the key will continue on to the next line, but is still counted as one line.

Method 1 - Getting Rid of It All

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:

cp ~/.ssh/known_hosts ~/.ssh/known_hosts.old
[abc123@computer ~]rm ~/.ssh/known_hosts

Method 2 - Targeting the Key Individually

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:

[abc123@computer ~]ssh-keygen -R HOSTNAME

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

# Host HOSTNAME found: line #
/home/user/.ssh/known_hosts updated.
Original contents retained as /home/user/.ssh/known_hosts.old

You can also edit the known_hosts file directly and remove the entire offending line which is indicated by: Offending key in /home/user/.ssh/known_hosts:1. In this case the line is 1. You may end up deleting something you did not intend to so back the file up first:

cp ~/.ssh/known_hosts ~/.ssh/known_hosts.old

Windows with PuTTY

Unlike the other options above, PuTTy does not create a convenient known_hosts folder in your home directory. Instead we need to edit the registry it creates:

  1. Search for regedit.exe and open it
    Clearing the known_hosts SSH File | Research Computing (1)
  2. Navigate to HKEY_CURRENT_USER/SOFTWARE/SimonTatham/PuTTy/SshHostKeys
    Clearing the known_hosts SSH File | Research Computing (2)
  3. Right click the offending key and click delete
    Clearing the known_hosts SSH File | Research Computing (3)

If there are any further questions, or there is an issue with the documentation, please contact rc-help@rit.edu for additional assistance.

Clearing the known_hosts SSH File | Research Computing (2024)

FAQs

How to clear Known_hosts SSH file? ›

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

What is the purpose of the SSH known_hosts file? ›

When connecting to a remote host, SSH checks the known_hosts file of the client to confirm the address or hostname for the server match the key we get from it.

Where is the SSH known hosts file? ›

The known_hosts file is located in the . ssh/known_hosts directory.

How do I remove known host from SSH on Mac? ›

We pass the -R option to the ssh-keygen command to remove all keys belonging to the specified hostname (with optional port number) from a known_hosts file. This option is useful to delete hashed hosts. We have ~/.

How to clear SSH connection? ›

Using the 'kill' command to terminate a specific session ID

When it comes to disconnecting an inactive or idle SSH connection, you can use 'kill' with the session ID associated with that particular connection. This will immediately terminate that specific SSH connection and log out any user associated with it.

How to update SSH known_hosts file? ›

gistfile1.txt
  1. Open known_hosts file and look for the invalid host. nano ~/.ssh/known_hosts.
  2. Remove the line that has the invalid host. Should be the same host in your .git/config of your repo. ...
  3. Pull from repo. git pull.
  4. You should see something similar to this. Answer 'yes' when asked.

What goes in known_hosts file? ›

The typical format of the "known_hosts" file contains three sections separated by spaces for each entry. The first section contains hostname/IP, the second section contains the algorithm, the third section contains the public key encoded in base64 format. Each entry creates a single unbroken line, similar to below.

How to read SSH known_hosts? ›

Steps to obtain known Hosts:
  1. With in your Linux environment, launch your terminal emulator. ...
  2. Next you will want to SSH into the SSH Server: ssh IP or DNS of the target host. ...
  3. From here you will need to change directory to the "Hidden" .ssh directory. ...
  4. to display the known host information use the command: cat known_hosts.
Apr 14, 2019

What is SSH and why is it important? ›

The Secure Shell (SSH) protocol is a method for securely sending commands to a computer over an unsecured network. SSH uses cryptography to authenticate and encrypt connections between devices.

How do I view SSH files? ›

To view the files in ssh, use the command 'ls'. This will provide the list of files and directories from the current location or the specified path. This command will display all the files and directories including hidden files and directories.

Where is the SSH server located? ›

The SSH server has its own set of configuration files, including the SSH server system-wide configuration file named sshd_config. By default, these files reside in the /etc/ssh directory on the remote host.

What is SSH host name? ›

In essence: Host is the string the user gives as input on the CLI when invoking SSH; HostName is the string that the SSH client will output over the network when attempting to connect to the server.

How to remove entry from Known_hosts file on Mac? ›

Open the file known_hosts with a text editor and remove the line with your device's host name or IP-address and the ecdsa key hint (e.g. ecdsa-sha2-nistp25). Open Terminal and enter nano ~/. ssh/known_hosts . Move the cursor to the respective line and hit ctrl K to remove the line.

How do I clear my host key in PuTTY? ›

Resolution
  1. Open the registry (regedit)
  2. Go to HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\SshHostKeys.
  3. Select SshHostKeys , right click and export.(This is for backup)
  4. There you should see Putty's cache of host keys. ...
  5. Delete the rows that you need.
  6. Regenerate the SSH keys and update into LC/VLC.
Jan 30, 2017

How to open known_hosts file in Mac? ›

Hosts File Location on Macs
  1. Launch the macOS Terminal application.
  2. Go to the Finder, then Click Go.
  3. Click Utilities.
  4. Double-click Terminal to launch the Terminal window.
  5. Now, enter the following command: sudo nano /etc/hosts.
  6. Then, enter the sudo password for your user to allow edit access to the file.
Nov 23, 2020

How to remove SSH cache? ›

In this folder you have a know_hosts file which contains all the known keys, the easiest way is to delete the files from the . ssh folder. Select all files, right click and click Delete. Relaunch the connection to the server and the SSH client normally offers you to validate the key as on the first connection.

How do I clear SSH cache in PuTTY? ›

Resolution
  1. Open the registry (regedit)
  2. Go to HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\SshHostKeys.
  3. Select SshHostKeys , right click and export.(This is for backup)
  4. There you should see Putty's cache of host keys. ...
  5. Delete the rows that you need.
  6. Regenerate the SSH keys and update into LC/VLC.
Jan 30, 2017

How to clear SSH in PuTTY? ›

How to clean up your putty session?
  1. Search powershell in your local system.
  2. Double click the powershell to open.
  3. Type the path of Putty.exe.
  4. Then type -cleanup command in the windows command line.
  5. Then press.
  6. Click Yes to clear the putty sessions.

How to purge SSH keys? ›

Deleting SSH Keys
  1. Step 1: Log into the Remote Server. You need to have administrative access to the remote server to delete SSH keys for a user. ...
  2. Step 2: Locate the SSH Directory. ...
  3. Step 3: Identify the Key to Delete. ...
  4. Step 4: Delete the Key. ...
  5. Step 5: Verify Deletion. ...
  6. Step 6: Restart SSH Service (Optional)
Oct 10, 2023

Top Articles
Latest Posts
Article information

Author: Zonia Mosciski DO

Last Updated:

Views: 5489

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Zonia Mosciski DO

Birthday: 1996-05-16

Address: Suite 228 919 Deana Ford, Lake Meridithberg, NE 60017-4257

Phone: +2613987384138

Job: Chief Retail Officer

Hobby: Tai chi, Dowsing, Poi, Letterboxing, Watching movies, Video gaming, Singing

Introduction: My name is Zonia Mosciski DO, I am a enchanting, joyous, lovely, successful, hilarious, tender, outstanding person who loves writing and wants to share my knowledge and understanding with you.