Fast Way to Edit SSH known_hosts
Few days ago I migrated my website to a new server, and whenever you try to connect
to a server using ssh
it must known it first, if not it will complain, and ask you
verfiy the server key signature:
$ ssh rayed.com
The authenticity of host 'rayed.com (x.x.x.x)' can't be established.
:
Are you sure you want to continue connecting (yes/no/[fingerprint])?
If you say yes, it will add the server signature to your system as known host, in
the file ~/.ssh/known_hosts
But what will happen if you moved the machine or reinstall it! ssh will show this scary warning, because it might someone trying man-in-the-middle attack:
$ ssh rayed.com
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ 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.
Please contact your system administrator.
Add correct host key in /Users/rayed/.ssh/known_hosts to get rid of this message.
Offending ED25519 key in /Users/rayed/.ssh/known_hosts:392
Host key for rayed.com has changed and you have requested strict checking.
Host key verification failed.
To solve it you can open the file ~/.ssh/known_hosts
and delete the offending
line, e.g. line #392 in my case.
A better and faster solution I always forget is using the command:
ssh-keygen -R rayed.com
The will delete the key assiocated with the host you gave, next time you connect it will treat it as new machine.