How to fix Offending RSA key in /home/user/.ssh/known_hosts:line in SSH
This warning appears when SSH detects that a remote server's host key has changed since you last connected to it. The offending key is stored in your known_hosts file, and SSH refuses to connect until you remove or update the old key entry.
Quick fix
Fixes Offending RSA key in /home/user/.ssh/known_hosts:line
ssh-keygen -R hostname.example.com- Verify the server change is legitimate
- Remove the offending key using ssh-keygen
- Manually remove the specific line (if ssh-keygen doesn't work)
- Attempt to reconnect to the server
ssh-keygen -R hostname.example.comOffending RSA key in /home/user/.ssh/known_hosts:line
What this error means
SSH maintains a file (~/.ssh/known_hosts) that stores the public host keys of servers you've connected to. This prevents man-in-the-middle attacks by verifying that the server you're connecting to is the same one you previously trusted. When the warning "Offending RSA key in known_hosts" appears, it means the server's current host key doesn't match what's stored in your known_hosts file. This typically happens because the server was reinstalled, SSH was upgraded on the server, the host key was manually regenerated, or you're connecting to a different server on the same IP address. SSH refuses to connect automatically and requires you to remove the old key before proceeding.
Typical symptoms
- SSH connection is refused with a 'REMOTE HOST IDENTIFICATION HAS CHANGED!' warning
- Error message shows 'Offending RSA key in /path/to/.ssh/known_hosts:N' (where N is a line number)
- The same hostname or IP worked fine before but now fails
- Connecting to a newly rebuilt or reinstalled server fails verification
- SSH prompts to accept the new host key but shows the offending key error first
Common causes
- The remote server was reinstalled or recreated, generating a new host key
- SSH server was upgraded or reinstalled on the remote host
- The remote server's host key was manually regenerated or rotated
- You're connecting to a different server that now has the same IP or hostname
- The known_hosts file was corrupted or manually edited incorrectly
- DNS or network configuration changed, pointing the hostname to a different server
How to fix "Offending RSA key in /home/user/.ssh/known_hosts:line"
Before removing any keys, confirm that the server actually changed. Contact your system administrator or check your deployment logs if you deployed a new server instance. This prevents accepting keys from a compromised server during a man-in-the-middle attack.
If you just rebuilt the server yourself, this is expected behavior and safe to proceed.
Use the ssh-keygen -R command to automatically remove the old key from your known_hosts file:
ssh-keygen -R hostname.example.comOr if using an IP address:
ssh-keygen -R 192.168.1.100The removed key is automatically backed up to ~/.ssh/known_hosts.old for safety. This command removes all entries for that hostname or IP from known_hosts.
If you prefer to manually edit the file, open it with your editor:
nano ~/.ssh/known_hostsFind the line number mentioned in the error message and delete it. For example, if the error says "line 15", delete line 15. Save and exit.
Alternatively, use sed to remove a specific line number (replace 15 with your line number):
sed -i '15d' ~/.ssh/known_hostsTry connecting again using SSH:
ssh user@hostname.example.comSSH will now ask you to verify and accept the new host key. Carefully examine the fingerprint shown and confirm it matches what you expect (consult your server provider or administrator if unsure). Type 'yes' to accept and add the new key to known_hosts.
The output will show:
The authenticity of host 'hostname.example.com' can't be established.
ED25519 key fingerprint is SHA256:...
Are you sure you want to continue connecting (yes/no/[fingerprint])?After accepting the new key, confirm it's in your known_hosts file:
cat ~/.ssh/known_hosts | grep hostname.example.comYou should see one line with the new key. If you see multiple entries for the same host, use ssh-keygen -R again and reconnect to clean it up.
You can also inspect the key fingerprint at any time with:
ssh-keygen -l -f ~/.ssh/known_hosts -F hostname.example.comAdvanced notes
Advanced troubleshooting:
Clearing all known_hosts: If you have many servers and want to start fresh, you can remove the entire file (not recommended for production):
rm ~/.ssh/known_hostsYou'll be prompted to accept keys for each server on next connection.
Automated deployment scenarios: In CI/CD pipelines or scripts, use the -o StrictHostKeyChecking=accept-new option to automatically accept new keys without user interaction:
ssh -o StrictHostKeyChecking=accept-new user@hostname.example.comThis is safe only if you control the server and network.
Finding multiple entries for the same host: Sometimes known_hosts contains both hostname and IP entries for the same server. Remove both:
ssh-keygen -R hostname.example.com
ssh-keygen -R 192.168.1.100SSH key algorithms: Older SSH servers may use RSA-only keys, while newer systems prefer ED25519 or ECDSA. If connecting to legacy systems, make sure your SSH client supports RSA keys (some newer systems disabled RSA by default).
Frequently asked questions
- What does "Offending RSA key in /home/user/.ssh/known_hosts:line" mean?
- ssh/known_hosts) that stores the public host keys of servers you've connected to. This prevents man-in-the-middle attacks by verifying that the server you're connecting to is the same one you previously trusted. When the warning "Offending RSA key in known_hosts" appears, it means the server's current host key doesn't match what's stored in your known_hosts file.
- How do I fix "Offending RSA key in /home/user/.ssh/known_hosts:line"?
- This warning appears when SSH detects that a remote server's host key has changed since you last connected to it. The offending key is stored in your known_hosts file, and SSH refuses to connect until you remove or update the old key entry.
- What causes "Offending RSA key in /home/user/.ssh/known_hosts:line"?
- The remote server was reinstalled or recreated, generating a new host key SSH server was upgraded or reinstalled on the remote host The remote server's host key was manually regenerated or rotated
- Is the "Offending RSA key in /home/user/.ssh/known_hosts:line" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐SSH Official Documentation
- ๐