PREP WITH PROOF
Menu
SECURITY & NETWORKING / PRACTICAL GUIDE

SSH Host Key Changed: Verify the Fingerprint Before Repair

Resolve SSH host key changed warnings safely. Verify fingerprints, inspect effective configuration, handle hashed known_hosts and custom ports, and stage repairs.

THE QUICK ANSWER

An SSH host-key warning means the server identity presented for this destination conflicts with a stored expectation. Verify the new fingerprint through an independently trusted channel, identify the exact hostname, port and trust file, and stage a narrow repair. Removing the warning does not by itself establish that the destination is genuine.

Keep the identity question separate from the login problem

You rebuild a virtual machine, reuse its DNS name and find that SSH refuses the connection with a host-identification warning. A legitimate rebuild is one possible explanation. So are an incorrect endpoint, a reused address, an unexpected backend or an attacker substituting a server. The warning alone cannot distinguish them.

A host key identifies the server to the SSH client. Your user key or password authenticates you to the server. Replacing your user key will not repair a conflicting server identity, and a successful password prompt is not a substitute for checking that identity first.

This guide uses OpenSSH commands and a Bash lab that runs entirely on temporary files. The lab was executed with OpenSSH 9.6p1 on Linux. It verifies local fingerprint and known_hosts operations, not a network handshake, live server rotation, DNS authenticity or user authentication. Commands that refer to reports.example.test are teaching examples, not a real service to contact.

Record what changed before editing a trust file

Keep the destination, time, warning text, offered key type and fingerprint, and any referenced file and line number. Compare them with the deployment or rotation record. If a colleague reports a rebuild, confirm which instance was rebuilt and which key was installed; a change ticket without the expected identity is incomplete evidence.

Check the client invocation and effective configuration. An SSH nickname can resolve to a different HostName or port, and HostKeyAlias can deliberately change the name used for host-key lookup. UserKnownHostsFile may point somewhere other than the familiar default. A jump host also has its own identity checks; identify which connection produced the warning.

ssh -G prints evaluated configuration and exits. Inspect hostname, port, hostkeyalias, userknownhostsfile, globalknownhostsfile, proxyjump and stricthostkeychecking. Review the configuration files too: advanced Match rules may execute local commands during evaluation. Use this inspection on configuration you control, and redact account names, paths and infrastructure details before sharing diagnostics.

# Configuration inspection; this does not establish a server connection.
ssh -G reports-prod
# Use the actual nickname from your normal SSH command.

Source: OpenSSH ssh: effective configuration and connection options ↗

Compare against an independently trusted fingerprint

Obtain the expected public host key or SHA256 fingerprint from a trusted administrative channel. Examples include an already authenticated infrastructure console or a verified administrator reading the correct host public-key file. Confirm the algorithm as well as the complete fingerprint; an Ed25519 fingerprint is not interchangeable with an RSA fingerprint.

On a typical Linux server using that configured Ed25519 key path, an administrator can run the command below through the trusted channel. Check the server’s HostKey configuration if the installation uses another path or key. The .pub file is public key material; do not request or transmit the corresponding private host key.

ssh-keyscan can collect what an endpoint presents, but it does not authenticate that material. Compare the collected fingerprint with the independently obtained expectation before adding anything to a trust store. Repeating the scan through the same untrusted route does not create independent evidence. If the values differ or the expected identity cannot be established, leave the connection blocked and resolve the discrepancy.

# Administrator: run through an independently trusted server channel.
ssh-keygen -l -E sha256 -f /etc/ssh/ssh_host_ed25519_key.pub

# Client: collect an UNTRUSTED candidate from your intended endpoint.
# Adapt the host and port; do not append this output directly to known_hosts.
ssh-keyscan -p 2222 -t ed25519 reports.example.test > candidate_hosts
ssh-keygen -l -E sha256 -f candidate_hosts

Source: OpenSSH ssh-keyscan: collection and authenticity limitations ↗

Work through a rebuilt reporting host

Consider a hypothetical reports-prod nickname that normally reaches reports.example.test on port 2222. The operations team rebuilds the reporting machine, but the client still expects its old key. Start by checking the evaluated destination. If it instead points to a retired address or an unexpected alias, correcting that mapping may be the right repair; enrolling the offered key would make the incorrect destination appear legitimate.

