Handling SSH Host Key Changes the Right Way
When you reprovision a server or replace hardware, SSH warns about a changed host identification. Here's the clean, safe method to remove the old key and trust the new one using ssh-keygen.
That scary SSH warning about a changed host identification is usually harmless when you've intentionally replaced hardware, reinstalled an OS, or reprovisioned a machine. Here's how to cleanly remove the old host key and trust the new one.
πWhat's Actually Happening
When you SSH to a remote host for the first time, your client stores that machine's unique host key in ~/.ssh/known_hosts. This fingerprint helps protect against man-in-the-middle attacks.
If you later connect to the same IP address or hostname but the host key has changed, SSH raises an alarm because it could mean:
- Legitimate change: You reinstalled the OS, swapped hardware, or reprovisioned the system
- Security threat: Someone is intercepting your connection (rare in trusted networks)
β The Clean & Safe Fix
The recommended approach uses ssh-keygen to surgically remove only the offending entry from your known_hosts file.
- 1Remove the old host key by IP address
SSH tells you exactly where the problem is. Use that line number or target the IP directly:
bashssh-keygen -R 192.168.9.72This removes only the entry for
192.168.9.72, leaving the rest of yourknown_hostsintact. - 2If you also connect by hostname, remove that toobash
ssh-keygen -R atlas-piThis ensures the hostname entry is also cleaned up.
- 3Reconnect and accept the new key
Now SSH again:
bashssh [email protected]You'll see the fingerprint prompt:
textThe authenticity of host '192.168.9.72' can't be established. ED25519 key fingerprint is SHA256:... Are you sure you want to continue connecting (yes/no)?Type
yesto store the new key.
π§Alternative Approaches
While ssh-keygen -R is cleanest, here are other methods:
Option 2: Manually Edit known_hosts
SSH tells you exactly which line is the problem. You can open the file and delete that specific line:
# Open in your preferred editor
nano ~/.ssh/known_hosts
# or
code ~/.ssh/known_hosts
# Delete the offending line (e.g., line 13), save, and exitOption 3: Nuclear Option (Not Recommended)
Delete the entire known_hosts file:
rm ~/.ssh/known_hostsπBest Practices for Long-Lived Infrastructure
If you frequently reprovision nodes in a homelab or cluster:
β Prefer Hostnames Over IPs
If your DNS is reliable, connect by hostname:
ssh atlas-pi.internal.example.netThis avoids IP collision issues when IPs get reused across different machines.
β Expect Host Key Changes
When you reinstall an OS, swap hardware, or replace nodes, this SSH warning is a feature, not a bug. It's working as designed.
β Optional: Preload Host Keys (Advanced)
For automation (Ansible, provisioning scripts), you can pre-fetch and store host keys:
ssh-keyscan atlas-pi >> ~/.ssh/known_hostsUseful for non-interactive SSH scenarios where you can't manually accept keys.
πQuick Reference
Remove old key and reconnect:
# Remove by IP
ssh-keygen -R 192.168.9.72
# Remove by hostname (if applicable)
ssh-keygen -R atlas-pi
# Reconnect
ssh [email protected]
# Type "yes" when promptedssh-keygen -R <IP> to remove the old key, then ssh again and type yes to trust the new one. Clean, surgical, safe.Filed under: SSH, Security, Linux, CLI, known_hosts, Infrastructure