Fix SSH Issue : Offending key for IP in ~/.ssh/known_hosts:n

When ever we connect to another host with ssh client, it asks the user to add the public key of the remote host in the ~/.ssh/known_hosts file of that user. This is by default enabled by “StrictHostKeyChecking yes” which can be altered to no in /etc/ssh/ssh_config file.

But sometimes what happened, the host name remain same, but the key value might have been changed, due to some reason like : may be a new system with same host name OR the system may be compromised OR any other reason, the error will come as the fingerprint will mismatch:

Offending key for IP in ~/.ssh/known_hosts:n

Where ‘n’ is the line number in that file.

So in this case manually open the   ~/.ssh/known_hosts and remove the line number “n”.

Or a simply perl pie will search and replace the line with an empty.

  perl -pi -e ‘s/\Q$_// if ($. == n);’ ~/.ssh/known_hosts

Leave a Reply