Command to delete a particular host from known_hosts

Occasionally (especially in the cloud world, where instances are cattle), the SSH fingerprint for a host changes. When this happens, you will see a warning.

If the warning is expected, the usual remedy is to delete the offending key from your “known_hosts” file (typically found under ~/.ssh/known_hosts). However, when you need to do this across a bunch of machines and you don’t know what line number the host will be on, on each machine, the following command might be useful:

sed -i -e ‘/[webserver-03.example.com]:2222/d’ ~/.ssh/known_hosts

It deletes any line which matches the host “[webserver-03.example.com]:2222” in the default “known_hosts” file.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.