Steps to connect AWS Instance - Sumanastech
25
Feb

Lost your PEM files? Use these steps to connect AWS Instance!

Worried about your PEM files? Here we have listed out a set of strategies you need to follow to get connected with AWS Instance.

This process is applicable if you have lost your .pem or .ppk key.

  • Initially, you need to take a snapshot of the root volume of your instance.
  • Secondly, you can create a new instance (for temporary purposes) in the same AZ just as the original instance. And remember, it is highly suggested that the recovery instance should be same as OS.
  • Once you create a snapshot of the instance, you need to stop the original instance.
  • You need to detach the root EBS volume from the original instance and make sure you make a note of volume (example /dev/xda or /dev/sda).
  • Once you are done with detaching, refer to the status column. Ensure the status being changed to availability. Attach this detached volume to the newly created temporary instance.
  • Make sure you mention your instance name while attaching the volume and device name like /dev/sdf or /dev/xdf.
  • The next steps are to mount it in /mnt folder since it is a secondary folder.

Commands to list attached block devices:


$  lsblk

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

  • Mount the secondary device (dev/xvdf1) to ‘/mnt’ mount point using the command:

# mount /dev/xvdf1 /mnt/

  • If you need to change the authorized key in original instance, check the .ssh directory.
  • You need to create a backup of the old authorized key in the original instance.

/mnt/home/ubuntu/.ssh/authorized_keys to  /mnt/home/ubuntu/.ssh/authorized_keys_back

Using the command:

cp /mnt/home/ubuntu/.ssh/authorized_keys /mnt/home/ubuntu/.ssh/authorized_keys.bck

  • You need to append the new key to the authorized_keys file of the original instance

You can use the Command:

cat /home/ubuntu/.ssh/authorized_keys >>

/mnt/home/ubuntu/.ssh/authorized_keys

  • Once you have appended, unmount the original instance volume from the temporary instance.

You can perform this with the Command:

# umount /mnt

  • You can attach the volume back to the original instance. This can be done /dev/sda1 during the attach of the root volume back to the instance.
  • Finally, you can start the original instance and connect using temporary instance pem file and access file.

Wrap Up

We hope now you would be relaxed! You can make use of this proven track strategy if you have lost your .ppm or ppk key.

If you have any other suggestions or queries, do register your comments in the comments section below!

Thanks for reading!

Comments

[bws_google_captcha]