flush iptables rules if unable to ssh in ec2


linux ec2 aws

Howto flush iptables rules if unable to ssh in ec2 in aws console

Check “Instance log” and “Get system screenshot” in Instance Settings to check if system is up

1- Stop your instance

2- Go to Instance Settings -> View/Change user Data

3- Paste this

    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
ufw disable
iptables -L
iptables -F
--//

4- Start instance and check if ssh is working.

5- Resolve the issue and remove the text pasted in step 3. Reboot instance and check.