Skip to content

Host key verification failed — diagnose the SSH warning instead of deleting the line

What "Host key verification failed" and "REMOTE HOST IDENTIFICATION HAS CHANGED" actually mean, how to verify a new host key out-of-band with ssh-keyscan and ssh-keygen -lf, and the surgical ssh-keygen -R fix.

Host key verification failed. is OpenSSH telling you it cannot prove that the machine answering on port 22 is the one it spoke to last time. It arrives in two shapes: a quiet refusal when the host is unknown and strict checking is on, and the full WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! banner when the key you have on file does not match the key the server just presented. Both are the check doing its job. The reflex — delete the offending line, reconnect, accept whatever comes back — discards the only signal you had, and it works exactly as well against a rebuilt VM as it does against an attacker sitting in the path. The correct first move is to obtain the server’s current fingerprint through a channel that is not the SSH connection you are suspicious of, compare it, and only then decide whether to update known_hosts or to stop and escalate.

What you’ll accomplish

  • Tell apart the three outcomes of host key checking: match, first contact, and mismatch.
  • Find the stored entry even though known_hosts is hashed and grep for the host name returns nothing.
  • Verify a new host key out-of-band by comparing ssh-keyscan output against a fingerprint read from the provider console.
  • Remove exactly one entry with ssh-keygen -R instead of truncating the file.
  • Pick an honest policy for automation: accept-new, a per-host UserKnownHostsFile, or an SSH certificate authority.

Prerequisites

  • An OpenSSH client from the 8.x or 9.x series. Check with ssh -V. Two version cut-offs matter here: OpenSSH 8.8 disabled the ssh-rsa (SHA-1) signature algorithm by default, and DSA was disabled at compile time in 9.8 before being removed outright in 10.0. Both show up as connection failures against old servers, covered in Troubleshooting.
  • Shell access on the client where the error appears, and write access to ~/.ssh/known_hosts.
  • An out-of-band channel that can tell you the server’s real fingerprint: a provider serial or VNC console, a hypervisor console, a configuration management run log, or a colleague with existing access who did not reach the host over the same network path.
  • Examples use the host alias web1.example.com at 203.0.113.24. Substitute your own.
  • If you have not set up key-based login yet, SSH key authentication covers the client side that this check protects.

How it works

SSH authenticates in both directions. You prove who you are with a password or a key; the server proves who it is with a host key — a keypair generated once, usually at first boot, and stored in /etc/ssh/ssh_host_*_key. During the key exchange the server signs a transcript with its private host key, and the client checks that signature against the public half. That proves the server holds the key. It says nothing about whether that key is the right one.

The right one is decided by trust on first use, or TOFU. The first time you connect, the client has no reference, so it shows you the fingerprint and asks you to vouch for it. When you say yes, the public key is appended to ~/.ssh/known_hosts and becomes the reference for every future connection. From then on the check is a lookup with three possible outcomes.

SSH host key verification path from client connect through the known_hosts lookup to the three outcomes: match, first contact prompt, and mismatch hard stop

A match is silent. No entry produces the TOFU prompt. A mismatch produces the loud banner and a hard stop — and the stop matters, because OpenSSH also disables password authentication and agent forwarding for that connection so you cannot hand a credential to whatever answered.

Two details make this confusing in practice. First, the lookup covers more than one file: ~/.ssh/known_hosts plus the system-wide /etc/ssh/ssh_known_hosts. If your organization distributes the system file, deleting your personal entry changes nothing. Second, entries are usually hashed. Upstream OpenSSH defaults HashKnownHosts to no, but Debian, Ubuntu and their derivatives ship HashKnownHosts yes in /etc/ssh/ssh_config, so the host name column looks like |1|hXk3...|Qd8...= and searching for web1.example.com finds nothing.

Step-by-step

1. Read the banner and classify the failure. The mismatch case prints a specific block. Reproduce it faithfully before acting on it:

