-4

I accidentally remove my pem file because of the "Permission denied (publickey)." error occuring when I log in.

So I found that I can not log in that instance anymore (I don't know why AWS has this bad policy).

So I try to copy instance to another instance. My old instance is Ubuntu 16, but I need to include every configuration files too.

So I found that AMI is used to copy instance.

But when I logged in new instance created from the AMI from old instance.

I got "Permission denied (publickey).".

I did all the tries that I can

  1. ssh -i MY_PEM.pem root@host
  2. ssh -i MY_PEM.pem ubuntu@host
  3. ssh -i MY_PEM.pem ec2-user@host
  4. ssh -i MY_PEM.pem AMI_NAME@host
  5. ssh -i MY_PEM.pem AMI_ID@host
  6. ssh -i MY_PEM.pem AMI_OWNER@host
halfer
  • 19,824
  • 17
  • 99
  • 186
LKM
  • 2,410
  • 9
  • 28
  • 53
  • Default user is most likely to be "ubuntu" or "ec2-user". Also see http://stackoverflow.com/questions/7881469/change-key-pair-for-ec2-instance. For lots more info on the topic – Nath Dec 17 '16 at 09:48
  • @Nath As you see above , i did try with "ubuntu@host" – LKM Dec 17 '16 at 09:49
  • What about ec2-user ? – Nath Dec 17 '16 at 09:50
  • @Nath 'root' is suggested in aws console so when i try with 'root' the connection is closed after msg 'Please login as the user "ubuntu" rather than the user "root".' is printed – LKM Dec 17 '16 at 09:53
  • ec2-user & ubuntu are the same as the result is 'Permission denied (public key)' – LKM Dec 17 '16 at 09:54
  • HOW COULD IT BE HAPPENDED..? IT'S CRAZY – LKM Dec 17 '16 at 09:55
  • Ok so the user is Ubuntu it just doesn't like your key.... Look at the other question for solutions – Nath Dec 17 '16 at 09:57
  • @LKM it's bit difficult to understand your question. Do you deleted pem key by mistake or did you delete public key in linux machine ? – Roshan Dec 17 '16 at 10:01
  • @roshan I did login as the way as I do before. But the result was 'Permission denied (public key)' so I deleted .pem file in my local machine thinking that regenerating is possible – LKM Dec 17 '16 at 10:03
  • So I followed this video exactly but the result is 'Permission denied (public key)' : https://www.youtube.com/watch?v=SwMh5lSh_JM – LKM Dec 17 '16 at 10:04
  • ok, i understand now, you lost private key so now you are not able to that machine. Am i correct ? – Roshan Dec 17 '16 at 10:05
  • @roshan Yes you're right!! so --> what i found is that regenerating key is impossible, --> so I tried AMI way but it prints the same message – LKM Dec 17 '16 at 10:08
  • 3
    Possible duplicate of [How to recover lost private key of instance of aws server?](http://stackoverflow.com/questions/32739421/how-to-recover-lost-private-key-of-instance-of-aws-server) – ydaetskcoR Dec 17 '16 at 10:14
  • 3
    Explain precisely why it's not a duplicate of that question. You seem to have lost your private key and now you can't log into your AWS instance. That is by design. If you want the data on the instance you will need to create a new instance using a new key pair, detach the volume from the old instance and then attach to the new instance. – ydaetskcoR Dec 17 '16 at 10:18

1 Answers1

1

When we loose private key, You can't login to that machine.

Please follow the below steps to recover the key.

Step 1) Detach your root volume from your machine using AWS console.
Step 2) Launch a fresh EC2 instance(Not from your old machine AMI)
Step 3) Attach your old volume to new EC2 machine
Step 4) Now login to new ec2 machine and mount the old EBS volume
Step 5) Now go to that partition then visit home directory inside that machine and go to .ssh folder. Step 6) Now generate a new private and public key. Then paste public key into authorized_keys file.
Step 7) Once you done with above steps, detach that volume from this ec2 machine.
Step 8) Now attach this volume to your old machine as root volume
Step 9) Now try to login to your old machine with the newly generated key.

Hope it helps !!

Roshan
  • 1,380
  • 13
  • 23