MacOS – SSH Key Error, sudo works fine

macossshterminalunix

When I try to ssh (using ssh user@server.com) into my server at home from either the local network or from outside, I get the following error:
enter image description here

But if I use sudo ssh user@server.com, it works with no problems:
enter image description here

I'm sure I just need to place some kind of key in that /Users/mattlove/.ssh/known_hosts directory, but I'm not sure how or why.

Any of you UNIX buffs out there have an answer?

E D I T :

Here are the results of running ls -l ~/.ssh/known_hosts as requested by @LoïcWolff

enter image description here

Best Answer

The first time you connect to a server, you are prompted to accept the fingerprint for the host you are connecting to. The fingerprint is a combination of a digital signature, the host name and the IP address of the server. When you do accept, the fingerprint is saved in ~/.ssh/known_hosts as being a "trusted" host.

For some reason, the server's fingerprint has changed since the first time you connected to it. This may be because it is not the server you expect it to be, for example when your request is being intercepted and redirected to a different server. Hence the security warning. The explanation might also be less malicious, for example when the server was replaced/reinstalled without restoring it's previous digital signature, or when the IP address has changed.

If despite the security warning, you still trust the server you are connecting to, you should open ~/.ssh/known_hosts and look for the line starting with the address of the server you are connecting to. Remove the entire line and save your changes, then reconnect to the server. The first time you connect, it should again prompt you to accept the updated fingerprint, after which you should have no more troubles connecting to that host.

The reason why it would work with sudo is because in those conditions you are running it as a different user (root), and each user has it's own known_hosts file (for root this is located at /private/var/root/.ssh/known_hosts). root may have never recorded the previous fingerprint of that host, and therefor has no way of comparing it to the current one, and no reason to distrust the server.