How to copy the old AWS instance?

75 views Asked by At

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
1

There are 1 answers

0
Roshan On

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 !!