Setting Up a Firewall on Ubuntu (UFW Guide)

A firewall is one of the most essential layers of protection for your VPS.
It helps you control which network traffic is allowed in or out of your server — reducing the risk of unauthorized access and attacks.

In this guide, you’ll learn how to set up and manage a firewall on Ubuntu using UFW (Uncomplicated Firewall).


🔰 1. What Is UFW?

UFW is a user-friendly firewall management tool built on top of iptables, designed to simplify Linux firewall configuration.
It’s available by default on most modern Ubuntu systems.


⚙️ 2. Check UFW Status

To verify if UFW is installed and active, run:

sudo ufw status

If you get Status: inactive, you can enable it after configuration.


🧩 3. Allow SSH Access First

Before enabling the firewall, make sure SSH access is allowed — otherwise you’ll lock yourself out!

sudo ufw allow 22/tcp

If you’ve changed your SSH port, adjust it accordingly (e.g. port 2222).


🌐 4. Allow Web and Other Common Services

Allow HTTP and HTTPS for web traffic:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

You can also allow other ports as needed, such as:

sudo ufw allow 21/tcp    # FTP  
sudo ufw allow 25/tcp    # SMTP (Mail)  
sudo ufw allow 3306/tcp  # MySQL (Database)

🚀 5. Enable the Firewall

After setting your rules, enable UFW:

sudo ufw enable

You’ll see:

Command may disrupt existing ssh connections. Proceed with operation (y|n)?
Type y and press Enter.


🔍 6. Manage and Review Rules

List active rules:

sudo ufw status numbered

Remove a rule by its number:

sudo ufw delete 3

Disable the firewall (not recommended):

sudo ufw disable

✅ 7. Summary

You now have a basic firewall protecting your Ubuntu VPS.
Recommended setup for Tudcloud VPS users:

  • Keep SSH (22 or your custom port) open
  • Allow web ports (80, 443)
  • Deny all other traffic by default
  • Combine with Tudcloud DDoS protection for maximum security

🧩 Conclusion

A properly configured firewall is your first defense line.
Once UFW is active, your VPS becomes significantly more secure against attacks.