Join our Newsletter — 33% off our NHI Course
Home› Guides› How to rotate SSH host keys across a…
Guide Secrets Management

How to rotate SSH host keys across a server fleet without known_hosts chaos: staged rotation, revocation and host certificates

PDF for newsletter subscribers · free to read online

← All guides
By NHI Mgmt Group, under the editorial direction of Lalit Choda Updated 19 November 2026 13 min read
On this page

An SSH host key is the identity a server proves to every client, script and pipeline that connects to it, so rotating it across a fleet is a trust migration, not a file swap. Done badly, it produces thousands of "REMOTE HOST IDENTIFICATION HAS CHANGED" warnings, broken automation, and engineers trained to delete known_hosts lines without thinking, which is exactly the habit that lets a real man in the middle through.

This guide covers planned rotation, rotation after cloned images left many servers sharing one key, and rotation after a host key may have been exposed. It uses three OpenSSH mechanisms: the client's UpdateHostKeys option, managed global known_hosts files with the @revoked marker, and host certificates trusted through @cert-authority. All option names and behaviour come from the OpenSSH manual pages (ssh_config(5), sshd_config(5), ssh-keygen(1) and sshd(8)) in the openssh-portable source. For the incident side of a key exposure, including user keys, use the SSH Key Compromise Response Playbook; this page focuses on the host key mechanics.

Key takeaways

  • Pick the method by trigger: UpdateHostKeys for planned rotation, a pushed global known_hosts with @revoked for compromise or cloned images, and host certificates for the long term fix.
  • UpdateHostKeys only learns new keys after the client has authenticated the host with a key it already trusts, so it is unsuitable when the old key may be in an attacker's hands.
  • Run old and new host keys side by side on the server during the overlap; sshd accepts multiple HostKey files.
  • Revoke the old key explicitly. Removing a key from servers does not stop a client that still trusts it from accepting an impostor presenting it.
  • With host certificates, clients trust one CA line, so rotating a server's host key becomes "issue a new certificate" with no client change.
  • Fix the image pipeline so host keys are generated on first boot, or cloned servers will keep sharing a key.

When host keys need rotating

  • Cloned images. A golden image or template was built with host keys baked in, so every server built from it presents the same identity. Compromise of one host exposes the key for all of them, and clients cannot tell them apart.
  • Suspected exposure. A private host key file under /etc/ssh/ was readable by the wrong account, included in a backup or image that left your control, or present on a host that was compromised. ATT&CK tracks theft of private keys under T1552 Unsecured Credentials, and a stolen host key is what enables adversary in the middle interception of SSH sessions.
  • Algorithm retirement. You are moving the fleet off older key types, for example retiring RSA host keys in favour of Ed25519, or planning a post quantum transition covered in the PQC SSH Migration Guide.
  • Policy cryptoperiod. Your key management standard sets a maximum lifetime for long lived keys.

The trigger matters because it decides whether the old key can still be trusted during the transition. For a scheduled rotation it can, so clients can be led from old to new. After exposure or cloning it cannot, so clients must be told directly which keys are good and which are revoked.

Why host key rotation breaks known_hosts

Clients store the host keys they trust in ~/.ssh/known_hosts and in the global files named by GlobalKnownHostsFile, which defaults to /etc/ssh/ssh_known_hosts and /etc/ssh/ssh_known_hosts2. Each line holds an optional marker, host name patterns, key type, base64 key and comment. With StrictHostKeyChecking set to yes or accept-new, ssh refuses to connect when a host's key has changed; accept-new adds keys for new hosts automatically but still blocks changed keys.

So a fleet wide rotation without preparation means every client that ever connected now sees a changed key. Humans then run ssh-keygen -R hostname to remove the old entry (the documented way to delete keys for a host, including hashed entries) and accept whatever key is presented next. Automation either breaks or, worse, was configured with StrictHostKeyChecking no and silently accepts anything. The aim of every method below is that clients learn the new key through a trusted path before the old one disappears.

