Let's imagine the following scenario.
I have an host key ABCDEF1234
for a given hostname, so my known_hosts
file looks like this (unhashed version):
example.com ssh-rsa ABCDEF1234
Now I connect to it, and hostnames resolve to 10.11.12.13
I have a message like this
Warning: Permanently added the ECDSA host key for IP address '10.11.12.13' to the list of known hosts.
And my known_hosts looks like this
example.com ssh-rsa ABCDEF1234
192.0.2.1 ssh-rsa ABCDEF1234
Now a few month later, example.com
admin tells me the RSA key is removed and changed in favour of 1234ABCDEF
key.
So I remove the offending key using ssh-keygen -R example.com
, then I connect for the first time, accepts the key, and know my known_host
looks like this:
192.0.2.1 ssh-rsa ABCDEF1234
example.com ssh-rsa 1234ABCDEF
And every time I connect I have this nice warning message:
Warning: the ECDSA host key for 'example.com' differs from the key for the IP address '192.0.2.1'
Offending key for IP in /home/jenkins/.ssh/known_hosts:162
Matching host key in /home/jenkins/.ssh/known_hosts:182
Now imagine there are tens or hundreds of IP for that particular hostname, that is quite a lot of lines to clean.
One solution is to use sed
to remove matching lines, but isn't the goal of ssh-keygen -R
to avoid messing with sed
?
sed -i '/ABCDEF1234/d' known_hosts
Is there another solution, that would remove all entries from a known_host
file that are associated with a given key, instead of an hostname or IP address ?