Contents
- What exactly does the warning say?
- Why does a host key change?
- When should you stop and not continue?
- How do you find the server's real fingerprint?
- How do you compare the fingerprints?
- How do you replace the old entry safely?
- What not to do
- How do you avoid the warning during planned changes?
- How does CH-J Tools handle host keys?
- Frequently asked questions
What exactly does the warning say?
Every SSH server has its own host key, which it uses to prove its identity on every connection. On the first connection your client stores the key's fingerprint; OpenSSH keeps it in ~/.ssh/known_hosts. On every later connection it compares what the server presents with what it has stored. If they don't match, it drops the connection before you send a password or use a key.
This is what OpenSSH prints. Three details matter most: the key type (ED25519 here), the new fingerprint and the line number of the old entry after the colon.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ 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:Jslkl9VoRw8iV+0Z08BS5vVm5X1d1GuLZsegTU2dZU8.
Please contact your system administrator.
Add correct host key in /home/user/.ssh/known_hosts to get rid of this message.
Offending ED25519 key in /home/user/.ssh/known_hosts:12
Host key verification failed.The client has no way of telling whether the server was reinstalled or whether an attacker has placed themselves between you and the server. Both look the same on the network. Treat the warning as an open question, not as an error to click away. Let the connection through only after you have found out from another source which key is correct.
Why does a host key change?
Most of the time it is routine administration. A host key changes, or you start getting a different key, mainly in these situations:
- The server was reinstalled or rebuilt from a clean installation. A fresh OpenSSH install generates new keys.
- The virtual machine was created from an image or template. Cloud images with cloud-init usually delete the old keys and generate new ones on first boot.
- The IP address or DNS name now belongs to a different machine, for example after a server swap, a DHCP change or a standby node taking over the address.
- A load balancer or NAT sits in front of the service and each node behind it has different keys.
- An administrator replaced the keys on purpose, for example after a security incident or when moving from RSA to Ed25519.
- A different service now answers on the same address and port, for example SSH in a container or on an appliance instead of the host system.
When should you stop and not continue?
An attacker who can intercept traffic (public Wi-Fi, a compromised router, spoofed DNS) triggers exactly the same warning. Close the connection immediately and do not type a password if any of the following is true:
- You know of no reinstall, migration or key change, and the administrator has not confirmed one.
- The warning appears on one network only, for example in a hotel, while from home or over VPN the same server still shows the old key.
- Only one of the servers you regularly use has changed, with no reasonable explanation.
- The server suddenly presents a weaker or older key type than the one you had stored.
With password authentication, an attacker in the middle receives your password in plain text. With key authentication your private key does not leave your machine, but the attacker can relay the session and read what you do in it. In neither case should you connect until you have verified the change.
How do you find the server's real fingerprint?
You need to get the correct fingerprint through a path that does not go over the suspicious SSH connection. Good options are the hypervisor console (Proxmox, VMware, Hyper-V), the cloud provider's web or serial console, physical access to the machine, or an administrator who sends you the fingerprint over another channel.
On a Linux server, list the fingerprints of all host keys. The output uses the same SHA256 format as the warning:
for key in /etc/ssh/ssh_host_*_key.pub; do sudo ssh-keygen -lf "$key"; done
256 SHA256:Jslkl9VoRw8iV+0Z08BS5vVm5X1d1GuLZsegTU2dZU8 root@server (ED25519)
3072 SHA256:7n3Qd0... root@server (RSA)On Windows Server with OpenSSH the host keys live in C:\ProgramData\ssh. Run PowerShell as administrator:
ssh-keygen -lf C:\ProgramData\ssh\ssh_host_ed25519_key.pubCompare the same key type. If the warning names ED25519, compare it with the fingerprint of ssh_host_ed25519_key.pub, not with the RSA or ECDSA key. Each type has a different fingerprint.
How do you compare the fingerprints?
Compare the fingerprint from the console with the one in the warning character by character, or at least the whole beginning and end. As a cross-check you can see from the client what the server presents right now. ssh-keyscan alone proves nothing, because it only shows what the network sent you. It is useful only for comparison with the value from the console:
ssh-keyscan -t ed25519 -p 22 server.example 2>/dev/null | ssh-keygen -lf -If the console fingerprint matches the one in the warning, the change is legitimate and you can move on to the next step. If it differs, someone or something else is on the path between you and the server. Do not use the connection; check DNS, routing and the network you are connecting from.
Some tools show a SHA-256 fingerprint as a 64-character hexadecimal string instead of base64 with the SHA256: prefix. It is the same checksum written differently. You can get the hexadecimal form from the public key like this (on macOS use shasum -a 256 instead of sha256sum):
awk '{print $2}' /etc/ssh/ssh_host_ed25519_key.pub | base64 -d | sha256sumHow do you replace the old entry safely?
Edit known_hosts only after you have confirmed that the fingerprints match. Do not delete the whole file and avoid editing it by hand; ssh-keygen has precise options for this.
- Check which entries you have stored for the server. The -F option also finds hashed entries where the server name is not visible in the file.
- Remove the entry for the server name. A non-standard port has its own entry in the form [name]:port in square brackets.
- If you also connect by IP address or another alias, remove those entries too. Otherwise the warning will come back.
- Connect again, compare the offered fingerprint with the verified value, and only then accept the connection.
ssh-keygen -F server.example
ssh-keygen -R server.example
ssh-keygen -R '[server.example]:2222'
ssh-keygen -R 203.0.113.10
ssh user@server.examplessh-keygen -R saves the previous state as ~/.ssh/known_hosts.old, so you can go back if you make a mistake. The line number in the message (known_hosts:12) is mainly for orientation. You can delete that line with another tool, but it is easy to remove the wrong entry by accident.
What not to do
You will often find quick advice online that does silence the warning, but also switches off the protection it exists for:
- Do not add StrictHostKeyChecking=no or UserKnownHostsFile=/dev/null to ~/.ssh/config for all servers. The client will then accept any key and you will never detect an attack.
- Do not delete the whole known_hosts file. You lose the protection for every server and will have to verify each fingerprint again on the next connection.
- Do not accept a new key just because you are in a hurry or because "it worked last time".
- Do not put a key into known_hosts that you obtained over the same untrusted connection, for example ssh-keyscan output without a comparison.
Short-lived test machines on an isolated lab network can be an exception. Even there, it is better to limit the relaxed settings to a specific address range in a Host block than to enable them everywhere.
How do you avoid the warning during planned changes?
Most unexpected warnings can be avoided if the key change is planned in advance.
- For a planned reinstall where the server should stay the same machine, back up /etc/ssh/ssh_host_* and restore the files afterwards with the same owner and permissions (private keys 600, root). The server then identifies itself to clients as before.
- Do the opposite for clones. Every machine created from a template needs its own keys; otherwise they share one identity and compromising one exposes all of them.
- Announce key changes to users in advance and send them the new fingerprint over another channel, such as an internal wiki or a signed e-mail.
- For larger environments consider SSH host certificates. The client then trusts a signing authority (an @cert-authority line in known_hosts) instead of individual keys, so replacing a key on the server does not trigger the warning.
- With UpdateHostKeys, OpenSSH can accept additional host keys from the server after a successful login. A new key can reach clients before the old one is removed.
- SSHFP records in DNS (generated by ssh-keygen -r) with VerifyHostKeyDNS only make sense if the zone is signed with DNSSEC.
On a cloned Linux machine you can create new keys from the console like this. On Ubuntu and Debian the service is called ssh, on RHEL and its derivatives sshd:
sudo rm /etc/ssh/ssh_host_*
sudo ssh-keygen -A
sudo sshd -t && sudo systemctl restart sshHow does CH-J Tools handle host keys?
CH-J Tools does not use OpenSSH's known_hosts file. It stores the SHA-256 fingerprints of host keys in its encrypted vault, which is unlocked together with the app. When a server presents a key that differs from the stored one, the app does not connect and shows both the known and the new fingerprint in a dialog.
- The entry is replaced only after you confirm it explicitly. Cancelling the dialog changes nothing.
- Your confirmation applies only to the key you saw in the dialog. If the stored key changes in the meantime, the app refuses the replacement and verification starts again.
- When connecting through an intermediate server (jump host), the key of every server on the route is checked separately and the dialog says which one changed.
- If a server's key changes during a running file transfer, the transfer stops and is not completed against a different machine.
The app shows the fingerprint as a 64-character hexadecimal SHA-256 string. To compare it with the server console, use the base64 -d and sha256sum command from the section above. The app cannot verify a new fingerprint for you; checking it over another channel is still up to you.
Frequently asked questions
Is a host key change warning always an attack?
No. Most often the server was reinstalled or newly created. The client cannot tell the difference, though, so verify every unexpected change with a fingerprint obtained outside the SSH connection.
Is it enough to delete the line from known_hosts?
Technically yes, but only after you have verified the fingerprint. Without verification you simply accept whatever key is presented on the next connection. Use ssh-keygen -R to remove it; it keeps a known_hosts.old backup.
Why does the warning come back after I removed the entry?
Usually because you have another entry for the IP address, a different alias or a non-standard port in the form [name]:port. Find them with ssh-keygen -F and remove each one. If the server sits behind a load balancer with different keys on each node, the keys need to be unified or host certificates used.
Is ssh-keyscan a safe way to get the new key?
Not on its own. It shows whatever the network presents, including a possible attacker. It is only useful for comparison with a fingerprint you know from the console or from the administrator.
Do I need to set up key login again after a host key change?
No. The host key identifies the server; your user key identifies you. The authorized_keys file on the server is not affected by a host key change unless the server was reinstalled without restoring its data.
Detailed documentation
OpenSSH: ssh_config (StrictHostKeyChecking, UpdateHostKeys) →