← /blog

SSH host key changed: when to accept it, and when to stop

2026-10-20 · Linux hardening · 5 min read

This is what I got when I reconnected to a container I had just recreated:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    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 a host key has just been changed.
The fingerprint for the ED25519 key sent by the remote host is
SHA256:R8PZd9wIg8Bb5Y1Ts5Nmr8tF29TrWwpY8fSYRf5AJaU.
Offending ECDSA key in /home/user/.ssh/known_hosts:12
Host key for [127.0.0.1]:2222 has changed and you have requested strict checking.
Host key verification failed.

One detail in that message looks like a contradiction: it names an ED25519 fingerprint, then an ECDSA entry in known_hosts. It is not a contradiction — the stored entry was ECDSA, the negotiation landed on ED25519, and known_hosts had nothing of that type for this host.

That distinction matters more than it looks. “A key of a different type appeared” is not the same event as “the identity changed”: a server can keep its ECDSA key and add an ED25519 one, or a client can reorder its preferences after an upgrade. To find out which happened, ask for the old type explicitly — ssh -o HostKeyAlgorithms=ecdsa-sha2-nistp256 — and see whether the stored key still matches. If it does, nothing was replaced; you are looking at a key type your known_hosts had never seen.

The honest reaction is to press yes and move on. That is also how people get owned. This post is the ten minutes I spent instead, and the rule I came out with.

What a host key is

Every SSH server has its own key pair, generated once when SSH is installed or first started. It works the same way as your own key pair, only backwards:

your key pair ...... proves who YOU are, to the server
the host key ....... proves who the SERVER is, to you

Without it, you would have no way to tell the server you meant from anything that answered on that address.

Why your machine remembers it

The first time you connect, your client receives the server's public key and stores the key itself in ~/.ssh/known_hosts. The fingerprint you see in the warning is not a separate record — it is a short, comparable form derived from that key, so you can check it by eye or read it over the phone. On every later connection, it compares what it gets now with what it stored:

same key ..... continue, silently
other key .... stop, and show the warning above

That warning is not a bug. It is the only signal you have that you might be talking to something else.

The detail that trips everyone: [host]:port

My entry was not stored under 127.0.0.1. It was stored under [127.0.0.1]:2222, because the port is not 22. That is why the obvious fix does nothing:

ssh-keygen -R 127.0.0.1              # does not touch the entry
ssh-keygen -R "[127.0.0.1]:2222"     # removes it

The brackets are not decoration. They tell the client "this is host and port, not a hostname". If you have ever "fixed" the warning and had it come back, this is why.

Order matters, and this is where people get hurt. Removing the entry does not verify anything — it deletes the only record you had of what the server used to be. If you run ssh-keygen -R first and then accept whatever appears, you have replaced a check with a shrug. Get the new fingerprint from a source you trust first (console, provider panel, a person on another channel), and only then remove the stale line and compare what you were given with what you see.

Two situations, one question

A changed key has two possible explanations, and they are not equally common. Everything else — a reinstall, a migration, an ephemeral container — is a variant of the first:

legitimate ..... you reinstalled the server, recreated the container,
                 migrated to a new provider, or the provider rotated the keys

hostile ........ someone is between you and the server, presenting their key

The question that separates them is not technical. It is:

Did I change anything?

If yes — a reinstall, a new container, a migration — you know why the key differs, and you can verify the new fingerprint through a channel you trust. If no, stop. Not "probably fine". Stop.

What I did

I had recreated the container two minutes earlier, so I knew the key would differ. I also had the fingerprint the setup script had printed — read from the build output, not from the connection that was warning me. With both in hand, removing the stale entry was safe:

ssh-keygen -R "[127.0.0.1]:2222"

The next connection asked me to trust a new key. I compared the fingerprint with the one the setup script had printed:

SHA256:R8PZd9wIg8Bb5Y1Ts5Nmr8tF29TrWwpY8fSYRf5AJaU   ← matches the script output

Then I accepted. Two conditions, both checked: I knew why it changed, and I had an independent source for the new value. Without both, "yes" is a guess.

On a server you do not control

This is where it gets less comfortable. You cannot re-run a setup script you did not write. In order of preference:

  1. The provider's console. Check whether your panel shows the host keys. If it does not, open the serial or VNC console and read /etc/ssh/ssh_host_ed25519_key.pub in there yourself.
  2. A second channel. Ask the person who runs it, over something that is not the connection in question.
  3. Nothing. If you cannot verify it, do not accept it. A failed connection costs you five minutes; a wrong "yes" costs you the server.

Why StrictHostKeyChecking=no is a bad habit

You will find this suggested in tutorials and CI snippets, usually to make a script stop complaining:

ssh -o StrictHostKeyChecking=no ...

What it does is turn off exactly the check that just protected you. The warning disappears, and so does the only signal that would tell you something is wrong.

If you need something for automation, accept-new is the narrower option: it adds host keys it has never seen, but still refuses a host whose key has changed. That is the part you actually want to keep — with the caveat that it is still trust-on-first-use: the first connection to a new host is accepted blind, so an attacker who gets there first is trusted. Ship the expected key out of band (configuration management, a known_hosts file baked into the image) if the first connection matters.

The rule

A changed host key is not an error to suppress. It is a question: do I know why this changed, and can I verify the new value somewhere else?

Two yeses — accept, and move on. Anything else — stop. The wall of @@@ is not the tool being dramatic. It is the tool doing its one job.

Sources: ssh_config(5) and sshd_config(5) for StrictHostKeyChecking, HostKeyAlgorithms and the [host]:port form; ssh-keygen(1) for -R.

Want your server checked? I run a read-only hardening audit — what's exposed, what it actually enforces, and the evidence behind each finding. If there is nothing worth fixing, you don't pay.