As we know that EC2 is a very useful service in AWS, to access EC2 instance commonly we are using key pair authentication method. There are many situations where we are not  able to log in the EC2 instance because of the .pem file lost from our end.

Here we are discussing how to access to a .pem key file lost ec2 instance.

For this example consider we have a .pem key lost instance with name ec2_instance and we are creating an EC2 instance to recovery with name ec2_recovery. Steps are given below

1. Stop the ec2_instance

We can use the option above to stop the ec2_instance.

2. Detach the ec2_instance volume

Go to the option volumes select detach volume corresponding to ec2_instance

Please note it the block device field before it will detach from ec2_instance.

3. Attach this volume to the ec2_recovery instance

Then log in to the ec2_recovery instance and perform following commands to copy the key from ec2_recovery instance to ec2_instance

a) $ lsblk

NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT

xvda   202:0   0   8G 0 disk

`-xvda1 202:1   0   8G 0 part

xvdf   202:80   0   8G 0 disk

`-xvdf1 202:81   0   8G 0 part

 

b) $ sudo mount /dev/xvdf1 /mnt

 

c) $ lsblk
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT

xvda   202:0   0   8G 0 disk

`-xvda1 202:1   0   8G 0 part /

xvdf   202:80   0   8G 0 disk

`-xvdf1 202:81   0   8G 0 part /mnt

d) $ cp /home/ec2-user/.ssh/authorized_keys /mnt/home/ec2-user/.ssh/authorized_keys

4) Stop the ec2_recovry instance and detach the ec2_instance volume and attached it to ec2_instance.

Please use device name as /dev/xvda.

5) We can start the ec2_instance after that and access using the key_recovery .pem file.

 

[tagline_box backgroundcolor=”description=” shadow=”no” shadowopacity=”0.7″ border=”1px” bordercolor=”” highlightposition=”top” content_alignment=”left” link=”” linktarget=”_self” modal=”” button_size=”” button_shape=”” button_type=”” buttoncolor=”” button=”” title=”” description=”If you have any queries on how to access .pem key file lost EC2 instance in AWS feel free to leave us a message and our representative will get back to you.

” margin_top=”50px” margin_bottom=”” animation_type=”slide” animation_direction=”left” animation_speed=”0.3″ class=”” id=””]

    [/tagline_box]