How do I fix "REMOTE HOST IDENTIFICATION HAS CHANGED" in SSH?
The server now shows a different host key than the one saved in your known_hosts file. Check the new fingerprint with the server's owner, remove the old entry with ssh-keygen -R, and connect again.
Last updated
Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b
container image), .
You connect to a server you have used before, and ssh stops with a large warning that the host identification has changed. It means the server’s host key is not the one saved in ~/.ssh/known_hosts. Confirm the new key with whoever runs the server, delete the old entry with ssh-keygen -R, and connect again.
What the warning says
Every SSH server has a host key. The first time you connect, ssh saves it in known_hosts. On every later connection it compares the key the server sends with the saved one. This is the check that tells you that you are talking to the same machine as last time.
Here is the check failing. The saved entry for web-1 is an old key, and the server now presents another one:
$ ssh deploy@web-1 hostname
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ 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:GnVT4RJZ9DlBJr4sGmScUa4LE+bwCU/ofYT5L04FpEE.
Please contact your system administrator.
Add correct host key in /home/dev/.ssh/known_hosts to get rid of this message.
Offending ED25519 key in /home/dev/.ssh/known_hosts:1
remove with:
ssh-keygen -f '/home/dev/.ssh/known_hosts' -R 'web-1'
Host key for web-1 has changed and you have requested strict checking.
Host key verification failed.
Read three lines of it. The line that starts with “The fingerprint” is the key the server sent now. “Offending ED25519 key in” names the file and the line number of the old entry. The last two lines say that ssh refused to go on, which is what you want.
Check the new key before you trust it
A changed key has two ordinary causes and one bad one. The server was reinstalled, or its keys were regenerated, or a new machine took over the address. Or someone is answering in the server’s place. The warning looks the same in all three cases.
Ask the owner of the server for the fingerprint, or read it yourself from a console you already trust, such as your provider’s web console. On the server, ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub prints it. You can ask the server for the key it presents right now and fingerprint that:
$ ssh-keyscan -t ed25519 web-1 2>/dev/null | ssh-keygen -lf -
256 SHA256:GnVT4RJZ9DlBJr4sGmScUa4LE+bwCU/ofYT5L04FpEE web-1 (ED25519)
Compare the SHA256: value with the one you were given. If they match, go on. If they do not, stop and tell the owner. Do not connect to see what happens.
Remove the old key
ssh-keygen -R deletes every entry for a host name from your known_hosts file. It also keeps the old file next to it:
$ ssh-keygen -R web-1
# Host web-1 found: line 1
/home/dev/.ssh/known_hosts updated.
Original contents retained as /home/dev/.ssh/known_hosts.old
Now connect once more. The server is new to ssh again, so it asks whether to trust the key. In a script, or here where nobody can answer, StrictHostKeyChecking=accept-new accepts a key for a host that has no entry yet and still refuses a key that changed:
$ ssh -o StrictHostKeyChecking=accept-new deploy@web-1 hostname
Warning: Permanently added 'web-1' (ED25519) to the list of known hosts.
web-1
Normally you see the question “Are you sure you want to continue connecting” with the fingerprint instead. Answer it by comparing that fingerprint with the one you checked a minute ago, not by typing yes out of habit.
ssh-keygen -F finds an entry, and with -l it prints the fingerprint ssh saved, so you can see that it is the one you checked:
$ ssh-keygen -lF web-1
# Host web-1 found: line 1
web-1 ED25519 SHA256:GnVT4RJZ9DlBJr4sGmScUa4LE+bwCU/ofYT5L04FpEE
From now on the connection works without a question:
$ ssh deploy@web-1 hostname
web-1
Mistakes that bring the warning back
You removed the host name but not the address. ssh saves a key under the name you typed, so web-1 and 203.0.113.10 are two entries for the same server. Run ssh-keygen -R for each name and address you connect with.
The server listens on another port. For any port but 22 the entry is written as [host]:port, and ssh-keygen -R web-1 does not match it. Name the port in the same form:
$ ssh-keygen -R '[web-1]:2222'
Host [web-1]:2222 not found in /home/dev/.ssh/known_hosts
The message above is what you see when nothing matches, and the file stays as it was.
Your file is hashed. When HashKnownHosts is on, which many distributions set by default, the host names in known_hosts are stored as hashes and you cannot find the line with grep. ssh-keygen -R and ssh-keygen -F understand hashes, so use them and not an editor.
Another file holds the key. The warning names the file. It can be a system file such as /etc/ssh/ssh_known_hosts, or a different file set with UserKnownHostsFile. Remove the entry from the file that is named, with ssh-keygen -f <file> -R <host>.
You turned checking off. StrictHostKeyChecking=no and UserKnownHostsFile=/dev/null make the warning go away for good, and they make every connection as unprotected as the very first one. They belong in a throwaway test container, not in ~/.ssh/config or a deploy script.
In Valakes
Valakes stops a connection in the same situation. It shows the trusted fingerprint and the new one, and it has no button that continues anyway. You clear the stop in Settings, under Known hosts. The help article below walks through that screen. The rules on this page are the generic OpenSSH ones, for the ssh command in a terminal.
Questions
Is this warning always an attack?
No. A reinstalled server, a rebuilt container and a cloud address given to a new machine all produce it, and they are far more common than an attack. You cannot tell them apart from the warning, so ask the owner of the server.
Can I just edit known_hosts by hand?
Yes. The file is plain text, one entry per line, and the warning names the line of the old key. ssh-keygen -R does the same job and keeps a copy of the old file as known_hosts.old.
Why does the warning name an IP address and not my host name?
ssh saves the key under the name you typed. If you connect by address one day and by name the next, there are two entries, and each needs its own ssh-keygen -R.
Is StrictHostKeyChecking=no a fix?
It silences the warning and also the protection. Someone between you and the server would get in without a sound. Remove the one old key instead.
References
- ssh-keygen(1) on Ubuntu 24.04, the -R, -F and -l options manpages.ubuntu.com
- ssh(1) on Ubuntu 24.04, the section on host key verification manpages.ubuntu.com
- ssh_config(5), StrictHostKeyChecking and UserKnownHostsFile manpages.ubuntu.com
- OpenSSH manual pages www.openssh.org
Related articles
The app is free.
Get notified at launchmacOS is coming to the Mac App Store. iOS and Android later.