AWS — Disable UFW in EC2 linux instance

Robin Ding
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,

  1. Go to your EC2 page
  2. Select the EC2 instance, stop the instancce
  3. Right click the EC2 instance, go Instance settings then Edit user data.
  4. 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 --//
  1. Restart your instance
  2. Verify your public IP address (normally it will be changed after your stop & restart
  3. SSH to the EC2 instance with new IP address.

--

--