Can’t connect/SSH to Linux EC2 instance anymore
Chances are that you’re here because you did something stupid. You locked yourself out of your EC2 instance somehow. Maybe you’ve played with the firewall settings on the machine, maybe you don’t know what you’ve done. Been there, done that. Luckily, there’s a workaround.
In my case, I’ve messed up UFW config on my Ubuntu instance. If you messed up some other configuration, you might have to edit a different file, but the basics of workaround are the same – attaching a disk from the instance you locked yourself out to the recovery instance you’ll create.
Here are the steps:
- Create a new recovery instance (the same as the one you messed up)
- Stop the problematic instance (DO NOT TERMINATE!)
- Detach the volume (problematic volume) from the original instance
- Attach it to the recovery instance as /dev/sdf
- SSH to the created recovery instance
- Run $ sudo lsblk to display the attached volumes and confirm the name of the problem volume. It usually begins with /dev/xvdf
- Mount problem volume: $ sudo mount /dev/xvdf1 /mnt
- Open the UFW configuration file: $ sudo vim /mnt/etc/ufwufw.conf
- Change ENABLED=yes to ENABLED=no and save
- Unmount volume: $ sudo umount /mnt
- Detach the problematic volume from the recovery instance and re-attach it to the original instance as /dev/sda1.
- Start the original instance and you should be able to log back in.