ssh ops@web1.example.com
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    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:hNQ2mVQ0hkFmXK1oQ5X2Q0e1M7vJ2xZ0rTgN9pLdC4s.
Please contact your system administrator.
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:42
  remove with:
  ssh-keygen -f "/home/you/.ssh/known_hosts" -R "web1.example.com"
Host key for web1.example.com has changed and you have requested strict checking.
Host key verification failed.

Three things to extract: the fingerprint the server just sent, the file and line number on the Offending line, and the key type. Write the fingerprint down — you are about to compare it against a value from somewhere else.

2. Look at what you have stored. grep will not find it if the file is hashed, so use ssh-keygen -F, which understands the hashing:

ssh-keygen -F web1.example.com
# Host web1.example.com found: line 42
|1|hXk3JYd0Qm5r8sV2wL9tN1cB4aE=|Qd8pT2vXn6yR0mK5jS3fW7uH1gA= ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFq2Rk9nZ0LmT4vXcY7bJ1sQ8hP3wE6uD5aB0iO9kN2t

Add -l to print its fingerprint rather than the raw line:

ssh-keygen -lF web1.example.com
# Host web1.example.com found: line 42
256 SHA256:Kd4TzP1nB8vR3mQ6wX0jL5sA9cE2yU7hF4gI1oN6bT8 |1|hXk3JYd0Qm5r8sV2wL9tN1cB4aE=|Qd8pT2vXn6yR0mK5jS3fW7uH1gA= (ED25519)

You now have both halves of the disagreement: Kd4Tz... stored, hNQ2m... presented.

3. Ask what changed, before you ask whether it is safe. Most mismatches have a mundane cause, and knowing which one you are in tells you where to look for the truth: the host was reimaged and sshd generated fresh keys on first boot; the IP was released and handed to a different tenant; a container was recreated; a distribution upgrade regenerated a key type; a floating IP now points at a second machine whose keys were never synchronized. None of those excuse skipping verification — they only tell you which out-of-band source is likely to have the answer.

4. Read the expected fingerprint out-of-band. Get onto the server without SSH — the provider’s serial console, the hypervisor console, or a cloud-init log that printed the fingerprints at first boot — and print the key you actually have there:

for f in /etc/ssh/ssh_host_*_key.pub; do ssh-keygen -lf "$f"; done
256 SHA256:2fL8rN4kD6sX1wQ9tY3bV7mZ0aC5eG8hJ2pR6uI4oK0 root@web1 (ECDSA)
256 SHA256:hNQ2mVQ0hkFmXK1oQ5X2Q0e1M7vJ2xZ0rTgN9pLdC4s root@web1 (ED25519)
3072 SHA256:9pQ7bXcR2dV5yTn0LmA8fJ3kW1sE6uH4iO2gY7zB1xM root@web1 (RSA)

The number in front is the key size in bits, then the SHA-256 fingerprint in base64 — 43 characters, no padding. Same format the client printed, so the two compare directly.

5. Confirm what the network is actually presenting. ssh-keyscan collects host keys without authenticating, which makes it safe to run against a host you do not trust yet:

ssh-keyscan -t ed25519 web1.example.com
# web1.example.com:22 SSH-2.0-OpenSSH_9.6p1 Ubuntu-3ubuntu13.5
web1.example.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHb7wR2mK9pQ0nT5vX1jL8sE3cY6uA4gD2iO7kB0zM1f

The # line goes to standard error, so discard it and pipe the key straight into ssh-keygen -lf - to get a comparable fingerprint:

ssh-keyscan -t ed25519 web1.example.com 2>/dev/null | ssh-keygen -lf -
256 SHA256:hNQ2mVQ0hkFmXK1oQ5X2Q0e1M7vJ2xZ0rTgN9pLdC4s web1.example.com (ED25519)

6. Compare the two strings, all 43 characters. They match here, so the key change is real and benign. If they do not match, you have a live problem: something between you and the console is presenting a different key. Stop at that point.

