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

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.

Tags:

Macos

Terminal