In Terminal, how do I add a host key to the file known_hosts?

In Terminal, how do I add a host key to the file known_hosts? Also, how do I remove a given host key from the same file?

I trying to ssh into a computer at my school and when I type in the command [email protected], it returns:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    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 a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
fb:59:b6:4f:e0:9d:99:64:12:44:c8:16:1c:46:b0:92.
Please contact your system administrator.
Add correct host key in /Users/johndoe/.ssh/known_hosts to get rid of this message.
Offending RSA key in /Users/johndoe/.ssh/known_hosts:1
RSA host key for mu.math.umn.edu has changed and you have requested strict checking.
Host key verification failed.

I am not sure, but I think want I want to do is add this "finger print for the RSA key" to the file known_hosts.


Solution 1:

You can use any text editor to edit ~/.ssh/known_hosts such as nano or vi. Use the format…

serverIP ssh-rsa key

Based on the edited question, it seems you have an invalid key in the file which disagrees with the key that is being provided by the host. A simple fix is to open ~/.ssh/known_hosts in any text editor, remove the entirety of line 1 (as indicated by "known_hosts:1" in the error), save the file and try to connect again. This time, you will be prompted to add the key and it will be added automatically.

The known_hosts file stores keys for hosts to confirm that the host that you are connecting to is the host that is expected. When the host changes the key, it prevents all connections as the expected key does not match. You need to remove the old key and the new key will be added automatically next time you try to connect.

Solution 2:

It would seem that the ssh server configuration you are trying to connect to has changed. If you know the username and password for the connection then you have to perform the following procedure:

  1. Open a terminal and type:

    open /Users/leo/.ssh/known_hosts
    
  2. Find the line that starts with the IP address of the ssh server you are trying to connect to and finish with the character "=" and delete it.

  3. Try to reconnect with:

    ssh username@IPServer
    
  4. Now you should ask yourself to add the server to the known hosts. Type yes and confirm and you can connect again.