AWS — Disable UFW in EC2 linux instance
1 min readAug 9, 2021
Days ago, I ssh to my EC2 linux instance, and enabled ufw (uncomplicated firewall) by mistake. After I logged out, then I could’t ssh in again.
I tried to google options, one option is to create a new EC2 instance with the exsting volume (need detach). I feel like that is too much work, I bet there should be better solution.
Later, I found this thread in stackoverflow, https://stackoverflow.com/questions/41929267/locked-myself-out-of-ssh-with-ufw-in-ec2-aws, Vicente had the same problem as me.
Here is the solution,
- Go to your EC2 page
- Select the EC2 instance, stop the instancce
- Right click the EC2 instance, go Instance settings then Edit user data.
- Paste below code to Modify user data as text then save (you may remove sudo).
Content-Type: multipart/mixed; boundary="//" MIME-Version: 1.0 --// Content-Type: text/cloud-config; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="cloud-config.txt" #cloud-config cloud_final_modules: - [scripts-user, always] --// Content-Type: text/x-shellscript; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="userdata.txt" #!/bin/bash sudo ufw disable sudo iptables -L sudo iptables -F --//
- Restart your instance
- Verify your public IP address (normally it will be changed after your stop & restart
- SSH to the EC2 instance with new IP address.