sed '${LINE}d' ~/.ssh/known_host

Remove a line from a file using sed (useful for updating known SSH server keys when they change)

When you SSH to a server who's hostname or IP has changed since the last time a connection was recorded in the known_hosts file a warning will be displayed since this indicated a possible DNS spoofing attack. If this is a known change then this command will remove the previous entry and allow the SSH connection. The SSH client will prompt you as if it was the first time connected to the server. Replace ${LINE} with the line of the offending key in ~known_hosts. 49 in the sample output.
Sample Output
user@host:~$ ssh user@server
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@       WARNING: POSSIBLE DNS SPOOFING DETECTED!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
The RSA host key for server has changed,
and the key for the corresponding IP address 1.2.3.4
is unknown. This could either mean that
DNS SPOOFING is happening or the IP address for the host
and its host key have changed at the same time.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
ff:ff:f:ff:ff:ff:ff:ff:ff:f:ff:ff:ff:ff
Please contact your system administrator.
Add correct host key in /home/user/.ssh/known_hosts to get rid of this message.
Offending key in /home/user/.ssh/known_hosts:49
RSA host key for server has changed and you have requested strict checking.
Host key verification failed.
user@host:~$ sed -i '49d' ~/.ssh/known_hosts
user@host:~$ ssh user@server
The authenticity of host 'server (1.2.3.4)' can't be established.
RSA key fingerprint is ff:ff:f:ff:ff:ff:ff:ff:ff:f:ff:ff:ff:ff.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'server,1.2.3.4' (RSA) to the list of known hosts.
user@server's password: 

-5
2012-01-16 18:00:12
sed

4 Alternatives + Submit Alt

What Others Think

To remove a line in know_hosts file you can also do ssh-keygen -R www.example.com
gilles · 331 weeks ago
That doesn't actually leave you with an ~/.ssh/know_host file with the line removed anyway.
DaveQB · 329 weeks and 3 days ago
I went for mv ~/.ssh/known_hosts ~/.ssh/known_hosts.old; sed "${LINE}d" ~/.ssh/known_hosts.old > ~/.ssh/known_hosts ... because not only does ssh-keygen silently fail with the -R option on my (ooooold) system, but sed doesn't know -i either ...
assarbad · 324 weeks and 5 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: