Known hosts and fingerprints
How SSH checks the host, what the first-connection prompt and the known_hosts file mean, and what to do if the key changes.
Every SSH server has one or more host keys. Your client remembers the key of each host it has met. This is how it notices a different machine, or an attacker in between.
The first connection
The client has never seen this host, so it shows the host key's fingerprint and asks if you trust it. This is called trust on first use. If you accept, the client saves the key. Next time it is checked silently.
Fingerprints are written like this:
SHA256:uNiVztksCsDhcc0u9e8BujQXVUpKZIDTMczCvj3tD2s
The older MD5 format is hex pairs with colons. SHA256 is the modern format.
How to check the first prompt
- The best way is to compare it with a fingerprint you got by another route: from the host's owner, in a ticket, or from the host console.
- On the host you can print its fingerprints:
ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub. - You can fetch the key from the network (
ssh-keyscan host) but that comes over the same path, so it does not prove anything on its own.
The known_hosts file
On a computer the file is ~/.ssh/known_hosts. One line per host: the host name (often hashed), the key type and the public key. To find or remove a hashed entry, see Hashed known_hosts: find, remove or hash hosts with ssh-keygen. Apps keep the same information in their own store.
When the key changes
The warning "Remote host identification has changed" means the key is not the one saved. There are harmless and serious causes:
- The host was reinstalled or its keys regenerated.
- The name now points at a different machine (a new server, a failover, a load balancer).
- Someone is intercepting the connection.
Do not just accept it. Ask the host's owner whether the keys changed. If yes, delete the old entry and accept the new fingerprint. On a computer: ssh-keygen -R host.
Other ways to trust a host
- Host certificates: a CA signs the host key, and the client trusts the CA. See Certificates. For the
@cert-authorityline and its errors, see SSH host certificates and @cert-authority in known_hosts. - SSHFP records (RFC 4255): the fingerprint is published in DNS, ideally with DNSSEC.
Related
- How SSH works
- SSH problems
- Read the SSH warning "REMOTE HOST IDENTIFICATION HAS CHANGED"
- SSH "Warning: Permanently added ... to the list of known hosts": what it means
- App guide: SSH login
- SSH host certificates and @cert-authority in known_hosts
- Hashed known_hosts: find, remove or hash hosts with ssh-keygen
- Rotate an SSH host key without warnings: UpdateHostKeys
- SSH "server gave bad signature for RSA key 0" (client_global_hostkeys_private_confirm)