ThisCalculator

Everyday

SSH Host Key Verification Before Trusting

Updated · Standard formula, shown below

SSH Host Key Verification Before Trusting

When you connect to a new SSH server and see the “authenticity of host … can’t be established” warning, you should verify the host key before accepting it. Blindly typing “yes” exposes you to man-in-the-middle attacks where an attacker intercepts your connection and steals passwords or data. This article explains why verification matters, how to do it correctly, and what to do when the key changes unexpectedly.

Why Host Key Verification Exists

SSH uses public-key cryptography to encrypt your connection, but encryption alone doesn’t tell you who is on the other end. The host key is the server’s identity: a long-term public key that the server presents during the initial handshake. If you accept a key without checking it, you are trusting that the person or machine sending it is the real server. An attacker positioned between you and the server (a man-in-the-middle) can present their own key, decrypt traffic, and forward it to the real server while capturing everything you send. Verifying the key before accepting it breaks that attack: you compare the presented key against a known-good value obtained out-of-band (for example, from the server administrator or a DNS record).

How SSH Key Exchange Works

When you run ssh user@host for the first time, the client receives the server’s host key and displays a fingerprint (a hash of the key, e.g., SHA256:uB2... ). The client asks you to confirm it. If you accept, the key is stored in ~/.ssh/known_hosts on your local machine. On subsequent connections, the client compares the presented key with the stored one. If they match, you connect without a prompt. If they differ, you get a warning that the host key has changed, which can indicate a real problem (server reinstall, key rotation) or an attack.

How to Verify a New Host Key

Before accepting a key, obtain the correct fingerprint from a trusted source. Common methods:

  • Ask the server administrator to run ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub (or the equivalent for the key type) and compare the output.
  • Check the server’s DNS SSHFP records: dig +short SSHFP hostname and compare the fingerprint.
  • If you have physical access, run ssh-keyscan hostname on a trusted network and compare.
  • For cloud instances, many providers display the host key fingerprint in their console or metadata service.

Once you have the correct fingerprint, compare it with the one shown by your SSH client. They must match exactly. Only then type yes. If they don’t match, do not accept; investigate the discrepancy.

What to Do When a Host Key Changes

If you get a “REMOTE HOST IDENTIFICATION HAS CHANGED” warning, stop and investigate. Do not blindly remove the old key and accept the new one. Possible causes:

  • The server was reinstalled or its host keys regenerated (common after OS reinstall or cloud instance rebuild).
  • You are connecting to a different machine that happens to use the same hostname (e.g., a load balancer or a new VM).
  • You are being attacked by a man-in-the-middle.

To fix a legitimate change, verify the new fingerprint through an out-of-band channel (as above). If it’s correct, remove the old key with ssh-keygen -R hostname and then reconnect, accepting the new key after verification.

Common Mistakes and Misconceptions

Many users skip verification because they think it’s unnecessary or too complicated. Here are common pitfalls:

  • Assuming the warning is just a formality. It is a security check, and ignoring it defeats the purpose.
  • Using StrictHostKeyChecking=no in your SSH config to bypass prompts. This automatically accepts any key, making you vulnerable.
  • Comparing only the beginning of the fingerprint. Always compare the entire string.
  • Not checking the key type. SSH supports RSA, ECDSA, Ed25519, etc. Make sure you compare the correct algorithm’s fingerprint.

Alternatives to Manual Verification

For large fleets or automated deployments, manual verification is impractical. Alternatives include:

  • Using SSH certificates: a certificate authority signs host keys, and clients trust the CA. This eliminates the need to verify each host individually.
  • Using SSHFP DNS records: clients can automatically fetch and verify the host key via DNS, if DNSSEC is used to prevent spoofing.
  • Using a configuration management tool (e.g., Ansible, Puppet) to distribute known_hosts files with pre-verified keys.

Costs and Trade-offs

Verification adds a small overhead: you must obtain the fingerprint out-of-band, which may require contacting an administrator or checking a web portal. For a single server, this is a one-time cost. For dynamic environments, it can be significant, which is why certificates or SSHFP are often used. The cost of not verifying is much higher: a successful man-in-the-middle attack can expose credentials, sensitive data, and give an attacker a foothold in your infrastructure.

Frequently Asked Questions

What does the SSH warning actually say?

It says something like: “The authenticity of host ‘example.com (192.0.2.1)’ can’t be established. ED25519 key fingerprint is SHA256:… Are you sure you want to continue connecting (yes/no)?” This is the client asking you to confirm the key before storing it.

Can I verify a host key after I’ve already accepted it?

Yes. You can manually check the stored key with ssh-keygen -lf ~/.ssh/known_hosts and compare it to the server’s actual key. If they differ, remove the stored key and re-verify.

Is it safe to skip verification if I’m on a VPN?

No. A VPN encrypts your traffic to the VPN endpoint, but the SSH connection still traverses networks between the VPN exit and the server. An attacker on that path can still intercept the connection. Always verify host keys regardless of network conditions.

What is the difference between a host key and a user key?

A host key identifies the server, while a user key identifies you (the client) to the server. Host keys are used to encrypt the session and prevent MITM attacks; user keys are used for authentication. Both are important, but host key verification is the first line of defense.

How often do host keys change?

They should change only when the server’s host keys are regenerated, which typically happens during OS reinstall or when an administrator explicitly rotates them. If a key changes without explanation, treat it as a red flag.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.