I run an Ubuntu desktop with a bunch of virtual servers in Virtual Box to test stuff out, etc. In the past I have also been connecting to other kinds of remote VPS Linux boxes. Currently my .ssh/known_hosts
file has a whole bunch of keys in it, most of which are not being used any more.
I want to clean up my .ssh/known_hosts
file, but how do I know which key belongs to what host? I.e. how do I know which keys I can safely remove and which ones I should leave alone?
To find out which entry is for a known hostname in known_hosts:
To delete a single entry from known_hosts:
With difficulty...
Ubuntu by default hashes hostnames the known_hosts file (this is not the default openssh behaviour), to make it difficult for anyone reading the file to know what systems you access.
If you really wanted to clean out the file, simplest option is probably just delete it and check the keys for servers you know as they arise, but really I'd just leave known_hosts alone.
You can stop new hosts entries from being hashed by commenting out the option in /etc/ssh/ssh_config
If you've got a list of all your hosts, you can do something like
That will overwrite your .ssh/known_hosts file with a newly generated one based on scanning the hosts.
And also do what theotherreceive suggests; HashKnownHosts is more annoyance than help here.
I had over 300 stale old entries in my known_hosts file. Not sure that it will work for all systems (or even most systems), but here is my Q&D script. You may have to adjust the matching strings or location.