13

How do I delete ALL SSH known hosts?

I've managed many VPSs before and I want to delete these keys.

  • http://askubuntu.com/questions/20865/is-it-possible-to-remove-a-particular-host-key-from-sshs-known-hosts-file ? – Rinzwind Feb 01 '17 at 21:46

1 Answers1

15

First of all you should remember to verify ssh key finger prints when connecting to a remote computer for the first time, to avoid MITM attacks.

Having said that, first making a backup, and then removing all previous ssh known hosts is a matter of doing this on your local computer :

cp -av ~/.ssh/known_hosts ~/.ssh/known_hosts-old
rm ~/.ssh/known_hosts

The known_hosts file will be created again after you completely initiated the first new ssh session.

albert j
  • 1,453
  • 11
    Why cp then rm instead of just mv? – Ginnungagap Feb 01 '17 at 22:58
  • Is not it safer? What if a crash happens during mv command? Will the file be destroyed? – PouJa May 14 '23 at 15:55
  • @Ginnungagap you are right. mv is a one-liner and it offers speed and atomicity. On the contrary, a 2-step approach of cp and rm is a File Overwrite Strategy which gives more control over how overwrites are handled. – Phemelo Khetho Nov 16 '23 at 11:51