Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 438

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T10:53:08+00:00 2024-11-25T10:53:08+00:00

How To Recover Access To EC2 Instance After Losing Pem File(SSH Keys) 2022

  • 62k

Losing credentials or log-in details to any platform or Infrastructure can be frustrating and can have a lot of consequences which is why we must protect our security credentials with all intentions.

Imagine losing credentials to your vault and you can't access your money

Image description

Never lost my ssh keys tho. So this article is a request from someone and I'm writing just to show a walk-through of how to recover missing Pem file to an AWS instance. let's go

RECOVERING ACCESS TO MY EC2 INSTANCE AFTER LOSING PEM FILE

For Pem file recovery to be possible, the lunched instance with the missing Pem file must be an EBS-Backed Instance because it is not possible on an instance backed by an instant store.

Assume that your instance with lost Pem file is “Lost pem server”

I. To confirm the backed-storage type

  • Click/Check the instance.

  • Select the “storage” tab.

Image description

II. Launch a Recovery Instance

  • Launch the “Recovery Instance” in the same AZ as the “Lost pem server”

  • To Launch a New Instance in the same Availability Zone as the instance(Lost pem server) with the missing Pem file….Make sure to check which AZ the instance is running(The above is running in us-east-2a).

  • Select the same network(VPC) and subnet as the old instance(Lost pem server).

III. Create key pair for the new instance(Recovery server) and connect to it using ssh. Be sure you can connect to the instance.

IV. Now stop the old instance(Lost pem server).

V. Detach the EBS volume connected to the “Lost pem server”.

  • Click on the instance with lost Pem and select the “Storage” Tab.

  • Scroll down to blocked devices and click on the ID for the Root Device. In my case, the root device is dev/sda1.

Image description

  • Make sure the Root Storage is still selected, then click on Action

  • Finally, select “Detach volume”

Image description

  • Go to “volumes” under EBS(Elastic Block Store) and select the volume you previously detached from the “Lost pem server”

Image description

  • Select the volume and click “Action”
  • Select “Attach Volume”.

Image description

  • Select the recovery server(Recovery Server).

Image description

VI. Go back to the shh terminal to the “Recovery server” and check the EBS volume
NOTE: The attached EBS volume won’t mount automatically, so you have to mount it.

  • Go to your terminal and ssh into the Recovery server. In my case, I’m using WSL in visual studio.

Enter the following commands

I. Optionally: you might want to copy the pem file from where it is to the .ssh folder

cp /mnt/c/Users/YourName/desktop/pem/recovery.pem ~/.ssh/recovery.pem 
Enter fullscreen mode Exit fullscreen mode

II. Connect to the instance using SSH

III. To check the list of storage on the device to see if the attached EBS was mounted or not. Though it doesn’t mount automatically.

lsblk 
Enter fullscreen mode Exit fullscreen mode

Image description

NOTE: The attached EBS(XVDF) has not been mounted to any path as you can see in the screenshot above.

IV. Also, confirm that the attached EBS is not empty because u can’t mount an empty EBS.

sudo file -s /dev/xvdf 
Enter fullscreen mode Exit fullscreen mode

Output: /dev/xvdf: DOS/MBR boot sector, extended partition table (last)

Output like this means the drive is not empty, so you can go ahead and mount.

V. Create a temporary directory to mount the EBS volume(for the Lost pem server) in the Recovery server.

sudo mkdir /mnt/tempvolume  
Enter fullscreen mode Exit fullscreen mode

The path = /mnt/tempvolume

VI. Mount the storage on path /mnt/tempvolume. Mount the drive with a number at the end “/dev/xvdf1”

/dev/xvdf1 is the disk to be mounted.

sudo mount /dev/xvdf1 /mnt/tempvolume  
Enter fullscreen mode Exit fullscreen mode

VII. Check if the EBS has mounted

lsblk   
Enter fullscreen mode Exit fullscreen mode

Image description

To check the list of storage on the device again.

VIII. Copy the SSH key of your “Recovery server” into the attached drive belonging to the “Lost pem server”

cp .ssh/authorized_keys /mnt/tempvolume/home/ubuntu/.ssh/ 
Enter fullscreen mode Exit fullscreen mode

NOTE: Ubuntu in path /mnt/tempvolume/home/ubuntu/.ssh/ is the server name, it can be ec2-user if u launched linux server.

IX. Check the list of contents in the temporary location on the storage you copied the key to.

ls -lah /mnt/tempvolume/home/ubuntu/.ssh/  
Enter fullscreen mode Exit fullscreen mode

Image description

The key “Authozied_keys” is now in the folder you copied it to.

X. Unmount the attached storage from the “Recovery server so you can attach it back to the “Lost pem server”.

sudo umount /mnt/tempvolume/ 
Enter fullscreen mode Exit fullscreen mode

XI. Check if the disk has unmounted.

lsblk 
Enter fullscreen mode Exit fullscreen mode

Image description

The mount point of /dev/xvdf1 is no longer /mnt/tempvolume

XII. Now detach the attached “Lost pem EBS” from the “Recovery server” to attach it back to its original server “Lost pem server”.

Image description

XIII. Attached the EBS to the “Lost pem server” and edit the name to /dev/sda1 and save.

Image description

Image description

To confirm the time the EBS was attached, check “Attachment time”

Image description
XIV. Connect to the “Lost pem server” with the key of the “Recovery Server”.

ssh -i "ggfgvfv.pem" ubuntu@ec2-3-145-xxx-92.us-east-2.compute.amazonaws.com 
Enter fullscreen mode Exit fullscreen mode

XV. Connect to the “Lost pem server” with the key of the “Recovery Server”.

Image description

NOTE: Don’t forget to terminate the instance if it’s not a free tier instance to avoid extra cost.

Thank you and next time don't misplace your key 😄

awsclouddevopswebdev
  • 0 0 Answers
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.