Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If a host's operating system has been re-installed and had its public key regenerated sshing to it will of course fail because the new key doesn't match the old one.

Is there an easier way to tell ssh that you know that the host's key has changed and that you want it to be updated. I think it feels a bit error-prone to use a text editor or something like sed to remove the offending line.

share|improve this question
up vote 25 down vote accepted

Use ssh-keygen -R hostname to remove the hostname from your known_hosts file. The next time you connect, the new host key will be added to your known_hosts file.

share|improve this answer
HOST=[hostname or IP]

# Remove the old key(s) from known_hosts
ssh-keygen -R $HOST

# Add the new key(s) to known_hosts
ssh-keyscan $HOST >> ~/.ssh/known_hosts

The next time you connect, you'll connect without being asked Are you sure you want to continue connecting (yes/no)? since the keys will already be in the known_hosts file.

share|improve this answer
see also the StrictHostKeyChecking in ~/.ssh/config – Jeff Schaller Apr 12 at 18:17

Your Answer


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.