Every home lab eventually produces this wall of at-signs: WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! followed by IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Usually the cause is boring, a reinstalled Raspberry Pi or a router that handed an old IP address to a new machine. Occasionally it is the attack the message describes. The common fixes found online range from correct to genuinely dangerous, and a test with two local SSH servers sharing one address, run with OpenSSH 9.6, shows which is which.
What the client remembers
known_hosts
The first time an SSH client connects to a server, the server proves its identity with a host key, and the client stores that key in ~/.ssh/known_hosts. On every later connection the client checks that the server presents the same key. If it does, the connection proceeds silently. If it does not, the client refuses and prints the warning.
For the test, a server was started on port 2222 with one Ed25519 host key, and a client connected with StrictHostKeyChecking=accept-new. It printed Warning: Permanently added '[127.0.0.1]:2222' (ED25519) to the list of known hosts. and connected.
Hashed entries
The stored line did not start with the address. It started with |1| followed by a long string of characters. The ssh_config manual explains the HashKnownHosts option: when enabled, hostnames are hashed before being written, and “they do not visually reveal identifying information if the file’s contents are disclosed.” The manual lists the default as no, but the Ubuntu system used for the test sets HashKnownHosts yes in its system-wide /etc/ssh/ssh_config, which is why the file was unreadable by eye. It also means searching the file with grep for a hostname finds nothing.

Reproducing the warning
Same address, new key
The test then stopped the first server and started a second one on the same address and port with a freshly generated host key, the same thing that happens when a machine is reinstalled. Connecting again produced the full warning:
@ 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:2TbUH2hDUu+y5UctpBSJHvtyUx7n4WyRFTiRRdm1Nco.
Offending ED25519 key in /home/.../known_hosts:1
Host key verification failed.
The client exited with status 255 and never sent a username or key. That is the protection working as designed.
accept-new does not accept changes
That refusal happened even with StrictHostKeyChecking=accept-new, which some guides suggest as a convenience setting. The manual is explicit: with accept-new, SSH “will automatically add new host keys to the user’s known_hosts file” but “will not permit connections to hosts with changed host keys.” It trusts on first use and then holds the line, which is why it is a reasonable setting for scripts that connect to new machines.
Deciding whether the change is legitimate
Ask why the key would change
Before touching known_hosts, the useful question is whether there is an innocent explanation. A fresh operating system install, a replaced SD card, a new virtual machine on an old IP address, or a router that reassigned a DHCP lease to a different device all produce new host keys. If none of those happened, the warning deserves more attention, especially on a network the client does not control, such as hotel or café Wi-Fi.
Compare fingerprints out of band
The warning prints the fingerprint of the key the server just offered. The safe check is to get the real server’s fingerprint through some other channel, such as its console, a screen attached to it, or a trusted management interface, and compare. On the server, ssh-keygen -lf on the host public key prints its fingerprint; the ssh-keygen manual describes -l as “Show fingerprint of specified public key file.” In the test, running it on the second server’s key printed SHA256:2TbUH2hDUu+y5UctpBSJHvtyUx7n4WyRFTiRRdm1Nco, matching the warning exactly.
Running ssh-keyscan from the client returned the same fingerprint too, but that proves nothing about who is on the other end. A machine in the middle answers a key scan just as easily as it answers a login. Only a check that does not travel over the suspect network path settles the question.