Decision tree for an SSH host key mismatch: rebuild check, out-of-band fingerprint verification, then either ssh-keygen -R or escalation

7. Remove the one entry that is wrong. ssh-keygen -R deletes every entry for that host, hashed or not, and keeps a backup:

ssh-keygen -R web1.example.com
# Host web1.example.com found: line 42
/home/you/.ssh/known_hosts updated.
Original contents retained as /home/you/.ssh/known_hosts.old

If you connect by address as well as by name, both are separate entries and both need removing:

ssh-keygen -R 203.0.113.24
# Host 203.0.113.24 found: line 43
/home/you/.ssh/known_hosts updated.
Original contents retained as /home/you/.ssh/known_hosts.old

Do not open the file in an editor and delete line 42 by number. If the file has been rewritten since the error, the line number has moved.

8. Reconnect and confirm the fingerprint you already verified. You get the TOFU prompt again, and this time you can answer it with evidence:

ssh ops@web1.example.com
The authenticity of host 'web1.example.com (203.0.113.24)' can't be established.
ED25519 key fingerprint is SHA256:hNQ2mVQ0hkFmXK1oQ5X2Q0e1M7vJ2xZ0rTgN9pLdC4s.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

The [fingerprint] option is worth knowing: paste the fingerprint you obtained out-of-band instead of typing yes, and OpenSSH accepts only if it matches what the server sent. It turns the prompt into a check rather than a rubber stamp.

9. Decide the policy for hosts you cannot prompt on. For CI runners and short-lived instances, pin the setting to the hosts it belongs to rather than globally. In ~/.ssh/config:

Host build-*.ci.internal
    StrictHostKeyChecking accept-new
    UserKnownHostsFile ~/.ssh/known_hosts.ci

Host *.example.com
    StrictHostKeyChecking yes
    UserKnownHostsFile ~/.ssh/known_hosts

accept-new adds unknown hosts without a prompt but still refuses a changed key — it removes the interactive block for first contact while keeping the protection that matters. StrictHostKeyChecking no is a different setting entirely: it accepts changed keys too, and it is what turns a compromised jump host into a silent problem. The pair StrictHostKeyChecking no plus UserKnownHostsFile /dev/null is the copy-paste that disables host verification completely; if you find it in a pipeline, treat it as a finding.

10. Stop managing keys per-workstation. At more than a handful of hosts, the durable fix is a certificate authority. Sign each server’s host key with a CA, and every client trusts one line in known_hosts instead of one line per server:

@cert-authority *.example.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILp0Xk9dN3mR7tQ2vY5bC8wE1sJ4uH6gA0iO2kT9zF3n
@revoked web1.example.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFq2Rk9nZ0LmT4vXcY7bJ1sQ8hP3wE6uD5aB0iO9kN2t

Rebuild a server, sign the new key, and no client sees a mismatch. The @revoked marker does the opposite: it makes a specific key refuse to connect even if some client still has it stored, which is how you retire a key after an incident. SSH certificate authority setup covers issuing and rotating those certificates.

Validate it works

Reconnect and confirm the check is silent — no prompt and no banner is the correct result:

ssh -o StrictHostKeyChecking=yes ops@web1.example.com true; echo "exit=$?"
exit=0

Confirm the stored entry now matches what the network presents, in one comparison:

diff <(ssh-keygen -lF web1.example.com | awk 'NR==2 {print $2}') \
     <(ssh-keyscan -t ed25519 web1.example.com 2>/dev/null | ssh-keygen -lf - | awk '{print $2}')

Empty output means the two fingerprints are identical. Finally, check that exactly one entry exists and no stale duplicate survived:

ssh-keygen -F web1.example.com | grep -c ssh-ed25519
1

