SelestL Solutions
Technology

How to Fix SSH Remote Host Identification Changed

By SelestL • Published on
How to Fix SSH Remote Host Identification Changed

You open your terminal to connect to a cloud virtual machine, VPS, or internal staging server using ssh user@hostname. Instead of a password or passphrase prompt, your terminal erupts with an alarming security alert: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Host key verification failed. Connection is terminated immediately.

The SSH remote host identification has changed warning is an essential cryptographic safeguard built into the OpenSSH client. When you first connect to an SSH server, your client saves its unique public fingerprint to a local ~/.ssh/known_hosts file. If that fingerprint subsequently changes, SSH assumes a man-in-the-middle (MITM) attack is intercepting your encrypted communication and abruptly severs access.

While this warning protects against network spoofing, it most commonly occurs in legitimate scenarios: when a cloud server has been reinstalled, an IP address has been reassigned to a new EC2 instance, or SSH host keys were regenerated. In this guide, we detail how to inspect the changed key and safely update your local record. If you are hardening infrastructure security across cloud environments, explore our enterprise cloud and IT infrastructure services.

1. Why SSH Triggers the Host Identification Alert

1. Why SSH Triggers the Host Identification Alert

OpenSSH relies on Trust On First Use (TOFU) authentication. When establishing a secure channel, both sides exchange cryptographic host keys (typically ED25519 or RSA). If the server identity fails to match your cached fingerprint, one of these scenarios has occurred:

  • Server Reinstallation or OS Upgrade: Rebuilding a cloud server generates brand-new public and private host keys while retaining the same static public IP address.
  • Dynamic Cloud IP Reallocation: Terminating an instance in AWS, DigitalOcean, or Hetzner releases its IP address into the pool, where it is subsequently assigned to an entirely different server.
  • Load Balancer Port Forwarding: Connecting to a domain that routes traffic across multiple backend nodes with differing host keys without a centralized certificate authority.
  • Genuine Man-in-the-Middle Attack: An attacker on a public Wi-Fi network or compromised DNS server is attempting to intercept your SSH credentials.

Security Verification: Verify Before Deleting

Before clearing a host key on a critical production server, always verify via your cloud provider web console (AWS, Linode, or Azure serial log) that the server was recently rebuilt or rebooted. Never bypass this warning blindly on public networks.

2. Step-by-Step Fix: Removing the Stale Host Key

According to official specifications from the OpenSSH project, the most secure and precise method to clear a stale fingerprint is using the ssh-keygen -R utility:

Fix 1: Using ssh-keygen -R (Recommended)

Run ssh-keygen -R followed by the remote hostname or IP address. This selectively removes the invalid key while preserving all other cached hosts:

# Remove key by IP address
ssh-keygen -R 192.0.2.1

# Remove key by hostname or domain
ssh-keygen -R server.example.com

The command updates your ~/.ssh/known_hosts file and creates an automatic backup at ~/.ssh/known_hosts.old.

Fix 2: Manual Line Deletion with sed

The SSH error message explicitly states the exact line number of the offending key (for example: Offending ECDSA key in /home/user/.ssh/known_hosts:14). You can delete that specific line directly:

# Linux (delete line 14)
sed -i '14d' ~/.ssh/known_hosts

# macOS (requires empty string argument with -i)
sed -i '' '14d' ~/.ssh/known_hosts

3. Reconnecting and Accepting the New Fingerprint

Once the old key has been removed, initiate the connection again. OpenSSH will prompt you to verify and save the new host key:

ssh user@192.0.2.1
# Output:
# The authenticity of host '192.0.2.1' can't be established.
# ED25519 key fingerprint is SHA256:4tL9q...
# Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

Type yes and press Enter. The new fingerprint is written to known_hosts, and you will be authenticated normally.

4. Workarounds for CI/CD and Ephemeral Containers

In automated testing environments where virtual machines are destroyed and rebuilt continuously, host key verification can fail automated runs. You can instruct SSH to disable strict checking for specific hosts in ~/.ssh/config:

# Only use this configuration for ephemeral staging/testing environments
Host staging.internal
    StrictHostKeyChecking no
    UserKnownHostsFile /dev/null
    LogLevel ERROR

5. Diagnostic Summary Table

Method Command Safety Level Backup Created?
ssh-keygen -R ssh-keygen -R <hostname> High (Recommended) Yes (known_hosts.old)
sed line delete sed -i 'line_num d' ~/.ssh/known_hosts Medium No
StrictHostKeyChecking StrictHostKeyChecking=no Low (CI/CD only) N/A

To learn more about secure deployment automation and key management, read our tutorial on setting up CI/CD with GitHub Actions. For custom cloud infrastructure setup, reach out through our contact page.

6. Where to Go From Here

The SSH host identification warning is a cornerstone of modern internet cryptography. Keep your terminal authentication streamlined with these daily practices:

  • Never delete your entire known_hosts file to fix a single host conflict; always use ssh-keygen -R.
  • Use SSH certificates (SSH CA) in larger engineering teams to authenticate hosts dynamically without relying on static known_hosts lists.
  • Implement SSH config profiles in ~/.ssh/config to manage custom ports, usernames, and key files systematically.

Frequently Asked Questions

What does WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED mean in SSH?
It means the cryptographic fingerprint presented by the remote server does not match the public key stored in your local ~/.ssh/known_hosts file, indicating either a rebuilt server, an IP reassignment, or a potential MITM attack.
How do I remove a single changed SSH host key on Linux and Mac?
Run ssh-keygen -R <hostname_or_ip> in your terminal (for example: ssh-keygen -R 192.168.1.50). This removes the old entry and saves a backup in known_hosts.old.
Where is the known_hosts file located on Windows?
On Windows OpenSSH, the file is located at C:\Users\<YourUsername>\.ssh\known_hosts. You can run ssh-keygen -R <hostname> directly from Command Prompt or PowerShell.
Is it safe to ignore the remote host identification warning?
Only if you can independently confirm that the server was recently reinstalled, had its operating system upgraded, or had its IP address reassigned. On untrusted or public networks, never ignore this warning.
Tags: #SSH #Linux #Security #Sysadmin #DevOps #Networking

Related Articles