Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing EC2 instance after losing keypair

Tags:

amazon-ec2

I booted up an EC2 instance months ago and at that time I had the .pem key stored in my PC. The PC since crashed and I had to reinstall Windows on it and I don't have a backup of the .pem file

Is there any way at all for me to get to the prompt on the EC2 instance- any way to ssh or otherwise get to the instance?

like image 728
bachposer Avatar asked Oct 31 '11 03:10

bachposer


People also ask

Can we launch instance without Keypair?

Windows: Admin password (for which private key is required to decrypt) is not needed because some other user is an admin. Caution: If you launch an instance with no keypair and don't have any of the above mentined ways to access the instance, you have to relaunch the instance with a keypair.

What if I lost my pem file in AWS?

First, create a new instance by creating new access file, call it 'helper' instance with same region and VPC as of the lost pem file instance. Now stop the lost pem file instance. Remember not to terminate instance but to stop it. Go to EBS volumes, select the root volume of the lost pem file instance and detach.


3 Answers

Not easily.

Guidance on the forums indicate that you can kind of do it by generating a new key pair and then bringing up a new instance and mounting the volume from the original instance and installing the keypair in that volume. After that the original instance should be able to use the new keypair.

According to this post on AWS Developer Forums it can be accomplished via:

Creating a new keypair downloads the private key to your machine and the public key is stored in your AWS account. When you launch a new (linux) instance the public key is placed into the /root/.ssh/authorized_keys file (or /home/ubuntu/.ssh/authorized_keys for Canonical Ubuntu AMIs), allowing your private key access to the instance.

Losing the private key can be fixed by logging in to the instance via another linux account if you have set one up.

Alternatively, if you're running an EBS-backed instance then you can Stop it, attach the root EBS volume to another instance, and place a new public key into the authorized_keys file, then return the volume to the original (stopped) instance and Start the instance again.

If none of these work, then - sorry - you're out of luck. It's called a key because you can't get in without it.

like image 92
John Weldon Avatar answered Oct 22 '22 17:10

John Weldon


You have to create an image of an current instance and then use that image for launching new instance. All files and data would copy from that instance and use different key when launching instance, then access it with same key

like image 33
Ramprasad Avatar answered Oct 22 '22 19:10

Ramprasad


When we loose private key, You can't login to that machine. However, there is an another way to access that machine by generating a new key-pair

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

like image 2
Roshan Avatar answered Oct 22 '22 18:10

Roshan