Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

SSH warns that the remote host identification has changed

ErrorWARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

Short answer

ssh compared the server's host key with the one saved in known_hosts and they differ. A rebuilt or reinstalled server, or a reused IP address, is the usual reason, but a man-in-the-middle looks the same. Get the real fingerprint from a trusted channel, compare it, and only then remove the old entry with ssh-keygen -R.

  • Applies to OpenSSH 9.x and 10.x client
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Do not delete anything yet. Note the line number in the Offending key message, then fetch the fingerprint the server shows right now and compare it with a value you got another way: the provider console, the server itself, or the person who rebuilt it.

    # fingerprint the server presents now
    ssh-keyscan -t ed25519 server.example.com 2>/dev/null | ssh-keygen -lf -
    
    # the entry saved in known_hosts earlier
    ssh-keygen -F server.example.com
  2. 2Match what you see, then jump to its fix

    1. 1You know the machine was recreated, resized onto new hardware or reinstalled, and the fingerprint from a trusted channel matches what ssh-keyscan returns.The server was rebuilt, reinstalled or restored from an imageFix
    2. 2The offending line is for an IP address or a short alias, and you recently moved the domain, got a new cloud server that reused an address, or switched networks.The name or address now belongs to a different machineFix
    3. 3No one changed the server, the trusted fingerprint differs from what the network shows, and the warning appears only from one network (cafe, hotel, office) or after a DNS change you did not make.Nothing was rebuilt and the fingerprint does not match: treat it as an attack until proven otherwiseFix

What does the error look like?

  • WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
  • IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
  • Offending ED25519 key in /home/user/.ssh/known_hosts:12
  • Host key verification failed.

Cause 1 of 3

The server was rebuilt, reinstalled or restored from an image

1Check

A fresh OS install generates new host keys, so the same name or IP now answers with a different key. Do not trust the network to confirm that: read the fingerprint on the machine itself (provider web console, or an already trusted session) and compare it character by character with what the client sees.

# on the server, through the console
ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub

# on your machine
ssh-keyscan -t ed25519 server.example.com 2>/dev/null | ssh-keygen -lf -

2Fix

Only after the two fingerprints match, remove the old entry for that name (and for the IP if it was saved separately), then connect again and check that the fingerprint ssh prints is the one you verified before answering yes.

ssh-keygen -R server.example.com
ssh-keygen -R 203.0.113.10
ssh server.example.com      # compare the printed fingerprint, then type yes

3Verify

The connection opens with no warning, and known_hosts now holds one entry for the new key.

ssh-keygen -F server.example.com

Confirmed by man.openbsd.org

Did this fix it?

Cause 2 of 3

The name or address now belongs to a different machine

1Check

known_hosts stores a key per name and per address you used. A cloud IP that was released and given to someone else, a DNS record pointed at a new host, or a home router address that now means another device all produce this warning. Look up where the name resolves today and which entries exist.

dig +short server.example.com
ssh-keygen -F 203.0.113.10
ssh-keygen -F server.example.com

2Fix

Confirm that the machine now behind the name is the one you meant (provider dashboard, DNS records), verify its fingerprint as above, then remove just the stale entries. Next time, connect by name so one stable entry is used, and use ssh-keygen -R for each stale name or address.

ssh-keygen -R 203.0.113.10
ssh-keygen -R old-alias
ssh server.example.com      # compare the printed fingerprint with the verified one before typing yes

3Verify

ssh shows a new-host prompt (not the warning) and the fingerprint matches the trusted one.

ssh -v server.example.com 2>&1 | grep -iE 'host key|fingerprint'

Confirmed by man.openbsd.org

Did this fix it?

Cause 3 of 3

Nothing was rebuilt and the fingerprint does not match: treat it as an attack until proven otherwise

1Check

A different key for a host that was not touched means something else is answering: an interception on the path, a hijacked DNS record, or a wrong destination. Compare from another network (for example a phone hotspot) and check DNS from two resolvers.

ssh-keyscan -t ed25519 server.example.com 2>/dev/null | ssh-keygen -lf -
dig +short server.example.com @1.1.1.1
dig +short server.example.com @9.9.9.9

2Fix

Do not log in, do not type a password, and do not remove the old entry. Leave the default checking on, which refuses a changed key. Report it to the server's owner or provider, review your DNS records and registrar account, and if a login did go through, rotate the credentials and keys used on that host.

# make sure you are on the default (strict for changed keys) behaviour
ssh -G server.example.com | grep -i stricthostkeychecking

3Verify

From a trusted network the fingerprint matches the one on the server console again, or the provider confirms the change.

ssh-keyscan -t ed25519 server.example.com 2>/dev/null | ssh-keygen -lf -

Confirmed by man.openbsd.org

Did this fix it?

How do I stop it coming back?

  • Write down the host key fingerprints of each server when you create it, in the same place you keep its console login.
  • When you rebuild on purpose, restore the old /etc/ssh/ssh_host_* files or announce the new fingerprints to the people who connect.
  • Never switch off host key checking to get past the warning; it is the only thing that tells you the server is the one you meant.
  • Connect by a stable DNS name instead of a changing IP address.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.