How to Fix ‘Host Key Verification Failed’ in Ubuntu SSH

Attempting to connect to a remote server or git repository via SSH from an Ubuntu terminal can be abruptly terminated with a security warning: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Host key verification failed
. OpenSSH terminates the connection immediately to protect against potential man-in-the-middle (MitM) attacks. However, this error is far more commonly triggered by legitimate events: a server OS reinstallation, IP address reassignment, cloud instance rebuild, or host key rotation.

Resolving this error safely requires verifying the remote host fingerprint and removing the outdated cryptographic entry from your local known_hosts file using the ssh-keygen utility.

Why Does OpenSSH Report ‘Host Key Verification Failed’?

When you connect to an SSH server for the first time, OpenSSH stores the server’s public host key fingerprint in your local ~/.ssh/known_hosts file. On subsequent connections, OpenSSH compares the presented key against the stored fingerprint. Verification fails when:

  • Remote Server Reinstallation: The remote server was reinstalled, formatted, or rebuilt from a cloud image, generating fresh SSH host keys while retaining the same IP address or domain.
  • DHCP IP Address Reassignment: In dynamic environments, a different physical server or container received the IP address previously occupied by a different machine.
  • SSH Daemon Key Rotation: System administrators upgraded security algorithms (e.g. rotating from legacy RSA keys to Ed25519 host keys).
  • Load Balancer with Multiple Backends: Multiple server instances behind an IP address lack synchronized host keys.
  • Actual Man-in-the-Middle Interference: A rogue device or compromised network gateway is attempting to intercept and decrypt your SSH session.

Step 1: Verify the Authenticity of the Host Key Change

Before deleting security warnings, confirm that the key change is expected and legitimate:

  1. Review recent infrastructure changes: Did you rebuild the server, launch a new droplet/EC2 instance, or change hosting providers?
  2. If you manage the remote server, obtain its authentic host key fingerprint directly via cloud serial console or local terminal:
    ssh-keygen -l -f /etc/ssh/ssh_host_ed25519_key.pub
  3. Compare this fingerprint against the fingerprint printed in the SSH warning output. If they match, the change is legitimate and safe to update.

Step 2: Remove the Outdated Key Using ssh-keygen -R

The standard, secure method to purge a mismatched host key without corrupting other entries in your known_hosts file:

  1. Execute the ssh-keygen -R command followed by the hostname or IP address of the target server:
    ssh-keygen -R 192.168.1.100
    (Replace 192.168.1.100 with your remote server’s actual IP address or domain name.)
  2. If the remote server operates on a non-standard port (e.g. port 2222), enclose the host in brackets and include the port:
    ssh-keygen -R "[example.com]:2222"
  3. The tool will output: # Host 192.168.1.100 found: line X … /home/user/.ssh/known_hosts.old created.
  4. Reconnect to your server:
    ssh [email protected]
  5. When prompted: Are you sure you want to continue connecting (yes/no/[fingerprint])?, type yes and press Enter to register the new key.

Step 3: Manually Delete the Offending Line from known_hosts

The error message explicitly identifies the line number containing the mismatched fingerprint (e.g. Offending ECDSA key in /home/user/.ssh/known_hosts:15):

  1. Use sed to delete the specific line number directly (replace 15 with the line reported in your error):
    sed -i '15d' ~/.ssh/known_hosts
  2. Alternatively, open the file in nano:
    nano ~/.ssh/known_hosts
  3. Press Ctrl + _ (Ctrl + Underscore), type the line number, and press Enter to jump directly to the target line.
  4. Press Ctrl + K to delete the entire line, save with Ctrl + O, and exit with Ctrl + X.

Step 4: Handling Hashed known_hosts Files

Modern Ubuntu installations enable HashKnownHosts yes by default in /etc/ssh/ssh_config, obscuring hostnames behind hashes such as |1|aB3...=:

  1. Do not attempt to search for the plain IP inside a hashed file manually.
  2. Always use ssh-keygen -R <hostname-or-ip> as described in Step 2; OpenSSH calculates the HMAC hash internally to locate and delete the matching record cleanly.

Step 5: Temporary Bypass for Ephemeral Cloud Test Environments

If you are orchestrating transient CI/CD pipelines, Vagrant, or dynamic Docker test containers that constantly cycle IP addresses:

  1. Temporarily disable strict host checking on a per-command basis:
    ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null [email protected]
  2. (Security Caution: Never apply this setting globally in production environments, as disabling strict checking removes protection against man-in-the-middle eavesdropping.)

Get the best tech tips delivered straight to your inbox.

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