How to connect to a Ubuntu EC2 instance if the private key is lost.

Updated 20 June 2022

EC2 is the technological stack’s main computational component. In reality, it makes developers’ lives simpler by offering safe, scalable computational resources in the cloud.

It substantially simplifies the process of scaling up or down and can connect with a variety of different services, and comes with a pay-as-you-go model.

To encrypt and decode information, Amazon used public-key technology. It does this by employing a pair of keys: a public key and a private key.

The client then receives the Private key and uses it to decode the data that the server has encrypted.

To be able to log into an instance, you must first establish a “Key Pair” and give it a name. Then you’ll log in to that EC2 Instance using the Private Key.

In fact, there is another way also to retrieve your Instance key pair if you’ve misplaced it. We have a simpler way to accomplish it, but I’d want to challenge myself to learn more about EC2 Instances, so I’ll run this test.

There is no way to modify the key pair associated with an instance at the metadata level once it has been launched, but you may change the ssh key you use to connect to it.

To change which ssh key you use to access that instance, modify the authorized keys file on the instance and replace the previous one with your new ssh public key. Your new public ssh key (The authorized keys) file is located in the .ssh subfolder of the user you’re logged in as’s the home directory.

Step 1: First, Stop your original instance.
Step 2: Create one temporary ubuntu instance in the same region as well as in the same Availability Zone.
Step 3: Then, Detach the root original ‘EC2 volumes’ from the original instance.
Step 4: Attach those EC2 original volumes to the temporary instance.

Logging with Temporary Instance

Run these below commands-

a- First, Run this command – lsblk -f

b- Create one directory under /mnt

c- mkdir -p /mnt/tempvol

d- mount with root volume of original ec2 instance.

e- mount /dev/xvdf1 /mnt/tempvol

f- copy the .ssh/authorized key to root volume of original ec2 instance.

g- cp /home/ubuntu/.ssh/authorized_keys /mnt/tempvol/home/ubuntu/.ssh/

h- umount the root volume of original ec2 instance.

i- umount /mnt/tempvol

After that go to the AWS console-

1- First, Detach the original root volume from temporary ec2.

2- Then, Re-attach the original volume to the original ec2 as the root volume.

3- Start the original ec2 instance.

4- Now connect original ec2 from temporary instance key-pair.

Conclusion

In this way, you can regain the missing Key pair and then get connected to your original EC2 instance. You can stay connected for more updates.


For further help or query, please contact us or raise a ticket.

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project






    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home