How to Open Ports on AWS EC2 with UFW: Secure Firewall Configuration Guide
Secure your AWS EC2 instance with UFW. Learn how to install and configure UFW to open SSH, MySQL, and HTTP ports while protecting your cloud server.

If you've ever worked with AWS EC2 instances, you know that keeping your instance secure is crucial. One way to do this is by managing your firewall, and in this blog post, well go over how to configure UFW (Uncomplicated Firewall) on your EC2 instance to allow specific ports—like SSH (port 22), MySQL (port 3306), and HTTP (port 80)—so you can connect to your instance and run services smoothly.
Why Use UFW on AWS EC2? Benefits of Uncomplicated Firewall
On Ubuntu and other Debian-based systems, UFW is a straightforward command-line interface for controlling firewall rules. Because it is easy to set up and still provides a high degree of security, it is ideal for EC2 instances. Allowing the traffic you require while keeping unnecessary ports open to the internet is the aim here.
What You Need Before Starting UFW on EC2
Before diving in, make sure:
- Your EC2 instance is running Ubuntu or another Debian-based Linux distribution.
- You have SSH access to the instance.
- UFW is installed (well check and install it if necessary).
How to Open Ports on EC2 Instance Using UFW: Step-by-Step Instructions
1. Check if UFW is Installed
First, let's check if UFW is installed on your EC2 instance. Connect to your EC2 instance and run:
sudo ufw status
If UFW is not installed, the command will return:
ufw: command not found
In that case, install it with:
sudo apt update
sudo apt install ufw
2. Allow Specific Ports
Now, let's open the ports you need:
# Allow SSH (port 22)
sudo ufw allow 22
# Allow MySQL (port 3306)
sudo ufw allow 3306
# Allow HTTP (port 80)
sudo ufw allow 80
These commands let traffic through on the specified ports, ensuring smooth access to your instance.
3. Enable UFW
If UFW is not already enabled, activate it by running:
sudo ufw enable
To verify, check the status:
sudo ufw status
You should see:
To Action From
-- ------ ----
22 ALLOW Anywhere
3306 ALLOW Anywhere
80 ALLOW Anywhere
4. Optional: Restrict Access to Specific IPs
You may want to restrict access to particular IPs for extra security. For instance, to only permit SSH from your IP:
sudo ufw allow from 203.0.113.0 to any port 22
You can do the same for MySQL and HTTP:
sudo ufw allow from 203.0.113.0 to any port 3306
sudo ufw allow from 203.0.113.0 to any port 80
This adds an extra layer of security by preventing unwanted access.
5. Verify Your Firewall Rules
Run the following command to check active rules:
sudo ufw status
This confirms which ports are open and from which IPs they can be accessed.
Troubleshooting Common Issues
Fix SSH Access Issues on EC2 After Enabling UFW
If you cant connect to your EC2 instance via SSH after enabling UFW, make sure port 22 is open:
sudo ufw allow 22
Also, check your AWS Security Group settings and ensure SSH is allowed. You can review AWS security group rules here.
Troubleshooting MySQL Port (3306) Access on EC2 with UFW
Ensure port 3306 is open and verify that your database allows remote connections.
Troubleshoot HTTP Port (80) Access on EC2 with UFW and Security Groups
Check if port 80 is open and confirm that your EC2 security group allows inbound HTTP traffic.
Final Thoughts: Secure Your EC2 Instance with UFW
You now know how to use UFW to open particular ports on your EC2 instance, enabling HTTP, MySQL, and SSH communication while restricting access to unwanted ports. This keeps your server safe while guaranteeing that critical services run correctly.
Related Reads
Want to dive deeper into AWS and cloud automation? Check out these blogs:
Automating Deployment and Scaling in Cloud Environments like AWS and GCP
Learn how to streamline your deployment processes and scale efficiently across cloud platforms like AWS and GCP.
Unleash the Power of AWS DevOps Tools to Supercharge Software Delivery
Explore the tools AWS offers to enhance your software delivery pipeline, improving efficiency and reliability.
Step-by-Step Guide to Multi-Cloud Automation with SkyPilot on AWS Step-by-Step Guide to Multi-Cloud Automation with SkyPilot on AWs
Deploy on Nife
Ready to deploy? Check out the Nife documentation:
From the team at Nife
Deploy your next app in minutes, not days.
Nife is a multi-cloud edge platform that lets you deploy, scale, and manage applications globally without the infrastructure headache.