AWS - Series How to connect an EC2 instance if you lost your keypair file ? Recover AWS keypair in AWS LINUX

 Hello Everyone

In this blog , we are going to see the below content and importantly to see, how to recover the lost keypair in EC2 Instance . This is one of most common scenario based questions in AWS

 

Table Content 

  • What is keypair
  • How to connect to an EC2 Instance in case of you lost the key pair of Linux2 [Accidentally deleted the key pair permanently] 
  • Create a new instance with name of MasterServer
  • Access the master  instance with keypair
  • Delete the keypair permanently
  • Create a new instance named "RecoveryServer"
  • Perform the task like  Deattach and attach the volumes between the master and recover Instance
  • Finally start the Master instance with recovery key pair 


What is keypair

A key pair, consisting of a public key and a private key, is a set of security credentials that you use to prove your identity when connecting to an Amazon EC2 instance

In this scenario, we are taking up two AWS linux2  instance from EC2

Masterserver01

Receoverserver01




Server got authenticated from Master Server keypair




Make one directory and keep some sample file




see the associate volume also attached on the respective master server







Now Delete  the PPK file from the saved location and try to login with same public Key




Create a new instance with same region with few extra storage , to identify the volume between master server and Recovery server

in our case 

master server has 8 GB

Recover server has 10 GB




Now , login to the instance of recover server along with the newly created  key pair




Now Stop the master server to detach the volume


go to volume and detach which was associated to master server




go to instance stop the Recover server 



At this point of time, both the instance are stopped 




Go to EBS - 

Select the Volume of Masterserver



Attach the volume to recover server





Now the both the volume are attached in to recover server



now go to instance and start the Recover server 




login to recover server using putty and check lsblk

In the below pic . Red circle belongs to recoverserver with root volume of  10 G

Blue Circle belongs to Masterserver with root volume of  8GB 


mount the volume with name of mnt

mount -o rw,nouuid /dev/xvdb1 /mnt





once its mounted, we need to copy the authorised_keys to Masterserver volume





cat /home/ec2-user/.ssh/authorized_keys >> /mnt/home/ec2-user/.ssh/authorized_keys




Now go to Recover server instance and make it sopped




Go the volume and select the master volume and select the detach





Select the same volume of and attach to the Master server instance which is in stopped state




Choose the customised device name in my case it is 

/dev/xvda



and attach

Start the Master instance and connect with recover keypair with putty and you are done 






I hope , this blog is helpful to AWS cloud admins , to recover the lost key pairs from Linux2 Instance. Happy Learning 😃


Comments