Search code examples
amazon-web-servicessshamazon-ec2rhel

How to recreate ec2-user home directory


I permanently deleted my ec2-user home directory on my rhel instance on amazon ec2

Thankfully I am still logged into my server

I don't have any backup copies of the directory or snapshots

I know I can recreate my public key from my private key and I also copied my public key to /home/ec2-user/.ssh/authorized_keys This is how I recreated my public key

ssh-keygen -y -f test.pem > test.pub

These steps aren't enough for some reason, I am still not able to ssh into my server

Am I missing any steps? Can someone guide me?

This is the error I get when I attempt to login

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    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
xx.
Please contact your system administrator.
Add correct host key in /Users/xx/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /Users/xx/.ssh/known_hosts:5
ECDSA host key for ec2-xx-xx-xx-xx.compute-1.amazonaws.com has changed and you have requested strict checking.
Host key verification failed.

Solution

  • copy your public key to /home/ec2-user/.ssh/authorized_keys create the .ssh directory and the authorized_keys file if they do not exist

    Do these commands as root

    chown -R ec2-user /home/ec2-user 
    chmod 700 /home/ec2-user/.ssh
    chmod 600 /home/ec2-user/.ssh/authorized_keys
    

    Then try and login. Think that will probably be enough for it to work