How to Configure UFW (Uncomplicated Firewall) on Ubuntu: A Complete Step-by-Step Security Guide

 Managing server security is one of the most critical tasks for system administrators and developers. On Linux systems, a firewall acts as the first line of defense against unauthorized traffic and malicious network attacks.

While Linux includes the powerful iptables utility by default, configuring it can be complex for beginners. That is where UFW (Uncomplicated Firewall) comes in. Designed as a user-friendly frontend for iptables, UFW allows you to manage network traffic rules easily without mastering complex commands.

In this guide, we will walk you through setting up and configuring UFW on Ubuntu step by step.

1. What is UFW and Why Use It?

UFW (Uncomplicated Firewall) is the default firewall configuration tool on Ubuntu Linux systems. It simplifies network management by offering:

Simple Syntax: Easy-to-remember commands for opening and closing ports.

IPv6 Support: Full compatibility with modern network setups.

Application Profiles: Pre-configured rules for common server applications like Apache, Nginx, and SSH.

2. Check UFW Status

By default, UFW is installed on Ubuntu but remains disabled. Before enabling it, check its current status by running:sudo ufw status verbose

If it shows Status: inactive, your firewall is currently turned off.

3. Set Default UFW Policies

Before opening specific ports, it is a best security practice to establish default traffic policies:

Block all incoming connections to prevent unauthorized access.

Allow all outgoing connections so your server can download updates and communicate with external services.

Run the following commands:sudo ufw default deny incoming

sudo ufw default allow outgoing

4. Allow SSH Connections (Crucial Step!)

⚠️ Important: Before turning on UFW, you MUST allow SSH connections. If you enable UFW without allowing SSH, you will be locked out of your remote server!

To allow standard SSH access on port 22:sudo ufw allow ssh

If you use a custom SSH port (e.g., port 2222), allow it specifically:sudo ufw allow 2222/tcp

5. Enable UFW Firewall

Once your SSH rule is in place, activate UFW with the following command:sudo ufw enable

Type y and press Enter when prompted to proceed.

6. Allow Common Web Services (HTTP/HTTPS)

If your server hosts a website or web application, you will need to open ports 80 (HTTP) and 443 (HTTPS):

Allow HTTP (Port 80):sudo ufw allow http

Allow HTTPS (Port 443):sudo ufw allow https

Alternatively, if you use Nginx or Apache, you can use application profiles:sudo ufw allow 'Nginx Full'

# or for Apache

sudo ufw allow 'Apache Full'

7. How to Delete a UFW Rule

If you accidentally open a port or no longer need a rule, you can delete it easily:

1.List all active rules with numbers:sudo ufw status numbered

2.Delete the rule by its number (e.g., rule number 2):sudo ufw delete 2

8. Conclusion

Setting up UFW on your Ubuntu server takes only a few minutes, but it provides a robust security layer against automated scans and malicious attacks. Always remember to allow your SSH port before turning on the firewall to ensure continuous server access.

Comments

Popular posts from this blog

Linux Server Hardening Best Practices: Essential Steps to Secure Your Server

Top 10 Essential Linux Commands for DevOps Engineers

Quantum Computing in 2026: Why Traditional Encryption is Facing Its Biggest Threat