If the endpoint is correct, ask the administrator to identify the replacement instance in the authenticated console and read its configured public-key fingerprint there. Compare that fingerprint with the one offered for the same algorithm. Record the instance identity and the verification channel alongside the maintenance reference. This establishes a reasoned link between the rebuild and the replacement identity, rather than relying on the timing of the warning.

After the fingerprints agree, find the bracketed port-2222 record in the affected trust file and stage its replacement. Keep the default-port record separate: it may belong to another service with another lifecycle. If the offered fingerprint disagrees with the administrator’s value, stop this sequence. Rechecking the address and backend is more useful than repeatedly deleting client records.

This scenario is an example of the reasoning order, not a captured production incident. Each step rules out a different explanation. A trustworthy administrative record establishes the expected key; client inspection establishes where that expectation belongs; the final connection verifies that the actual route presents the verified identity.

Find the exact entry, including its port

For a nondefault port, the usual known_hosts lookup form is [hostname]:port. A hostname on port 22 and the same hostname on port 2222 can therefore have different entries. Quote the bracketed form in shell commands so it remains one literal argument.

Use ssh-keygen -F to find entries in the specific file involved. It understands hashed hostnames, which a plain text search for the hostname may miss. Hashing conceals stored hostnames from casual inspection; it does not make an unverified key trustworthy. Check IP-address entries separately if your effective configuration performs those checks.

Before removal, inspect the matching material and back up the file. ssh-keygen -R removes all keys belonging to the requested endpoint from the selected file, including hashed matches. It is not limited to the single algorithm printed in the warning. Review certificate-authority, revocation or centrally managed entries through their owning process rather than treating every line as a disposable personal record.

# Read-only lookup in an explicitly selected file.
ssh-keygen -F '[reports.example.test]:2222' -f "$HOME/.ssh/known_hosts"
ssh-keygen -F reports.example.test -f "$HOME/.ssh/known_hosts"

Source: OpenSSH ssh-keygen: fingerprints and known_hosts operations ↗

Stage a narrow repair after identity verification

Once the identity is established, copy the affected trust file into a new review location and preserve a backup. Remove only the intended endpoint from the review copy, then add the verified public-key line under the correct lookup name. If you collected several algorithms, verify each key you retain; accepting one fingerprint does not validate every line in a scan.

Compare the review copy with the backup. Unrelated hosts should remain unchanged, and the replacement should correspond to the verified algorithm and key. Use the organization’s managed update process to apply the approved change. On machines managed by configuration automation, changing a personal file may be ineffective or may hide the real source of the mismatch.

Keep host verification enabled when testing the repair. StrictHostKeyChecking=yes rejects unknown or changed keys. accept-new can enroll previously unknown hosts automatically, but it still rejects changed keys; it is not proof of a first connection’s identity. Turning checking off changes the trust decision instead of explaining the warning.

# Only after independent verification; adapt the source file if needed.
review_dir=$(mktemp -d)
cp "$HOME/.ssh/known_hosts" "$review_dir/original"
cp "$review_dir/original" "$review_dir/known_hosts"
ssh-keygen -R '[reports.example.test]:2222' -f "$review_dir/known_hosts"
# Add only the independently verified public-key line to the review copy.
# Review the difference and apply through your approved management process.
printf 'Review copy: %s\n' "$review_dir/known_hosts"

Source: OpenSSH ssh_config: trust files, aliases and rotation ↗

Practice hashed lookup and replacement without a server

Save this complete script as known_hosts_lab.sh and run it with Bash. It creates two disposable Ed25519 keys and three teaching entries, hashes the hostnames, removes only the port-2222 entry, and adds the locally generated replacement. The default-port entry and unrelated hostname must remain findable.

An empty passphrase is used only for these short-lived teaching keys. The EXIT trap removes the temporary directory, including private keys and the backup files made by ssh-keygen. The script does not read or modify your real SSH trust files. Its local key generation supplies the expected fingerprint in this exercise; a real server requires the separate verification channel described earlier.

#!/usr/bin/env bash
set -eu
lab_dir=$(mktemp -d)
trap 'rm -rf "$lab_dir"' EXIT

# Disposable teaching keys, never production host or user keys.
ssh-keygen -q -t ed25519 -N '' -C pwp-old -f "$lab_dir/old"
ssh-keygen -q -t ed25519 -N '' -C pwp-new -f "$lab_dir/new"
old_fp=$(ssh-keygen -l -E sha256 -f "$lab_dir/old.pub" | awk '{print $2}')
new_fp=$(ssh-keygen -l -E sha256 -f "$lab_dir/new.pub" | awk '{print $2}')
test "$old_fp" != "$new_fp"

