# SSH Host Keys and known_hosts

> 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:** [SSH Keys and Logins](https://learn.benibix.com/learn/infrastructure-ssh)  
**Time:** 10 min

**After this lesson you can:**

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

## 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.

![Three rows. First visit: the server shows key A, you say yes, and key A is saved in known_hosts. Later visit: the server shows key A, it matches the saved key, and the login goes on. Key changed: the server shows key B, there is no match, and SSH stops with a warning that it may not be the real server.](https://learn.benibix.com/diagrams/inf3-host-key.svg)

### 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.

**Words in this lesson:** [SSH Host Key](https://learn.benibix.com/term/infrastructure/host-key), [known_hosts](https://learn.benibix.com/term/infrastructure/known-hosts)

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

**Answer:** Stop, and find out why before you log in. A changed key you cannot explain may mean a fake server is in the middle. Find out why first. Only remove the old line once you know the change is real.

---

Source: https://learn.benibix.com/learn/infrastructure-ssh/ssh-host-keys-and-known-hosts
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