Troubleshooting

  • grep finds nothing in known_hosts. The file is hashed, so the host name is not there in plain text. Use ssh-keygen -F <host> to search and ssh-keygen -R <host> to remove; both hash the argument before comparing. The hashing exists so that a stolen known_hosts does not hand an attacker an inventory of every host you reach, which is a reasonable trade on a laptop. Set HashKnownHosts no if you want a readable file, or ssh-keygen -H to hash an existing plain one in place.
  • You removed the entry and the warning came back identically. The offending key is in the system-wide file, not yours. Re-read the Offending line: it names the exact file, and /etc/ssh/ssh_known_hosts needs root to edit. Remove it with ssh-keygen -f /etc/ssh/ssh_known_hosts -R <host> and fix whatever distributes that file, or your next configuration management run will put it straight back.
  • A CI runner prompts forever, or fails with no output. There is no TTY to answer the TOFU prompt, so the job hangs or dies. The fix is to seed the file before the job connects: run ssh-keyscan -t ed25519 <host> >> ~/.ssh/known_hosts in a setup step, ideally against a fingerprint you committed to the repository, or hand the runner a known_hosts file from your secret store. StrictHostKeyChecking accept-new is the acceptable shortcut when the runner is on a network you control; disabling checking entirely is not.
  • Unable to negotiate ... no matching host key type found. This is not a mismatch, it is a refusal to speak. Modern clients no longer offer ssh-rsa (SHA-1) or DSA, and an old server has nothing else. The message names what the server offered:
Unable to negotiate with 198.51.100.9 port 22: no matching host key type found. Their offer: ssh-rsa

Re-enable the algorithm for that one host, and treat it as a ticket to upgrade the server rather than a fix:

Host legacy-switch.example.com
    HostKeyAlgorithms +ssh-rsa
    PubkeyAcceptedAlgorithms +ssh-rsa

Confirm what your client will negotiate at all with ssh -Q HostKeyAlgorithms. DSA has no equivalent escape hatch on a current client: 9.8 stopped compiling it in by default and 10.0 deleted the code, so a stock modern client cannot be talked into it — you need a build with DSA compiled in, an older client, or a newer server.

  • The mismatch appears intermittently, on roughly half of connections. A load balancer or DNS round-robin is putting you on different backends, each with its own host keys, so the entry is correct half the time. Confirm it by scanning repeatedly and watching the fingerprint alternate. The fix is to give the backends one identity: copy a single set of /etc/ssh/ssh_host_*_key files to every node behind the address, or sign them all with a CA and trust the CA instead.
  • ssh still refuses after ssh-keygen -R. Confirm which files are actually in play with ssh -G <host> | grep -i knownhosts. A per-host alias can point at a file you forgot about; SSH client config and host aliases covers how those blocks resolve.

When this is the wrong approach

The whole procedure above assumes you can reach an authoritative answer about what the server’s key should be. If you cannot — no console access, no cloud-init record, no configuration management inventory, nobody who can read the fingerprint off the machine — then there is no verification step, only a guess. Deleting the entry at that point does not resolve the warning; it silences it. The right move is to stop, leave the entry in place as evidence, and escalate to whoever owns the host.

The same applies when the answer comes back wrong. A fingerprint that does not match the console is not a puzzle to work around: assume the session is being intercepted, do not retry with password authentication, and treat any credential you have already sent to that address as exposed. Rotate it.

Two more cases where this article is not the tool. If you are hitting the prompt on hundreds of ephemeral hosts — autoscaling groups, preview environments, throwaway containers — you have an identity problem rather than a known_hosts problem, and per-host entries will never converge. Go to a certificate authority. And if the error is Permission denied (publickey), this is the wrong page entirely: that is your key failing, not the server’s.

The authoritative references are the OpenSSH ssh_config manual for StrictHostKeyChecking, UserKnownHostsFile, HashKnownHosts and HostKeyAlgorithms, and the ssh-keygen manual for the -F, -R, -l and -H flags and the known_hosts file format.

If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.