7

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 johndoe@mu.math.umn.edu, 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.

6

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.

  • So this is something I need to do in a text editor, not terminal? – Sam Feb 17 '15 at 17:08
  • @Itried Edited answer based on your added info in question. – grg Feb 17 '15 at 17:10
  • Ok, I installed a text editor. Now how do I open that file in MacVim? – Sam Feb 17 '15 at 18:25
  • 2
    @Itried I don't have any experience with MacVim. I suggest you use TextEdit with open ~/.ssh/known_hosts or nano with nano ~/.ssh/known_hosts, both of which are preinstalled. – grg Feb 17 '15 at 18:26
  • 1
    @testing You don’t add anything to the key, you just use the key as-is. You don’t need to restart for changes to take effect. A comma delimiting IPs and host names allow for a key to apply to more than one IP or host name. – grg Jul 15 at 15:43
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .