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 MOUNTPOINTxvda 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.
” margin_top=”50px” margin_bottom=”” animation_type=”slide” animation_direction=”left” animation_speed=”0.3″ class=”” id=””]
Leave A Comment
You must be logged in to post a comment.