How to recover locked out AWS EC2 ssh machine

One time I accidentally messed with the `/etc/passwd` and locked myself out of being able to SSH into the machine. Since this is a remote machine in AWS I had no way of doing what I'd normally do. Which is attaching a keyboard and monitor and fixing this manually.

To fix, use the AWS EC2 Management page to:
- spin up a new instance of vanilla ubuntu EC2 (let's call it David)
- shutdown the locked machine (let's call it Goliath)
- unmount Goliath's volume
- attach the volume to David

Then follow this guide:

Summary of what I did from this guide:
sudo file -s /dev/xvdf # MBR (not data type)
sudo file -s /dev/xvdf1 # ext4
sudo mkdir mount_folder
sudo mount /dev/xvdf1 mount_folder # ext4 mounted
cd mount_folder
# undo crazy setting (see Note #1)
cd .. # to unmount
sudo umount /dev/xvdf1
# Note #2

Note #1: For me I tried to modify `/etc/ssh/sshd_config` to allow one more user to login. But this made me unable to login after. So I removed the offending line.

Note #2: now in the Volume webpage
- undo attach to David (Volumes tab)
- mount to Goliath (Volumes tab: attach as EBS path /dev/sda1)
- boot up Goliath (Instance tab)

Leave a Reply

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