read -r key_type key_data comment < "$lab_dir/old.pub"
for host in '[reports.example.test]:2222' reports.example.test other.example.test; do
  printf '%s %s %s\n' "$host" "$key_type" "$key_data"
done > "$lab_dir/known_hosts"

ssh-keygen -H -f "$lab_dir/known_hosts" > /dev/null 2>&1
ssh-keygen -F '[reports.example.test]:2222' -f "$lab_dir/known_hosts" > /dev/null
ssh-keygen -R '[reports.example.test]:2222' -f "$lab_dir/known_hosts" > /dev/null
if ssh-keygen -F '[reports.example.test]:2222' -f "$lab_dir/known_hosts" > /dev/null; then
  printf '%s\n' 'Unexpected old endpoint entry remains' >&2
  exit 1
fi
ssh-keygen -F reports.example.test -f "$lab_dir/known_hosts" > /dev/null
ssh-keygen -F other.example.test -f "$lab_dir/known_hosts" > /dev/null

# In this lab the replacement comes from our own local key generator.
read -r key_type key_data comment < "$lab_dir/new.pub"
printf '%s %s %s\n' '[reports.example.test]:2222' "$key_type" "$key_data" >> "$lab_dir/known_hosts"
found_fp=$(ssh-keygen -F '[reports.example.test]:2222' -f "$lab_dir/known_hosts" |
  ssh-keygen -l -E sha256 -f /dev/stdin | awk '{print $2}')
test "$found_fp" = "$new_fp"
printf '%s\n' 'PASS: isolated host lookup, removal and replacement'

If the warning persists, revisit the destination and trust source

A second warning after a narrow repair often means a different trust source or endpoint is involved. Read the new message rather than removing additional lines indiscriminately. Check the real SSH client, account, nickname and port used by the failing tool; a GUI or CI runner may have a different home directory and trust file.

Use the decision table to choose the next investigation. These branches are diagnostic prompts, not evidence that a particular event occurred. For example, alternating keys could indicate legitimate backend inconsistency or an incorrect route. Identify each backend through a trusted administrative channel before accepting a collection of keys.

EvidenceNext investigation
Verified key exists, but warning names another fileGlobal or managed trust store, different client account
Default-port lookup succeeds, custom-port lookup failsBracketed endpoint and configured port
Hostname lookup differs from normal nickname behaviorHostName, HostKeyAlias and canonicalization settings
The offered key changes between attemptsDNS resolution, backend identity and routing
Only a jump connection failsJump-host trust and configuration, separately from target
Fingerprint does not match trusted expectationStop the connection and investigate the identity discrepancy

Plan rotation while the old identity is still trusted

For planned changes, distribute the new public host keys before withdrawing the old trusted path. OpenSSH UpdateHostKeys supports learning additional keys after authentication under specified trust conditions. Review both client and server support, and test the effective setting; do not assume it is enabled when custom trust files or other verification options are in use.

A host-certificate deployment can instead rely on an established host certificate authority and hostname validation. That changes the operational model, so adoption needs a separate design and rollout rather than an emergency edit during a mismatch. Preserve revocation controls and use the applicable administrator procedure for certificates.

The completed repair should explain why the identity changed, how its replacement was verified, which trust record changed and which client path was tested. A cleared warning is only the visible result. The useful outcome is a connection whose server identity remains accountable at the next rebuild or rotation.

Source: OpenSSH ssh_config: trust files, aliases and rotation ↗

KEEP THESE THREE THINGS

Your next steps

  1. Verify the complete algorithm-specific fingerprint through an independently trusted channel.
  2. Match the actual hostname or alias, port, client account and trust file before changing a record.
  3. Practice locally, stage narrow changes and retain host verification during recovery.

Sources & official resources

Linked resources checked on 2026-10-26. Technical behavior and provider guidance can change; check the documentation for the version you use.

About this guide and its recommendations

Published by Prep With Proof with AI-assisted drafting. Study routines and examples are editorial suggestions, not official exam instructions or claims of a learner's personal results. Cited provider facts are linked; ownership of recommended Prep With Proof products is disclosed. No competitor endorsement or guaranteed certification outcome is implied.

Suggest a correction →