DEV Community

Nate
Nate

Posted on

SSH Host Key Changed After a Rebuild? Verify It Before Updating known_hosts

A server rebuild can legitimately change its SSH host key. But the warning that a host key has changed is also an important defense against connecting to an impostor. Don’t work around it with StrictHostKeyChecking=no or delete your entire known_hosts file. First verify the new key through a trusted channel, then update only the entry for the affected server.

1. Confirm which server SSH is actually connecting to

An SSH alias can hide the address and port used for the connection. Check the relevant configuration, often in ~/.ssh/config:

Host app-prod
    HostName 203.0.113.20
    Port 2222
    User deploy
Enter fullscreen mode Exit fullscreen mode

Here, app-prod is the name you type, but the connection goes to 203.0.113.20 on port 2222. The port matters: a nonstandard port is normally represented in known_hosts as [hostname]:port.

Ask OpenSSH for the effective settings rather than assuming which configuration file or matching block applies:

ssh -G app-prod | grep -E '^(hostname|port|hostkeyalias|proxyjump) '
Enter fullscreen mode Exit fullscreen mode

ssh -G prints resolved client settings without connecting. For a general explanation of aliases and other configuration options, see practical SSH config examples.

2. Verify the replacement fingerprint independently

Get the new host-key fingerprint from a channel you already trust, such as the VPS provider’s authenticated console, a server console you control, or a trusted configuration-management system. On the rebuilt server, an administrator can fingerprint a public host-key file with:

sudo ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub
Enter fullscreen mode Exit fullscreen mode

Use the path and algorithm appropriate to the key being checked. Compare the displayed fingerprint with the one your SSH client reports. If the server is managed by someone else, ask its administrator to provide the fingerprint through an authenticated channel.

ssh-keyscan can collect a key offered over the network, but it does not establish that the key belongs to your server. Don’t treat its output—or a new fingerprint shown during the suspicious connection itself—as independent verification.

If you cannot confirm the fingerprint, stop and investigate. A rebuild is a plausible explanation, not proof that the change is safe.

3. Find the matching known_hosts entry

For the example above, search for the address and port in the form OpenSSH uses for a nondefault port:

ssh-keygen -F '[203.0.113.20]:2222'
Enter fullscreen mode Exit fullscreen mode

The command displays matching entries if present. If the results are hashed, that is normal: hashing obscures hostnames in the file but still allows ssh-keygen -F to search for them. You can also search for the alias:

ssh-keygen -F app-prod
Enter fullscreen mode Exit fullscreen mode

Check both when you’re unsure how the existing entry was recorded. If the configuration sets HostKeyAlias, that value controls the name used for host-key checking; inspect the hostkeyalias value from ssh -G and search for that name instead. Avoid removing an entry based on an assumed alias-to-address mapping.

4. Remove only the affected host entry

Back up the file before changing it:

cp ~/.ssh/known_hosts ~/.ssh/known_hosts.backup
Enter fullscreen mode Exit fullscreen mode

Then remove the entry for the exact address and port:

ssh-keygen -R '[203.0.113.20]:2222'
Enter fullscreen mode Exit fullscreen mode

This removes matching entries for that host identity from the default known_hosts file; it does not mean “remove just one key algorithm.” If ssh -G shows a custom userknownhostsfile, use that file explicitly with -f, for example:

ssh-keygen -R '[203.0.113.20]:2222' -f ~/.ssh/known_hosts.work
Enter fullscreen mode Exit fullscreen mode

If the matching host has other keys you intend to retain, inspect the ssh-keygen -F output and edit only the stale line in the relevant file instead. Be especially careful with shared hostnames, IPs reused by multiple services, and aliases configured with HostKeyAlias.

5. Reconnect and compare before accepting

Reconnect using the same alias:

ssh app-prod
Enter fullscreen mode Exit fullscreen mode

Because the old entry is gone, OpenSSH may ask whether to trust the presented key. Compare the fingerprint in that prompt with the fingerprint you verified independently. Accept only if they match. OpenSSH then records the accepted key in known_hosts; future connections can detect an unexpected change.

If the prompt doesn’t appear, don’t assume the update failed. The key may already be recorded under another matching name or file. Search the effective hostname, port, and any HostKeyAlias value again.

Jump hosts have their own host keys

With ProxyJump, SSH checks the jump host and the destination as separate SSH connections. A changed key for the bastion does not mean the destination’s key changed, or vice versa. Inspect the relevant host blocks and verify the fingerprint for the machine that actually triggered the warning. See how SSH jump hosts and ProxyJump work for the connection path; this article’s key point is to update only the identity that changed.

For a changed destination behind a bastion, the destination’s configured hostname and port still matter for its host-key entry. The jump host’s own alias, hostname, port, and key must be checked separately. If you’re diagnosing which hostname an alias resolves to, this guide to SSH hostname resolution errors covers the related lookup behavior.

A safe recovery is deliberate: resolve the effective host identity, verify the replacement fingerprint out of band, remove only the corresponding stale entry, and confirm the fingerprint again when reconnecting.

I'm building ShadeTerm — an SSH client that organizes terminals, SFTP, code editing, and server tools into workspaces.

Top comments (0)