Skip to main content

ssh: Cleaning known_hosts obsolete entries

One thing that bothers me a lot when using ssh with VMs in my homelab is that I re-use IPs after destroying/recreating VMs. My .ssh/known_hosts then grows and contains a lot of obsolete entries, and eventually, it ends up conflicting.

Here the trick to disable strict host checking on connect for a subset of IPs. Just add the following in .ssh/config:

$ cat ~/.ssh/config
...
Host 10.2.1.*
  StrictHostKeyChecking no
  UserKnownHostsFile=/dev/null
...

Another possiblity is to remove them. Simply use ssh-keygen -R:

$ ssh-keygen -R "10.2.1.22"
# Host 10.2.1.22 found: line 40
# Host 10.2.1.22 found: line 41
# Host 10.2.1.22 found: line 42
/home/mycroft/.ssh/known_hosts updated.
Original contents retained as /home/mycroft/.ssh/known_hosts.old