Skip to main content

Linux sysadmin basics: Troubleshooting known_hosts failures

SSH is easy to use, but when something causes your known_hosts to backfire on you, it can be frustrating. Here's how to fix this problem.
"p1120431" by generalising is licensed under CC BY-SA 2.0

SSH is easy to use and is generally trouble-free. But, there's this little file in your home directory that can become a rare pain point for this ubiquitous protocol and tool. The offending entity is the ~/.ssh/known_hosts file. If you didn't know about it, you should acquaint yourself now. This file will come up at some point in your career.

If you've ever seen the following message, you either can freak out that someone is attempting to hack you, or you can realize that DHCP or some other anomaly has done a dirt cheap dirty deed on you:

$ ssh

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 ECDSA key sent by the remote host is
Please contact your system administrator.
Add correct host key in /Users/khess/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /Users/khess/.ssh/known_hosts:4
ECDSA host key for has changed and you have requested strict checking.
Host key verification failed.

What has happened here is that you've attempted to connect to a system that no longer has the IP address A different system has that IP address and SSH is complaining that you might be getting hacked. The reality is often much less exciting. Your known_hosts file may not sync with actual host reality. 

Multiple things can cause this IP address musical chairs game. One of the systems could have been down for an extended period of time and when another's lease was up, it grabbed the next available IP address. A patching event and subsequent reboot caused some of your systems to acquire different IP addresses. The system could have been reinstalled and a new identity generated, or perhaps someone wised up and gave static IP addresses to your server systems. (It's my opinion that all servers should have static IP addresses. The same goes for any support components, such as routers, switches, access points, IoT devices, cameras, conference room systems, and even printers. Only end-user devices should have the dubious privilege of acquiring random IP addresses. I have my reasons for this strong opinion, which I'll share in another article later.)

Regardless of how it happened, the system you were interested in connecting to has a new IP address. One that is also known to your system, but with a different fingerprint. 

The aforementioned known_hosts file is a simple text file that lives inside a hidden directory (.ssh) in your home directory. To view its contents, enter the following command:

$ cat .ssh/known_hosts ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMJWpXf8Ou/XJ1Q5YcIS5Me4GgN1F7AYHbvIPayfGqwd90kvhQygrPCYEqVdw44lEOIZe+DEKP4F6Otdt38yf68= ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBJRNTZm8M9IYYN3uJL/yCPv4EJRMDZkkcnt1CY8L6OR84wstYdhqtNqV0v/LQiz/AoRbHKxPTdjXfIOrc1vYDHc=

ken, ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBB53lh2RyFiMfJFKBkLa643lS4GGILG6oVGq33KyBSgaDU2ZHryYak1FU1HcPa6Xb/xYqEUugv9cOXFRwajbrmg= ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMGjMsJPX4bfdEnVy59Uf2VhH1oAt7Zemd5bfSoSGxX69HBcjkekO/LkEZUlhVplBlHFqJBqs2gWYp3zVDCTwr4= ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBIkK9WylcZcgjT7gUz9DMLehUBApION1CLiBVc3Pa/YfAbIDUlIWta12zL5B12RmBXgoYvntdW5rcowPiqjL0/o=

The easy solution to this problem is to remove the known_hosts file and allow new keys to be generated for each host you connect to. For this demonstration, I remove the entry from the known_hosts file and then connect to the new host at that address, so you can see a comparison of what happens in the process. First, remove a known_hosts entry with the ssh-keygen command:

$ ssh-keygen -R -f .ssh/known_hosts

 Host found: line 4
.ssh/known_hosts updated.
Original contents retained as .ssh/known_hosts.old

The original entry is saved to .ssh/known_hosts.old in case it's needed in the future.

After removing the entry from the  known_hosts file, I attempted the connection again to host

$ ssh
The authenticity of host ' (' can't be established.
ECDSA key fingerprint is SHA256:I02UyJs2vS0ym4jWn5upAWZDqwu5RjMg4aM9hPq8G1k.
Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '' (ECDSA) to the list of known hosts.

You can see that the key I added now matches the one listed in the error at the beginning of the article. To verify a host's key, issue the following command on the remote host:

$ ssh-keygen -lf /etc/ssh/
256 SHA256:I02UyJs2vS0ym4jWn5upAWZDqwu5RjMg4aM9hPq8G1k no comment (ECDSA)

Wrapping up

Unless you use static IP addresses and your systems never change, you'll have this problem at some point in your career. It happens to me all of the time because I create and destroy virtual machines several times a month. In fact, it happens so often that by now I should have a script that removes the known_hosts file at the beginning of every month.

Ideally, you would never have to remove an entry from the known_hosts file (or the entire file). But if you have to, you've just learned how to do it.

Takeaways: known_hosts, ssh-keygen

[Want to try out Red Hat Enterprise Linux? Download it now for free.]

Topics:   Linux   Networking  
Author’s photo

Ken Hess

Ken has used Red Hat Linux since 1996 and has written ebooks, whitepapers, actual books, thousands of exam review questions, and hundreds of articles on open source and other topics. Ken also has 20+ years of experience as an enterprise sysadmin with Unix, Linux, Windows, and Virtualization. More about me

Try Red Hat Enterprise Linux

Download it at no charge from the Red Hat Developer program.