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:
- Review recent infrastructure changes: Did you rebuild the server, launch a new droplet/EC2 instance, or change hosting providers?
- 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 - 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:
- Execute the
ssh-keygen -Rcommand followed by the hostname or IP address of the target server:
(Replacessh-keygen -R 192.168.1.100192.168.1.100with your remote server’s actual IP address or domain name.) - 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" - The tool will output: # Host 192.168.1.100 found: line X … /home/user/.ssh/known_hosts.old created.
- Reconnect to your server:
ssh [email protected] - 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):
- Use
sedto delete the specific line number directly (replace15with the line reported in your error):sed -i '15d' ~/.ssh/known_hosts - Alternatively, open the file in nano:
nano ~/.ssh/known_hosts - Press
Ctrl + _(Ctrl + Underscore), type the line number, and press Enter to jump directly to the target line. - Press
Ctrl + Kto delete the entire line, save withCtrl + O, and exit withCtrl + 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...=:
- Do not attempt to search for the plain IP inside a hashed file manually.
- 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:
- Temporarily disable strict host checking on a per-command basis:
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null [email protected] - (Security Caution: Never apply this setting globally in production environments, as disabling strict checking removes protection against man-in-the-middle eavesdropping.)