Removing the old key the right way
ssh-keygen -R
Once the new key is confirmed, the old entry has to go. The warning itself suggests the command, and the manual describes -R as removing “all keys belonging to the specified hostname (with optional port number) from a known_hosts file.” In the test:
ssh-keygen -f ~/.ssh/known_hosts -R '[127.0.0.1]:2222'
printed # Host [127.0.0.1]:2222 found: line 1, updated the file and kept the original as known_hosts.old. Reconnecting added the new key and connected normally.
Why not edit the file by hand
Deleting the line numbered in the warning with a text editor works, but -R handles the details: it finds hashed entries, removes every key type for that host rather than just the one listed, and handles non-standard ports, which are stored in the bracketed [host]:port form. The companion option -F, which the manual says will “Search for the specified hostname … in a known_hosts file, listing any occurrences found,” is the way to confirm an entry exists when the file is hashed.
The dangerous shortcut
StrictHostKeyChecking=no
The most common shortcut on forums is to turn host key checking off. The manual describes what no does: new keys are saved automatically, and connections to hosts with changed keys “may proceed, subject to some restrictions.” The test showed what that means in practice. With the original key still remembered and the server switched back to a different key, a client run with -o StrictHostKeyChecking=no printed:
Offending ED25519 key in .../known_hosts:1
Password authentication is disabled to avoid man-in-the-middle attacks.
Keyboard-interactive authentication is disabled to avoid man-in-the-middle attacks.
connected-anyway
SSH blocked password logins, which protects a password from being typed into an impostor. But public key authentication went ahead and the command ran on a server whose identity had just failed verification. Any data or commands sent through that session went to whoever held the new key. Setting StrictHostKeyChecking no permanently in a config file, or piping known_hosts to /dev/null, removes the one check that tells a legitimate server from an impostor.
When skipping checks is defensible
There are narrow cases, such as throwaway virtual machines rebuilt dozens of times a day on an isolated network, where the risk is accepted on purpose. Those setups usually point UserKnownHostsFile at a separate file for those hosts only, so the main known_hosts keeps protecting everything else.

Verifying keys without trusting the first connection
Host certificates
The weak point of known_hosts is the first connection, when the client has nothing to compare against and simply trusts whatever key it is shown. OpenSSH offers a way around that. The sshd manual’s known_hosts format section describes a line marker, @cert-authority, “to indicate that the line contains a certification authority (CA) key.” A single CA line in known_hosts, covering a pattern such as *.home.lab, lets the client trust any host certificate that CA has signed. Reinstalled machines get a new key and a new signed certificate, and clients accept them without a warning and without a first-use leap of faith.
The same section describes the opposite marker: “Revoked keys are specified by including the ‘@revoked‘ marker at the beginning of the key line,” and such keys are never accepted. That is the tool for a host key known to be compromised, which simply deleting the line does not achieve.
Fingerprints in DNS
A second option publishes host key fingerprints as SSHFP records in DNS. The ssh_config manual’s VerifyHostKeyDNS entry “Specifies whether to verify the remote key using DNS and SSHFP resource records.” Set to yes, “the client will implicitly trust keys that match a secure fingerprint from DNS,” while fingerprints that are not secure are treated as if the option were set to ask. The default is no. For a home lab without signed DNS zones, host certificates are usually the more practical of the two.
Several keys for one name
The sshd manual adds one more detail that explains some confusing warnings: “It is permissible (but not recommended) to have several lines or different host keys for the same names.” Stale duplicate entries, often left over from copying known_hosts between machines, are one more reason to remove entries with ssh-keygen -R, which clears every key for the name, rather than by line number.
Avoiding the warning next time
Keep host keys across reinstalls
The cleanest prevention is to stop host keys from changing. Backing up the /etc/ssh/ssh_host_* files before reinstalling a server and restoring them afterwards gives the machine the same identity, and clients never notice. For machines provisioned from scratch, giving each one a fixed IP address or DNS name, and not reusing old addresses for new machines, avoids collisions in known_hosts.
Let SSH learn rotated keys
Newer OpenSSH versions can also rotate keys gracefully. The ssh_config manual describes UpdateHostKeys, which lets the client accept additional host keys that a server sends after authentication; it “supports graceful key rotation,” is enabled by default unless UserKnownHostsFile has been changed or VerifyHostKeyDNS is turned on, and needs OpenSSH 6.8 or later on the server. A server that adds a new key while still holding the old one can then switch over without ever triggering the warning.
Sources
- OpenBSD manual page, ssh_config(5): https://man.openbsd.org/ssh_config
- OpenBSD manual page, sshd(8), SSH_KNOWN_HOSTS FILE FORMAT: https://man.openbsd.org/sshd
- OpenBSD manual page, ssh-keygen(1): https://man.openbsd.org/ssh-keygen
