Skip to content
BENIBIX Learn

SSH Host Keys and known_hosts

Level 1 · Connecting

How a server proves it is the real one with its host key. Why ssh asks yes or no on the first visit, and what to do when the key changes.

Course contentsLesson 2 of 9

After this lesson you can

  • Answer the first-visit question with care
  • Say what known_hosts holds
  • Act on a changed host key warning

10 mincovers 2 glossary entries

Worth reading first: SSH Client and Server

How your laptop knows it reached the real server

Every SSH server has a host key: a key that proves it is the same server each time. On your first visit, ssh shows the key's fingerprint and asks if you trust it. Say yes, and it saves the key in a file called known_hosts. On every later visit, it checks the server shows the same key.

The check is there to stop a fake server. Someone could sit between you and the real one and pretend to be it. Without the check, you would type your password straight to them.

The fingerprint is a short code, such as SHA256 followed by 43 letters and numbers. To check it for real, compare it with the one your host shows in its panel, or the one the server prints on its own screen. Most people just type yes, and most of the time it is fine.

known_hosts lives in the .ssh folder in your home folder. It has one line per server: the address and the key. You rarely need to open it.

If the key ever changes, ssh stops with a big warning that starts with REMOTE HOST IDENTIFICATION HAS CHANGED. It will not log you in. There are two kinds of cause. The harmless one is that the server was rebuilt, and a rebuilt server gets a new key. The bad one is a fake server.

If you know you rebuilt it, remove the old line with ssh-keygen -R and the address. Then connect again, and ssh asks about the new key as if it were a first visit.

A common mistake is to delete the whole known_hosts file to make the warning go away. That throws away the saved key for every server you use. Remove only the one line, and only when you know why the key changed.

console
$ ssh-keygen -R 203.0.113.10
# Host 203.0.113.10 found: line 4
/home/emma/.ssh/known_hosts updated.
$ ssh emma@203.0.113.10
ED25519 key fingerprint is SHA256:Xq3v...
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
After a rebuild, remove the old key's line. Then accept the new key on the next visit.
Saved on the first visit, checked on every visit after that.

Worked example

Jack rebuilds his server

Jack wipes his VPS and starts it again from scratch. The next morning, ssh refuses him with the changed-key warning.

He knows why, so he runs ssh-keygen -R 203.0.113.10 and connects again. He checks the new fingerprint against the one in his host's panel, types yes, and is in.

The words for this lesson2

Check yourself

ssh warns that a server's host key has changed. You did not rebuild it. What do you do?

Finished this lesson?

Marking it complete also marks its 2 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.