Before you start

  • Inventory host keys and fingerprints. Collect the public host keys from every server and record fingerprints with ssh-keygen -l -f /etc/ssh/ssh_host_ed25519_key.pub. Group by fingerprint: any fingerprint shared by more than one server is a cloned key and must be treated as exposed.
  • Inventory the clients. List bastions, CI runners, configuration management servers, backup systems and monitoring that connect by SSH, and how each stores host keys (per user known_hosts, global file, or a UserKnownHostsFile override in a tool's own configuration). The How to Find ssh authorized keys across a server fleet guide uses the same discovery approach for user keys.
  • Find unsafe client settings. Search configurations and scripts for StrictHostKeyChecking no and UserKnownHostsFile /dev/null. Those clients will not warn you of anything during or after the change.
  • Decide the target state. Choose the key types you will keep, and whether you are moving to host certificates now or later.
  • Check versions. The [email protected] extension that UpdateHostKeys relies on is supported only by sshd from OpenSSH 6.8 and later.

Method 1: graceful rotation with UpdateHostKeys

The ssh_config(5) page describes UpdateHostKeys as letting ssh accept notifications of additional host keys sent by the server after authentication and add them to UserKnownHostsFile, which "supports graceful key rotation by allowing a server to send replacement public keys before old ones are removed." The values are yes, no and ask.

Three conditions limit when it works. Additional keys are only accepted if the key used to authenticate the host was already trusted or explicitly accepted, the host was authenticated via UserKnownHostsFile rather than GlobalKnownHostsFile, and the host was authenticated with a plain key rather than a certificate. It is enabled by default unless the user has overridden UserKnownHostsFile or enabled VerifyHostKeyDNS; otherwise it is set to no. The ask setting is incompatible with ControlPersist.

Procedure

  1. Generate the new key at a new path on each server, for example ssh-keygen -t ed25519 -N '' -f /etc/ssh/ssh_host_ed25519_key_new, keeping the private file readable only by root. sshd refuses a host key file that is group or world accessible.
  2. Add it as an additional HostKey line in sshd_config, keeping the existing lines. The sshd_config(5) page states that multiple host key files are allowed, and that HostKeyAlgorithms restricts which keys are actually used.
  3. Validate and reload sshd (sshd -t to test the configuration, then reload through your service manager).
  4. Overlap. Leave both keys in place long enough for regular clients to connect. Each client that authenticates the server with the old trusted key receives the full key list and records the new one in its known_hosts.
  5. Push the new public keys to managed clients as well. Clients that use a global file or an overridden known_hosts path will not learn through UpdateHostKeys, so update them directly (Method 2).
  6. Retire the old key. Remove the old HostKey line, reload sshd, and securely delete the old private key file.

Test step 2 on a staging pair first with the key types you actually use, and confirm with ssh -v from a client which host key is negotiated and that the new key appears in its known_hosts. Do not use this method after a suspected exposure: whoever holds the old private key can also authenticate as the server, and so could present keys of their own choosing to clients during the overlap.

Method 2: a managed global known_hosts with revocation

For compromised keys, cloned images and clients that UpdateHostKeys cannot reach, distribute trust directly through configuration management.

  1. Generate fresh keys on each server and replace the old ones. For cloned hosts, remove the shared key files and run ssh-keygen -A, which generates host keys of all default types that do not already exist, with the default path and an empty passphrase.
  2. Collect the new public keys from each server over a channel you already trust (your configuration management agent, a cloud API returning console output, or an out of band check), not by connecting with SSH and accepting what is presented.
  3. Build the global file. Write one line per host with its names and addresses, and add the old keys with the @revoked marker. sshd(8) defines @revoked as meaning the key "is revoked and must not ever be accepted", and shows the form @revoked * ssh-rsa AAAA.... A wildcard host pattern on a revoked line makes sense for a cloned or stolen key, since no server should present it.
  4. Push the file to /etc/ssh/ssh_known_hosts on every client and bastion, and into the known_hosts used by CI and automation tools.
  5. Clean stale personal entries. Ask users, or script it, to remove old host lines with ssh-keygen -R, so a changed key warning is not triggered by a stale personal entry that conflicts with the global file.

For larger revocation sets, the client option RevokedHostKeys points to a file of revoked keys, either one public key per line or an OpenSSH Key Revocation List generated with ssh-keygen -k. Note the warning in ssh_config(5): if that file does not exist or is not readable, host authentication is refused for all hosts. Deploy the file before you deploy the option, and monitor its presence.

If you hash known_hosts files with ssh-keygen -H, the original is moved to a file with a .old suffix, and ssh-keygen -F hostname still finds entries. Remove the .old file afterwards if hashing is meant to hide your host inventory.

Method 3: host certificates and @cert-authority

The lasting fix for known_hosts churn is to stop trusting individual host keys. With host certificates, an SSH CA signs each server's host public key, and clients trust the CA. The ssh-keygen(1) example for a host certificate is:

ssh-keygen -s /path/to/ca_key -I id -h -n foo.example.org /path/to/host_key.pub

The -h flag makes a host certificate rather than a user certificate, the principals given with -n are host names and may contain wildcards, the output is written to host_key-cert.pub, and -V sets a validity interval. On the server, the HostCertificate option names the certificate file, whose public key must match a private key already set by HostKey. On clients, a single known_hosts line with the @cert-authority marker, such as @cert-authority *.example.org ssh-ed25519 AAAA..., trusts every certificate the CA issues for matching names.

Once this is in place, rotating a server's host key is local: generate a new key, have the CA sign it, update HostKey and HostCertificate, and reload. Clients see a new certificate from a CA they already trust and raise no warning. Use short validity periods and automate renewal, keep the CA private key in hardware or a secrets platform, and revoke a compromised host key or certificate with @revoked or a KRL. Remember that UpdateHostKeys does not apply to hosts authenticated by certificate, so certificates and Method 1 serve different stages. The design choices for running the CA itself are covered in the SSH Protocol and Certificate Authority Guide.

A practical migration order is: issue certificates for the existing host keys, distribute the @cert-authority line, confirm clients validate certificates, and only then rotate keys, because at that point rotation no longer needs any client change.

Fixing cloned images at the source

  • Remove host keys from images. Delete /etc/ssh/ssh_host_* as the last step of every image build.
  • Generate on first boot. Run ssh-keygen -A (or your distribution's first boot service) before sshd starts, so each instance gets unique keys.
  • Register keys at birth. Have the provisioning step report the new public keys, or have the CA sign them, so trust is established without anyone accepting a key on first connection.
  • Check continuously. Compare fingerprints across the fleet in your inventory job and alert on duplicates.

Verify the rotation

  • From a managed client, connect with ssh -v and confirm the fingerprint or certificate presented matches your inventory.
  • Confirm the old key is refused: a test server deliberately presenting the old key should be rejected because of the @revoked entry or the KRL.
  • Re-run the fingerprint inventory and confirm no duplicates and no old fingerprints remain on any server.
  • Check CI and automation runs for host key failures over the following days.
  • Re-scan client configurations for StrictHostKeyChecking no and remove it now that trust is managed.

Common failure patterns

  • Swapping keys in one step. Old keys replaced fleet wide with no overlap, so every client sees a changed key at once.
  • Relying on UpdateHostKeys after a compromise. The old key still authenticates the impostor, so the client learns nothing it can trust.
  • Removing without revoking. Old keys deleted from servers but not marked @revoked, so clients would still accept them from an attacker.
  • Turning off host key checking to stop the noise. StrictHostKeyChecking no added to scripts during the change and never removed.
  • A missing RevokedHostKeys file. The option deployed before the file, so all host authentication fails.
  • Keys still in the image. Servers rotated, then the next autoscaling event brings back the cloned key.
  • An unprotected host CA key. The CA becomes a single key that can impersonate every server.

Practitioner checklist

  • Inventory every host key fingerprint and flag duplicates as exposed.
  • Inventory SSH clients, including automation, and their known_hosts locations.
  • Choose the method per trigger: UpdateHostKeys for planned change, pushed known_hosts with @revoked for exposure or cloning.
  • Run old and new HostKey lines side by side during planned overlaps, and test on staging first.
  • Collect new public keys through a trusted channel, never by accepting them over SSH.
  • Revoke old keys with @revoked or a KRL, deploying the file before any RevokedHostKeys option.
  • Remove host keys from images and generate them on first boot.
  • Plan the move to host certificates with short validity and a protected CA key.
  • Remove StrictHostKeyChecking no and /dev/null known_hosts settings from scripts.

Standards and references

Related NHI Mgmt Group resources: SSH Key Compromise Response Playbook · SSH Protocol and Certificate Authority Guide · SSH Key Management Guide · OpenSSH Server Hardening Guide

PDF for subscribers

Take this guide with you

Every section, table and checklist in one PDF, with links to the sources and related guides. Subscribe to our weekly NHI & AI Identity Journal and we’ll email you a download link.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

PDF Downloads for Newsletter Subscribers

Get the guide

Enter your details and we’ll email you a download link.

The full guide is free to read online. The PDF is a thank you for subscribing.

Fill in all fields marked * and tick both boxes to continue.