What are SSH host keys? How a server proves who it is

What SSH host keys are, how known_hosts uses them, how to check a fingerprint, and what to do when the host identification has changed.

6–9 minutes
Front panels of rack-mounted servers

SSHSSH A secure way to type commands on a server from your own computer, as if you were sitting at it. More about SSH → host keys are the key pair a server uses to prove its own identity to you, so you know you are talking to the real machine and not an impostor. This guide is for anyone who has seen the “authenticity of host” prompt or the “host identification has changed” warning and wants to know what is going on and what to do about it.

Every SSH server has its own host key pair, stored in /etc/ssh/ (for example ssh_host_ed25519_key and ssh_host_ed25519_key.pub). The first time you connect, your computer saves the server’s public host key in ~/.ssh/known_hosts, and on every later visit it checks that the server still holds the matching private key. If the key changes, SSH stops with a warning. Check the fingerprint with your host before trusting a new or changed key, then remove the old entry with ssh-keygen -R hostname.

Host keys and user keys are different things

SSH uses two separate sets of keys, and they do opposite jobs:

KeyWhere it livesWhat it proves
Host keyOn the server, in /etc/ssh/That the server is the one you think it is
User keyOn your computer, in ~/.ssh/That you are allowed to log in

Your own SSH keys are optional: you could log in with a password instead. Host keys are not optional. Every SSH server has them, and they are created automatically when the SSH server software (OpenSSH, on almost every Linux server) is installed or first started.

A typical server has several host key pairs, one per key type:

/etc/ssh/ssh_host_ed25519_key
/etc/ssh/ssh_host_ed25519_key.pub
/etc/ssh/ssh_host_ecdsa_key
/etc/ssh/ssh_host_ecdsa_key.pub
/etc/ssh/ssh_host_rsa_key
/etc/ssh/ssh_host_rsa_key.pub

The files without .pub are private keys. They are readable only by root and should never leave the server. The .pub files are public and safe to share. When you connect, the server and your SSH client agree on one type (usually Ed25519 on current systems) and use that pair.

What happens when you connect

Each time you open an SSH connection, the server signs part of the connection setup with its private host key. Your computer checks that signature against the public host key it has on record. Only a machine holding the private key can produce a valid signature, so a server that passes the check is the same server you saw last time.

This protects you against a “man in the middle”: someone on the network (a hostile Wi-Fi hotspot, a hijacked DNSDNS The internet’s phone book. It turns a name people can remember, like example.com, into the number address computers use to find the server. More about DNS → answer) pretending to be your server to capture your password or commands. Without the private host key, they cannot pass the check.

The first connection

The first time, your computer has nothing to compare against, so SSH asks you:

The authenticity of host '203.0.113.10 (203.0.113.10)' can't be established.
ED25519 key fingerprint is SHA256:Xb3vN1...
Are you sure you want to continue connecting (yes/no/[fingerprint])?

The fingerprint is a short summary (a hash) of the public host key. If you type yes, SSH saves the key in ~/.ssh/known_hosts and checks it on every later connection. This model is called “trust on first use”: the first connection is the one moment you are exposed, which is why it is worth checking the fingerprint. Our guide on connecting to a server with SSH walks through this first login.

The known_hosts file

known_hosts is a plain text file with one line per server key. On Debian and Ubuntu the host names are hashed by default (the HashKnownHosts setting), so you will see scrambled text rather than names. To find the entry for a server without reading the file by eye, run:

ssh-keygen -F 203.0.113.10

On Windows the file is C:\Users\yourname\.ssh\known_hosts, and the same ssh-keygen commands work in PowerShell. See where SSH keys are stored for every location.

How to check a host key fingerprint

The safe way to answer the first-time prompt is to compare the fingerprint with one you got another way.

On the server

If you can reach the server through your host’s web console, or someone who manages it can send you the result, run this on the server:

for f in /etc/ssh/ssh_host_*_key.pub; do ssh-keygen -lf "$f"; done

