EC2 SSH sudden keypair issue

I was normally working with the SSH on my server, then disconnected to re-connect using a different keypair, but received this message:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @
WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that a host key has just been changed. The fingerprint for the ECDSA key sent by the remote host is SHA256:****. Please contact your system administrator. Add correct host key in /home//.ssh/known_hosts to get rid of this message. Offending ECDSA key in /home//.ssh/known_hosts:4 remove with: ssh-keygen -f "/home//.ssh/known_hosts" -R ec2-.us-east-2.compute.amazonaws.com ECDSA host key for ec2-**.us-east-2.compute.amazonaws.com has changed and you have requested strict checking. Host key verification failed.

I insisted and didn't work. Deleted the known host and didn't work.

Now the old keypair doesnt work either if i try to connect to ubuntu@ec2-**.us-east-2.compute.amazonaws.com. The new error is

Permission denied (publickey).

The only way to connect is directly using the public IP. I tried flushing the DNS cache and nothing. What could've happened?


Solution 1:

You have a cached host key for that hostname in your ~/.ssh/known_hosts file. Did you previously connect to a host with this same name, but then replace the instance?

Either remove your ~/.ssh/known_hosts file entirely, or remove the entry for the hostname from the file in an editor.

Solution 2:

Use this to remove the ssh keys belonging to a hostname or IP from your known hosts file.

ssh-keygen -R [hostname-or-IP]