# Locked myself out of SSH port Ubuntu AWS server using ufw

**Published:** April 17, 2019
**Tags:** SSH

**Summary:** How to regain SSH access to an AWS EC2 Ubuntu instance after ufw locks you out, by disabling the firewall via a user-data script.


---

Currently, I locked myself out of my VPS (Amazon EC2) after configuring ufw firewall. Here are what I've done to disable ufw and take back the control of my VPS.

1. Stop your problem instance
2. Paste this script in `Instance Settings > View/Change User Data`

```
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
--//
```

3. Start your instance and now you should be able to SSH.

- Source: [https://anhthang.org/2018/06/29-disable-firewall-in-ec2-instance-from-aws-console](https://anhthang.org/2018/06/29-disable-firewall-in-ec2-instance-from-aws-console)

