Lost your Amazon EC2 Keypair?

Here’s How to Regain Access and Connect to Your Instance Step-by-Step

·

2 min read



In this, we have created two instances (webserver and appserver) of which one has lost its keypair.

We lost our keypair for the appserver Instance.

Step 1: Stop the appserver Instance ( to detach the volume )

Step 2: Detach the appserver instance volume ( Actions — Detach volume)

Step 3: After Detach, Select the same Volume, go to Actions and Attach Volume.

Step 4: Attach the volume to the instance for which we have the key (webserver instance)

Keep the device name as the default (/dev/sdf).

Click → Attach Volume ( Now the appserver’s detached volume is attached to the webserver )

Step 5: Connect the webserver instance and run the following commands.

sudo su
mkdir -p /var/newkey

#To see all the volume connected
lsblk

xvda — root volume , xvdf — attached volume

#Mount the volume to the folder
mount -o nouuid /dev/xvdf1 /var/newkey

The above command is used to mount a filesystem on the device “**/dev/xvdf1*” to the directory “/var/newkey” with the “nouuid” option. nouuid means not to follow the UUID (universally Unique Identifier).*

#overwrite the authorized keys
cp /home/ubuntu/.ssh/authorized_keys /var/newkey/home/ubuntu/.ssh/authorized_keys

The command is copying the “authorized_keys” file from the original “.ssh” directory to a new location under “/var/newkey/home/ubuntu/.ssh/”

#Finally unmount the volume
umount /var/newkey

Step 6: Stop the webserver instance ( to detach the appserver’s volume)

Volume — Detach Volume (appserver instance volume)

Volume — Attach Volume

Select the appserver instance

Rename to → “/dev/xvda” ,which is referred to the root or primary disk of the system

Step 7: Start the webserver and appserver instances.

Now connect the appserver instance with the webserver’s keypair

Congratulations! You have successfully connected to the instance using the key pair you possess.

Thank you for your support!

Don’t forget to show your appreciation by following me ❤️ for more captivating blog posts.