SSH "remote host identification has changed": what to do
OpenSSH remembers the key of every host you have accepted. When the host offers a different key, it refuses to connect and prints a loud warning. The cause is often harmless, but the warning exists because it is also what a man-in-the-middle attack looks like. Check first, then fix.
This page describes standard OpenSSH behaviour. The commands are the usual ones from the OpenSSH tools, and wording can differ a little between versions.
What the warning says
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
...
The fingerprint for the ED25519 key sent by the remote host is
SHA256:...
Add correct host key in /home/you/.ssh/known_hosts to get rid of this message.
Offending ED25519 key in /home/you/.ssh/known_hosts:12
Host key verification failed.
Three parts matter. The fingerprint is the key the server
offered just now. The offending key line gives the file and
the line number of the entry that no longer matches. And
Host key verification failed means ssh stopped before it sent
anything, so no password or key signature has gone to that host.
Do not connect yet, and do not wipe the file
Two reflexes make this worse. Deleting the whole known_hosts file
throws away the check for every other host you use. Setting
StrictHostKeyChecking no, or answering yes without
looking, turns the check off for the one host where it just fired. Either way
you would accept whatever key is on offer, including one from something that
is in the way.
Compare the new fingerprint with one that reached you another way
The warning shows the new key's fingerprint. You need a second copy that did not come through this connection. Good sources:
-
The machine's own console, or a shell you already trust on it. On a stock
OpenSSH server the host public keys sit next to the server config, and
ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pubprints the fingerprint of one. - The output of the installer or image build that created the machine, if it printed one and you kept it.
- A colleague who already checked it at the console, and who tells you over a channel other than the one under suspicion.
Compare the full SHA256: text, not the first few characters. If
you would rather not compare by eye, paste both into the
SSH key fingerprint checker. It runs
in your browser, sends nothing, and says whether the two match. To see what
your file recorded before, ssh-keygen -l -F host prints the
fingerprint of the stored entry for that name. To see what kind of line your
file holds for that name, such as a plain or a hashed one, paste it into the
line checker; it does not say which
key is right.
Be careful with ssh-keyscan. It fetches the key over the same
network path as the connection, so a match between its output and the warning
proves nothing: both came from whatever answered. The fingerprint you compare
against has to come from somewhere else.
Common harmless causes
- The operating system was reinstalled. A fresh install generates new host keys, unless someone copied the old ones across.
- A virtual machine or cloud server was rebuilt from an image and the name or address stayed the same.
- The address now belongs to another machine. DHCP leases, reused cloud addresses and a swapped Raspberry Pi on the same IP all do this.
-
A tunnel port or alias now leads somewhere else. With a
reverse tunnel,
known_hostsrecords the loopback address and port, such as[localhost]:22022, which says nothing about which device sits behind it. If a port is reused for a different device, or two devices share an entry, ssh sees a changed key. AHostKeyAliasper device prevents it, and the jump host guide shows the config. - The server's keys were regenerated on purpose as part of a rotation or a hardening step.
Each of these has something you can confirm: a reinstall someone did, a rebuild in your own records, a port you reassigned. If you cannot name the event that explains it, treat the warning as unexplained.
If the fingerprints match: remove only that entry
Use the line number from the warning, or let ssh-keygen find the
entry by name. It removes every key stored for that name and keeps a copy of
the old file as known_hosts.old.
ssh-keygen -R server.example.com
The name has to be written the way known_hosts stores it. For a
server on a port other than 22, that is the bracketed form with the port. For
an alias, it is the alias:
ssh-keygen -R "[server.example.com]:2222"
ssh-keygen -R "[localhost]:22022"
ssh-keygen -R pump-07
If the file has hashed host names, so the line shows a string starting
|1| and not a name, ssh-keygen -R still finds the
entry by name. A host may also have an entry under its address as well as its
name. If the warning comes back after you removed one, run
ssh-keygen -R for the other form too.
Then connect once. ssh shows the new fingerprint and asks whether to continue.
Compare it against the fingerprint you verified, and answer
yes only if it is the same. From then on it is the recorded key.
If ssh then prints
Failed to add the host to the list of known hosts
and a path, the key was not saved. That is a different problem from a stale
line; see
SSH "Failed to add the host to the list of known hosts".
If they do not match: stop
A different fingerprint from the one the machine itself shows means something other than that machine is answering at this name or address. Do not connect and do not remove the line. Check that you have the right name, address and port. Look at what sits on the path: a captive portal, a proxy or a tunnel port that points at the wrong place. Ask whoever runs the machine. Treat it as a security question until it is explained.
If the machine is a RescueShell device
That case works differently, because the browser and CLI check the device's key for you. See host key changed in the troubleshooting page, and the first-connection prompt for the fingerprint you are asked for the first time.
Try it on one device
The first device is free to try with no card, for 30 days from when it connects, not from when you sign up. Start a trial.