Locked out of root account on EC2 Ubuntu instance

amazon ec2

I managed to lock myself out of the root account of an Amazon EC2 Ubuntu instance by changing some permissions to .ssh.

Below are the steps I tried and failed miserable.

  1. Create a new, temporary instance (created a volume by itself).
  2. Stop the old instance. At this step, I am able to login to new instance.
  3. Detach the EBS volume from the old instance (Volume was /dev/sda1).
  4. Attach the old volume to new instance on /dev/sdb1.

Actual result:
5. Reboot the new instance and now can't login and permission key error.
PS: If I dont reboot the new instance, I am able to login but when I am mounting using mount /dev/sdb1 /mnt and I get an error drive not found.

Expected result:
5. Mount the device in new instance.
6. Edit the sudoers file.
7. Unmount the device.
8. Detach the volume.
9. Attach the volume to old instance on /dev/sda1.
10. Boot old instance.
11. Link the old instance back to the elastic IP address.
12. Start breathing again.
13. Delete the temporary instance.

Best Answer

Not sure if this will work but instead of what you did above.

Attach the old EBS drive instance to /dev/sdb2 and don't detach the current /dev/sdb1 as this is running an instance that you can still SSH into.

Inside the new running instance you should be able to run

sudo mount /dev/sdb2 /mnt
chmod /home/ubuntu/.ssh 700

# assumes the home directory is the default ubuntu one 
# for ubuntu/debian instances.

Followed by:

sudo umount /mnt

Now detach the drive in EC2 console and reattach it to your old instance and start it back up. Hopefully now you'll be able to ssh back in.