Join our Newsletter — 33% off our NHI Course

How does known_hosts work?

known_hosts stores SSH host keys so a client can verify that a server is the same host it connected to before. If the key changes unexpectedly, the client warns of a possible interception or server rebuild. Security teams should manage key changes carefully, remove stale entries and investigate unplanned mismatches rather than bypassing the warning.

What known_hosts is actually doing during SSH verification

The known_hosts file is a trust cache for SSH server identities. When you connect, the client compares the server’s presented host key with the key already stored for that host, and the match tells it, “this is the same server I saw before.” That makes the file a practical control against silent interception, DNS abuse, and accidental confusion between hosts that share names but not keys.

The important nuance is that known_hosts does not prove the server is the right business service by itself. It proves continuity of the SSH host key for a given host name, address, or alias. If the key changes, the client is warning you that the trust relationship has changed and that the change should be explained, not ignored.

For teams that manage many servers, the file also becomes an operational record of prior trust decisions. Stale entries from rebuilt hosts, migrated systems, or reissued keys can create noise, while missing entries can force users into one-time trust prompts. A disciplined SSH Key and SSH Certificate Management Guide approach helps keep that trust record aligned with real infrastructure changes.

Why host key changes matter and when the warning is real

An unexpected host key change can mean several different things. It may indicate a legitimate rebuild, host replacement, load balancer or jump-host change, key rotation, or a migrated service. It may also indicate a man-in-the-middle attempt, a spoofed endpoint, or an attacker who has taken over the server and replaced its SSH identity. The warning exists because the client cannot safely guess which explanation is true.

The safest response is to treat the mismatch as an event to validate, not as a prompt to click through. If the change was planned, confirm it against a trusted source of infrastructure truth, then update the entry deliberately. If the change was not planned, investigate the host, the network path, and any recent access or rebuild activity before trusting the new key. A real-world reminder of why this matters is the GitHub RSA SSH host key exposure 2023 incident, which shows that even well-run environments can need rapid key rotation and careful trust updates after exposure.

Keeping old entries forever is also risky. An outdated known_hosts record can block legitimate administration after rebuilds, but removing entries blindly can erase a useful check against spoofing. The right balance is to retire obsolete host records when the server is intentionally replaced and keep a clear process for approving key changes.

How to manage mismatches without breaking trust

When a mismatch appears, first determine whether the host was expected to change. Check change tickets, provisioning logs, DNS updates, orchestration records, or maintenance windows before touching the file. If the infrastructure change is confirmed, replace the old entry with the new key in a controlled way. If it is not confirmed, treat the event as a possible compromise until proven otherwise.

Good practice is to separate operational convenience from trust validation. Automated rebuilds and ephemeral hosts may need a more deliberate workflow so users are not trained to bypass warnings. That can mean pre-seeding known_hosts from a trusted provisioning step, using SSH host certificates where appropriate, or standardising how rebuilt systems are reintroduced into the fleet.

For SSH hardening decisions, the control logic is simple: the client should only trust a host key change when the organisation can explain why the identity changed. If the explanation is missing, the warning is doing its job.

Risk and Threat Considerations

Known_hosts reduces the chance of silent SSH interception, but it can also create false confidence if teams ignore warnings or overwrite entries without checking why they changed. The main risk is not the file itself, it is the habit of treating an identity change as routine when it may signal compromise, spoofing, or an untracked rebuild.

Failure mechanism: An attacker who can position themselves between the client and server, or who can alter the server’s host key, can trigger trust confusion or impersonate the target host if operators accept the new key without validation.

Impact: A successful bypass can expose SSH sessions, administrative commands, and automation traffic, and it can also hide the fact that a server has been rebuilt or tampered with.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP Non-Human Identity Top 10 addresses the attack and risk surface, while CIS Controls v8 and NIST SP 800-53 Rev 5 set the governance and control requirements practitioners need to meet.

Framework Control / Reference Relevance
CIS Controls v8 CIS-4 — Secure Configuration of Enterprise Assets and Software known_hosts is a secure configuration artifact for SSH trust.
Recommendation — Standardize SSH host key handling and remove stale trust entries during configuration maintenance.
NIST SP 800-53 Rev 5 IA-5 — Authenticator Management SSH host keys and their rotation are managed authenticators for server identity.
SI-4 — System Monitoring Unexpected host key changes are an observable indicator that merits investigation.
Recommendation — Track host key issuance, rotation, and retirement as part of authenticator lifecycle control. Alert on unexpected SSH host key changes and investigate them before re-establishing trust.
OWASP Non-Human Identity Top 10 NHI-07 — Long-Lived Secrets SSH host keys can persist too long and become stale or exposed trust material.
NHI-01 — Improper Offboarding Rebuilt or retired hosts can leave stale SSH trust records behind.
Recommendation — Reduce host key lifetime where operationally possible and rotate exposed keys promptly. Remove obsolete host key entries when systems are decommissioned or rebuilt.

Practitioner Guidance

What to verify: Before updating known_hosts, verify the host change against a trusted change record, provisioning system, or infrastructure owner. If you cannot explain the mismatch, treat it as a security issue, not a housekeeping task.

What good looks like: Planned rotations are documented, stale entries are removed during rebuilds, and users do not learn to suppress the warning as a normal part of administration. The file should reflect the current fleet, not a history of unresolved exceptions.

Practitioner takeaway: known_hosts works best when host key changes are rare, explainable, and controlled, because the warning is only useful if your process treats it as evidence to investigate rather than friction to bypass.