Fix “Host key verification failed, offending host key is” error

If you have a VPS/server and reinstall it, you may see a message similiar to the below while logging in to it:

scp .ssh/ [email protected]:~/
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    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 the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
43:ab:e2:c8:66:c3:c3:b7:b3:49:6d:01:57:4b:cd:39.
Please contact your system administrator.
Add correct host key in /root/.ssh/known_hosts to get rid of this message.
Offending key in /root/.ssh/known_hosts:4
RSA host key for 192.145.45.167 has changed and you have requested strict checking.
Host key verification failed.
lost connection

 

To fix this, you need to delete the offending host key from .ssh/known_hosts.

You can do this with sed:

sed -i '4d' .ssh/known_hosts[/code]
The "-i" option means, do an "in-place" modification of the file. The "4d" means to delete line number 4 from the file.

So after doing it, try again:
[[email protected]] ~ #sed -i '4d' .ssh/known_hosts
[[email protected]] ~ #scp -r .ssh/ [email protected]:~/
The authenticity of host '192.145.45.167 (192.145.45.167)' can't be established.
RSA key fingerprint is 43:ab:e2:c8:66:c3:c3:b7:b3:49:6d:01:57:4b:cd:39.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.145.45.167' (RSA) to the list of known hosts.
[email protected]'s password:
authorized_keys~                              100%  399     0.4KB/s   00:00
authorized_keys                               100%  801     0.8KB/s   00:00
id_rsa                                        100% 1679     1.6KB/s   00:00
known_hosts                                   100% 1768     1.7KB/s   00:00
id_rsa.pub                                    100%  391     0.4KB/s   00:00