Amazon Web Services

How to access ec2 ubuntu instance after locking out yourself outside root

Deep
Written by Deep

Hi guys, one of my friend looked himself out of root on AWS EC2 Ubuntu instance of a company client and was unable to login to that instance. He completely lost his access to that machine. After a lot of research we together found some useful working tips that saved our job. 😛

You can use these steps to get rid  of following issues:

  1. Connection Timeout (Logging in issue)
  2. Permission denied (publickey)

How to get access back for your instance ?

Step 1: 

Clone your current instance (Which is not accessable).

Go to Instances > Select Your instance > Action > Launch More like this

This will launch a duplicate instance. Just rename this instance (just for making your operation easy)

Step 2 : 

Go to Volumes and find out the volume that was attached to blocked instance. (You can find it with the help of description of the volume)

Select the volume  and detach it Go to  Action > Detach Volume.

 

Step 3 : 

Select the volume again and attach it to the new created instance.

Go to Action > Attach Volume

Select the newly created instance and set mount point or device name to /dev/sdh

(You can use /dev/sdi  or /dev/sdj based on availability. See here for more details on device naming)

Step 4: 

Login to newly created device and type following command

Create a directory in which you will access the blocked volume / disk.

List all volumes that are attached to this instance :

I found blocked volume that was named as /dev/xvdh and root was /dev/xvdh1

(Don’t worry if you don’t find /dev/ as prefix in your list. because shell automatic hides it. And the name may varry according the device name you setup at the time of attaching to this instance )

Now mount the blocked volume into current working disk at directory we created above

Step 5: 

Now you can edit the files which were blocking to access your old instance.

After completing your task don’t forget to unmount the blocked instance’s volume .

 

Step 6:

Stop your newly created instance.

Go to Instance > Select newly created instance > Action > Stop

 

Step 7 : 

Detach the blocked volume from newly created instance .

Go to Volumes > Find and select your old or blocked instance volume > Detach it.

Attach this volume to old / blocked instance.

Restart your old instance and you are done !

Don’t forget to delete / terminate newly created instance.

 

 

Leave a Comment