1
votes

When i try to log in into EC2 instance through ssh, i got the below error Permission denied (publickey)

I checked the host name and username, everything is fine. Wrongly i given the chmod -R 777 . from ec2 instance root directory when i was logged at last time, After that i could not able connect to instance. I need some files from ec2-instance. Is there any way to get log in into ec2?. Also i tried with new instance. Its working.

Is there any possibility?

5

5 Answers

2
votes

I haven't tried this myself, but I don't see why it wouldn't work.

Try snapshotting your instance (create image button from ec2 console). Once complete, find your snapshot in the Ec2 console. It should be backed by an EBS volume with an id of the pattern "vol-xxxxxxxx".

Spin up a new instance and attach "vol-xxxxxxxx" as secondary storage. SSH to the new instance and mount the device "vol-xxxxxxxx" correlates to (e.g. /dev/xvdf) to a temp directory and find the files you're looking for.

2
votes
  1. Detach your root volume and attach to another instance.
  2. Login to the ec2 instance
  3. mkdir tempfolder
  4. sudo mount /dev/svdf1 (normally /dev/svdf1, you can list out your volumes to make sure)
  5. cd tempfolder/home
  6. chmod 700 -R ec2-user
  7. sudo umount tempfolder
  8. Detach volume and attach it to old instance, remember it's root instance so you attach it with name "/dev/xvda".
0
votes

I faced the similar problem. you will not able to re-cover the old instance, just create new instance and set the permissions chmod 777 (don't use -R) option, then your problem will be resolved.

0
votes

One reason can be that your key file is not publicly viewable for SSH to work. Use this command if needed:

  • chmod 400 mykey.pem

Also keep in mind the correct user id for EC2(ec2-user) instance and the command:

  • ssh -l ec2-user -i .ssh/yourkey.pem public-ec2-host
0
votes

Use Winscp to revert the permission change.

Recently I had accidentally changed the "/home/ec2-user" directory permissions to 777 using putty. I was immediately logged out. I was also logged into the server using "Winscp" and it didn't get disconnected after chaging the permissions.

The solution is change the permission on "/home/ec2-user" back to 700 using Winscp and I was able to log back in. It worked for me. Winscp saved me a lot of trouble.