Each line shows the key size, the SHA256: fingerprint and the key type. Match the line with the same type as the prompt (for example ED25519) against what your computer shows. Some hosting control panelsControl panel A website you log into to manage your hosting with buttons and forms, instead of typing commands. cPanel and Plesk are the common ones. More about Control panel → also display the fingerprints of a new server.

From your computer, before logging in

You can fetch a server’s public host keys without logging in and print their fingerprints:

ssh-keyscan 203.0.113.10 | ssh-keygen -lf -

This shows what the server is offering over the network right now. It is handy for recording fingerprints, but it is not proof of identity on its own: if someone is intercepting your connection, this command shows their key too. Compare it with a fingerprint from a trusted source.

“Remote host identification has changed”

If the key a server presents does not match known_hosts, SSH refuses to connect and prints a large warning:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
...
Offending ED25519 key in /home/yourname/.ssh/known_hosts:3

Most of the time the reason is innocent:

  • The server was reinstalled or rebuilt, which creates new host keys.
  • The IP addressIP address The number address of a device on the internet, like 203.0.113.10. Computers use it to find each other, the way post uses street addresses. More about IP address → was given to a different server, common with cloud servers you delete and recreate.
  • You moved the site to a new server but kept the same domain name.
  • A Raspberry Pi or test machine had its system card reflashed.

Do not clear the warning just because it is in the way. If nothing changed on your side (no rebuild, no move, no new IP address), treat it as a possible attack: do not type your password, and check with your host first.

Fixing it once you know why

When you are sure the change is expected, remove the old key and connect again:

ssh-keygen -R 203.0.113.10

If you also connect by domain name, remove that entry too:

ssh-keygen -R example.com

For a server on a non-standard portPort A numbered door on a server. Each service listens behind its own door: web pages on 443, email on others, remote login on 22. More about Port →, include the port in this exact form:

ssh-keygen -R "[203.0.113.10]:2222"

ssh-keygen -R keeps a backup of the old file as known_hosts.old. The next connection shows the first-time prompt again, so check the new fingerprint before typing yes.

When you need to regenerate host keys

You rarely touch a server’s host keys, with one important exception: cloned servers. If you build servers from a copied disk image or VM template, every copy has the same host keys, so they cannot be told apart and one compromised copy exposes the others. Regenerate the keys on each clone.

On any current Linux distribution, remove the old keys and create fresh ones:

sudo rm /etc/ssh/ssh_host_*
sudo ssh-keygen -A

ssh-keygen -A creates every default host key type that is missing. Then restart the SSH service. On Ubuntu and Debian:

sudo systemctl restart ssh

On AlmaLinux and Rocky Linux:

sudo systemctl restart sshd

Keep your current session open while you do this and test a new connection from a second terminal. Everyone who connects afterwards will see the “host identification has changed” warning, so tell them the new fingerprints.

Common problems

SymptomCauseFix
Prompt appears every time you connectknown_hosts is not writable, or you connect by a different name or IP each timeCheck permissions on ~/.ssh and connect using one consistent name
“Host identification has changed” after a rebuildThe rebuild created new host keysConfirm the new fingerprint, then run ssh-keygen -R with the host name or IP
Warning appears with no change on the serverPossible interception, or the IP now belongs to another machineStop, do not log in, and verify with your host
“Host key verification failed” in a scriptNo saved key and the script cannot answer the promptAdd the verified key to known_hosts in advance
Warning names a line number such as known_hosts:3That line holds the old keyssh-keygen -R removes it without editing by hand

You may see advice to set StrictHostKeyChecking no to make the warnings go away. That switches off the protection host keys exist to provide. For automated jobs, StrictHostKeyChecking accept-new is a safer middle ground: it saves keys for new hosts automatically but still refuses a key that has changed.

Official documentation

Get the next guide

One email when there is something worth reading: new guides, errors going around, and hosting changes that affect your site.

No tracking pixels, and one click to leave.

Something out of date? Software changes. If a step no longer works, tell us and we will check it and update the page.