Strovent SystemsSign in
Reference

Rotate an SSH host key without warnings: UpdateHostKeys

Add the new host key next to the old one, let OpenSSH clients learn it through UpdateHostKeys, then remove the old key. Clients that learned it see no warning.

Replacing a server's host key the simple way makes every client stop with REMOTE HOST IDENTIFICATION HAS CHANGED (see Read the SSH warning "REMOTE HOST IDENTIFICATION HAS CHANGED"). OpenSSH has a way to avoid that: the server announces all of its host keys after login, and the client adds the new ones to known_hosts before the old key goes away.

How it works

  • After a user logs in, an OpenSSH server (6.8 or later) sends the client the public half of every host key it has loaded, using the [email protected] extension.
  • If the client's UpdateHostKeys option allows it, the client asks the server to prove that it holds the private key for each key it does not know yet. Then it adds those keys to known_hosts, and removes keys saved for that host that the server no longer offers.
  • The keys arrive over a connection that was already checked against a key the client trusts, so the user is not asked about them.

Is UpdateHostKeys on?

Since OpenSSH 8.5 it is on by default, with conditions. The release notes list them:

  • The host's key was found in the user's known_hosts, not the global one.
  • The same key is not saved under another name.
  • No certificate host key is in use.
  • known_hosts has no wildcard pattern that matches the host.
  • VerifyHostKeyDNS is off, and the default UserKnownHostsFile is in use.

You can also set UpdateHostKeys yes or UpdateHostKeys ask per host in ~/.ssh/config. With ask, the client lists the changes and prompts Accept updated hostkeys? (yes/no):. With yes, the lines Learned new hostkey: ED25519 SHA256:... and Deprecating obsolete hostkey: RSA SHA256:... appear only with ssh -v.

Rotating a key on the server

1. Create the new key. Host keys must have an empty passphrase:

ssh-keygen -t ed25519 -N '' -f /etc/ssh/ssh_host_ed25519_key_2026

2. Add it to sshd_config and keep the old key. If sshd_config lists no HostKey lines, sshd loads its default key files. Once you list one, it loads only the listed files, so list the old keys too:

HostKey /etc/ssh/ssh_host_ed25519_key
HostKey /etc/ssh/ssh_host_ed25519_key_2026

For key exchange, sshd uses the first loaded key of the type it agreed with the client. Put the new key after the old one when both are the same type, so clients keep seeing the key they know.

3. Reload sshd. sshd rereads sshd_config when it gets a hangup signal, or restart the service.

4. Wait. Each OpenSSH client that logs in during this period learns the new key. How long to wait depends on how often your users connect.

5. Remove the old HostKey line and reload again. Clients that learned the new key connect with no warning, and drop the old key from known_hosts at that login.

Clients that did not log in during the wait, clients that do not support the extension, and clients whose conditions above were not met still get the changed-key warning. Give those users the new fingerprint, from ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key_2026.pub, through a channel other than SSH.

Why a client did not update

Run ssh -v host.example.com and look for lines that start with client_input_hostkeys or mention skipping UserKnownHostsFile update:

  • "host key found matching a different name/address": the same key is also saved under another name, for example the host's IP address from an earlier connection. Remove the extra entry with ssh-keygen -R 192.0.2.10 and connect again.
  • "CA/revocation marker, manual host list or wildcard host pattern found": the matching known_hosts line is a pattern, a list of names, or a @cert-authority or @revoked line. The client does not rewrite those.
  • "server gave bad signature for RSA key 0": the server's proof for a new key failed, usually because the server is older than OpenSSH 7.7. See SSH "server gave bad signature for RSA key 0" (client_global_hostkeys_private_confirm).

The alternative: host certificates

If you manage many servers, a host certificate signed by your own CA lets clients trust any host key the CA signs, so changing a key needs no client change at all. See SSH host certificates and @cert-authority in known_hosts.

Sources

Spotted a mistake? Email [email protected].