How to Fix the Host Key Has Changed Error in SSH

Imagine having a special key that unlocks the door to a remote computer, known as a server. This "key" is an SSH key, used to securely connect to that server over the internet. Your computer keeps a list of these keys in a secure file. However, sometimes you may encounter an error stating, "Host key has changed". Here's a straightforward guide to understanding and fixing this issue.
When you connect to a server for the first time, your computer saves a "fingerprint" of the server’s key. This fingerprint acts like a unique identifier for the server. On subsequent connections, your computer checks if the fingerprint matches the saved one. If it doesn’t, you’ll see the Host key has changed error.
Think of it as having a photo of a friend’s house. If the house suddenly looks different when you visit again, you’d be confused. Similarly, this error means your computer is saying, “This doesn’t look like the server I remember!”
Several reasons could cause the server’s fingerprint to change:
Your computer keeps fingerprints of previously connected servers in the known_hosts
file, located in the .ssh
directory:
/Users/YourName/.ssh/known_hosts
(Mac) or /home/YourName/.ssh/known_hosts
(Linux)..ssh
folder is hidden, so you may need to enable "show hidden files" in your file manager. C:\Users\YourName\.ssh\known_hosts
. Before proceeding, confirm with the server administrator that the key change is legitimate. This ensures you’re not connecting to a compromised or malicious server.
ssh-keygen
command to delete the old key for the server. Replace <server's address>
with the server’s hostname (e.g., example.com
) or IP address (e.g., 192.168.1.100
):ssh-keygen -R <server's address>
For example:ssh-keygen -R example.com
ssh-keygen -R 192.168.1.100
This removes the old fingerprint from the known_hosts
file. ssh user@<server's address>
yes
and press Enter. Imagine you’re trying to connect to a server at 192.168.1.100
and encounter the Host key has changed error. Here’s how to resolve it:
ssh-keygen -R 192.168.1.100
ssh [email protected]
yes
and press Enter.