AWS — Recover lost EC2 Key-pair !

Avishek Roy
teckdevops
Published in
3 min readApr 24, 2019

--

Have you lost your running EC2 instance/s key pair and now wants to recover but thinking how ?

Simple, follow below steps :-)

STEPS:

  1. Stop the running EC2 instance (Say Instance1).

EC2 → Instances → Select concerned instance(Right Click)→ Instance State → Stop.

2. Detach root volume i.e. /dev/xvdf (say root)from Instance1.

EC2 → Volumes → Select Volume(Right Click) → Detach Volume.

3. Start a new t2.micro EC2 instance, in the same subnet and select to create a new key pair.

EC2 → Launch a new Instance(say InstanceTemp) → Create a new key pair .

4. Attach volume root to the InstanceTemp , as /dev/sdf.

5. Jump(ssh) to the InstanceTemp and mount volume root to /vol

sudo mkdir /vol
lsblk
sudo mount /dev/xvdf1 /vol/

MOUNT

6. Copy ssh keys to mounted volume via below command.

sudo cp ~/.ssh/authorized_keys /vol/home/ec2-user/.ssh/

COPY SSH KEYS

7. Exit from the InstanceTemp and Go back to EC2 Management Console.

8. Terminate the InstanceTemp.

9. Detach volume root from InstanceTemp.

10. Attach volume root back to the main instance as /dev/sda.

11. Start the Instance i.e. Instance1.

12. Login to Instance1 but now using the new key pair. Success ! Yippee!

TRY LOGIN

— A blog by teckdevOps

--

--