removing outdated ssh fingerprints from known_hosts with sed or … ssh-keygen

At least from the last issue in Debian-based systems including Ubuntu you might know the pain of getting the message from you ssh client that the server host key has changed as ssh stores the fingerprint of ssh daemons it connects to. Actually this is a neat feature because it helps you detecting man in the middle attacks, dns issues and other things you probably should notice.

Until recently I opened the file .ssh/known_hosts in vim, deleted the entry, saved the file and started over again. I randomly checked “man ssh” which gives you a lot of hints about the usage of known_hosts but I just did not find information about how to delete an old fingerprint or even overwrite it. I imagined something like “ssh –update-fingerpring hostname” with an interactive yes/no question you cannot skip. There is the setting “StrictHostKeyChecking” that might get you out of the fingerprint-has-changed-trouble but it does not solve the real problem as you want those checks.

So after hanging around with Mnemonikk discussing this he pointed out a very simple method with “sed” that is really handy and helps you understanding sed more deeply. You can advise “sed” to run a command on a specific line. So have a look at this session:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ ssh secrethost
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
[...]
Offending key in /home/ccm/.ssh/known_hosts:46
[...]
Host key verification failed.
$ sed -i "46 d" .ssh/known_hosts
$ ssh secrethost
The authenticity of host 'secrethost (1.2.3.4)' can't be established.
RSA key fingerprint is ab:cd:ef:ab:cd:ef:ab:cd:ef:ab:cd:ef:ab:cd:ef:ab.
Are you sure you want to continue connecting (yes/no)?

We just took the line number 46 which ssh complains about and run in in-place-editing mode (-i) with the command run on line 46 the command delete (d). That was easy, wasn’t it? Small lesson learned about sed. Thank you Mnemonikk (he is currently working on a screencast about screen if you let me leak some information here :) .

But to be honest I’s still looking for the “official” method the delete a key from known_hosts. Therefore I browsed through the man pages and finally found what I was looking for in “man ssh-keygen”. Yes, definitely zero points for usability as deleting with a tool named “generator” is confusing but it works, however. You can advice ssh-keygen to delete (-R) fingerprints for a hostname which helps you when you turned hashed hostnames on in you known_hosts:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ ssh secrethost
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
[...]
Offending key in /home/ccm/.ssh/known_hosts:63
[...]
Host key verification failed.
[ccm@hasung:255:/etc/ssh]$ ssh-keygen -R secrethost
/home/ccm/.ssh/known_hosts updated.
Original contents retained as /home/ccm/.ssh/known_hosts.old
[ccm@hasung:0:/etc/ssh]$ ssh secrethost
The authenticity of host 'secrethost (1.2.3.4)' can't be established.
RSA key fingerprint is ab:cd:ef:ab:cd:ef:ab:cd:ef:ab:cd:ef:ab:cd:ef:ab.
Are you sure you want to continue connecting (yes/no)?

So “ssh-keygen -R hostname” is a nice syntax as you even do not have to provide the file name and path for known_hosts and it works with hashed names. Nevertheless I’ll also use the sed syntax – keep it trained it’ll help you in other cases also.

Ubuntu BBQ on 31th of May – End of Linuxtag

Ubuntu Berlin strikes again! On the 31th of May, this Saturday (and last day of the “Linuxtag”), Ubuntu Berlin is proud to present the “Ubuntu BBQ” – an event you should not miss when staying in Berlin for attending “Linuxtag 2008“, live here or happen to be around by chance.

Hosted again by the sunken starship “c-base” we are happy to provide you with drinks at low prices, and BBQ and entrance for … free! Sponsorships from Canonical Ltd. (you might have heard of them) and ubuntu Deutschland e.V. (thank you!) and an invitation from the Linuxtag Community staff made this possible and we are anxious to see how many Linuxtag attendees make their way to the base.

But it’s not about eating and drinking: The event gives you the possibility to meet a lot of GNU/Linux and Ubuntu interested folks, even some of the well known free software gurus in a relaxed atmosphere. Of course you can use a free wifi network with you notebook/gadget/whatever, but don’t forget: It’s a party. Music will be around and you can sit directly at the rivercoast of the “Spree”. It’s said the weather will be great.

You are invited the join us starting from 4 pm – the BBQ will start from 7pm – so you have enough time to come over from Linuxtag. We will arrange some groups you can join on Linuxtag making it easier for you to find the c-base. See more on our (yet only German) announcement.