How to Fix ‘Host key verification failed’ in Linux SSH

Connecting to a remote Linux server, virtual private server (VPS), cloud instance, or Raspberry Pi using OpenSSH only to be abruptly blocked by a prominent warning screen reading: “@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ … Host key verification failed” halts remote terminal operations immediately. OpenSSH operates on a trust-on-first-use (TOFU) cryptographic security model. When you first establish an SSH connection to a remote IP address or domain, OpenSSH stores the server’s public host key fingerprint in your local ~/.ssh/known_hosts file. On subsequent connection attempts, SSH matches the remote server’s cryptographic signature against the cached entry. If the server has been reinstalled, its operating system re-imaged, a floating IP address re-assigned, or an active man-in-the-middle (MITM) interception is taking place, the fingerprints mismatch and OpenSSH terminates the session to protect your credentials. This guide explains how to identify the cause and update your known hosts record safely.

What Causes ‘Host key verification failed’?

OpenSSH flags a cryptographic fingerprint mismatch under several scenarios:

  • Server Operating System Reinstallation: A cloud instance or bare-metal server was rebuilt, generating brand-new SSH host keys (RSA, ECDSA, or ED25519) on the same IP.
  • Dynamic or Floating IP Address Reassignment: In cloud environments (such as AWS EC2, DigitalOcean, or Hetzner), releasing and acquiring public IPs often assigns an IP previously used by an unrelated machine.
  • SSH Daemon Configuration Updates: System administrators upgrading OpenSSH or modifying host key generation parameters (e.g., deprecating SHA-1 RSA in favor of ED25519).
  • Port Forwarding and NAT Changes: Routing connections to different internal servers across the same external forwarding port.
  • Man-in-the-Middle (MITM) Attack: An unauthorized third party intercepting network packets between your client machine and the destination host.

Method 1: Remove Stale Fingerprint with ssh-keygen -R

The safest and most precise method to clear a mismatched host key is using OpenSSH’s native key generation utility.

  1. Open your Linux terminal.
  2. Execute the ssh-keygen -R command targeting the hostname or IP address of the remote host:
    ssh-keygen -R 192.168.1.100
  3. If you connect via a custom non-standard SSH port (e.g., port 2222), enclose the IP address in brackets and append the port:
    ssh-keygen -R "[192.168.1.100]:2222"
  4. Terminal outputs a confirmation message indicating that the old key was updated and backed up to ~/.ssh/known_hosts.old.
  5. Attempt to connect again via SSH:
    ssh [email protected]
  6. OpenSSH displays the fresh fingerprint authenticity prompt:
    The authenticity of host '192.168.1.100' can't be established.
    ED25519 key fingerprint is SHA256:abc123xyz...
    Are you sure you want to continue connecting (yes/no/[fingerprint])?
  7. Type yes and press Enter. The new fingerprint is appended to your known hosts record.

Method 2: Manually Delete the Offending Line in known_hosts

If you prefer manual file editing, OpenSSH error messages specify the exact line number of the offending record.

  1. Read the error output in your terminal. Look for the line indicating:
    Offending ECDSA key in /home/username/.ssh/known_hosts:14
    In this example, line 14 contains the conflicting host key.
  2. Delete line 14 directly using the stream editor (sed):
    sed -i '14d' ~/.ssh/known_hosts
  3. Alternatively, open the file in the nano editor:
    nano ~/.ssh/known_hosts
    Use Ctrl + _ to jump to line 14, press Ctrl + K to delete the line, and press Ctrl + O and Enter to save.
  4. Re-run your SSH command and accept the new key.

Method 3: Temporarily Bypass Strict Host Checking (Testing Environments)

When spinning up automated disposable development containers, strict key verification can be bypassed temporarily.

  1. Pass custom runtime configuration parameters into your command:
    ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null [email protected]
  2. Caution: Never employ this flag on untrusted public networks or in production infrastructure, as disabling host checking exposes your credentials to MITM interception.

SSH Verification Troubleshooting Matrix

Scenario Risk Level Recommended Action
Server was freshly re-imaged / reinstalled Zero (Expected) Run ssh-keygen -R <hostname>
Connecting to cloud floating IP Zero (Expected) Delete specific line in ~/.ssh/known_hosts
Key changed unexpectedly on live production server High (Potential MITM or breach) Contact hosting provider / network admin to verify server fingerprint
Testing short-lived Docker / Vagrant VMs Low (Isolated network) Use UserKnownHostsFile=/dev/null flag

Removing obsolete cryptographic keys with ssh-keygen securely resolves host verification errors and restores SSH connectivity.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.