Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is it possible to remove a particular host key from SSH's known_hosts file?

I usually end up deleting the entire known_hosts file, which I have no problems with doing, but just out of curiosity, is it possible to remove just a single entry?

I opened the known_hosts file, but I am struggling to understand its contents.

Below is the message I faced, which led me to ask this question:

Add correct host key in /home/wissen16/.ssh/known_hosts to get rid of this message.
Offending key in /home/wissen16/.ssh/known_hosts:1
RSA host key for foo.com has changed and you have requested strict checking.
Host key verification failed.
share|improve this question

5 Answers 5

up vote 117 down vote accepted

Use this command to remove entries from known_hosts:

ssh-keygen -R hostname
share|improve this answer
1  
very elegant solution! –  theTuxRacer Jan 12 '11 at 6:30
3  
It works with an IP address as well. For instance, I have a DNS host shortcut for my Web server. To remove a conflict I had between the keys for the custom hostname and the IP address, I had to remove the entries for both.So ssh-keygen -R xxx.xxx.xxx.xxx. –  StrangeElement Nov 13 '12 at 21:10

Yes, you can remove just one key. Just open it in an editor and delete the offending line. The number after the colon in the error message is the line number, so that's the line to delete -- line 1 in your example..

share|improve this answer

I have only recently started using host key's, but when I have messed with them it is generally one key per line so backup the file and remove them one at a time until you find the right one. Then add the others back. Bit of a long way to do it, but should work.

Also based on that error, and with no idea what so ever, it could be the first host key in the file that is the problem so open up the file with vim

vim ~/.ssh/known_hosts

and hit

dd

then save it.

share|improve this answer
    
You can use :set nu in vim to show the line numbers. Takkat answer is the best one anyway. –  Javier Rivera Jan 10 '11 at 10:46
    
I agree didn't know you could do that. Am going to use it in the future. I kept meaning to lookup the line numbers setting for vim. thanks. –  percent20 Jan 10 '11 at 21:17

Just to share another clean and easy answer I just found. Removing the hostname is out for me, as the known_hosts file is hashed. However, I COULD manually edit out the host entry based on the line number in the error message. As noted by Mike Scott previously, the offending hostname line number is in the error message.

Or, I can do this. From here: how to fix offending key in ssh known_hosts file

I got this bit of cli magic

sed -i 'xd' ~/.ssh/known_hosts

Replace the x with the line number, and voila. He also offers a perl answer if the sed will not work.

share|improve this answer
sed '/10\.20\.120\.211/d' ~/.ssh/known_hosts > temp && mv temp ~/.ssh/known_hosts

In this case, 10.20.120.211 is the host I want to delete from my known_hosts file, make sure you escape the special characters like (.)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.