Sysadmin

UFW Firewall Ubuntu: Complete Configuration from A to Z (2026)

UFW Firewall Ubuntu: Complete Configuration from A to Z (2026)

Securing a Linux server—especially an Ubuntu instance—is a top priority for any sysadmin. A firewall acts as your first line of defense, serving as an essential checkpoint that dictates what traffic can reach your system and what gets dropped. Ignoring this layer leaves the door wide open to port scans, brute-force attacks, and exploit attempts. Thousands of bots constantly scan the web for exposed servers; don’t be an easy target.

In this guide, we’ll dive deep into UFW (Uncomplicated Firewall), Ubuntu’s default, user-friendly tool for managing Netfilter rules. I’ll walk you through a step-by-step methodology to configure UFW effectively, ensuring your system remains secure without sacrificing functionality. From basic setup and default policies to advanced rules, logging, and troubleshooting, you’ll be equipped to lock down your Ubuntu machines with confidence. My goal is to help you master UFW, turning complex firewall management into a straightforward, robust process.

Prerequisites and Test Environment

To follow along, you’ll need an Ubuntu server or VM (preferably the latest LTS, like Ubuntu 22.04) with sudo privileges. All commands will be executed from the terminal. Make sure you have a working SSH connection before you start—this prevents you from accidentally locking yourself out while configuring rules. This guide covers everything you need to know about configuring UFW on Ubuntu for professional environments.

Installing and Enabling UFW

UFW usually comes pre-installed on Ubuntu, but it’s always good practice to verify and install it if necessary. Once installed, you can enable it. Remember: enabling UFW without first configuring an SSH rule can instantly lock you out of your server.

sudo apt install ufw
sudo ufw enable

Default Policies (Deny In, Allow Out)

The most robust firewall strategy relies on a “deny incoming, allow outgoing” default policy. This means UFW will automatically block all inbound traffic unless explicitly allowed by a rule, while permitting all outbound traffic. This approach ensures your server won’t expose unintended services, and your applications can communicate outward without restrictions.

sudo ufw default deny incoming
sudo ufw default allow outgoing

Allowing SSH (The Essential Rule Before Enabling)

This is the CRITICAL step. Before enabling UFW with a deny incoming default policy, you absolutely must allow SSH access. If you skip this, enabling the firewall will sever your remote connection. If your SSH port isn’t the default 22 (a common security best practice), you’ll need to specify the custom port.

sudo ufw allow ssh
# Or, if SSH is on port 2222:
sudo ufw allow 2222/tcp

Allowing Specific Ports (HTTP, HTTPS, Custom)

Once SSH access is secured, you can start allowing traffic for the services your server needs to host. Common examples include web servers (HTTP and HTTPS) or custom applications listening on specific ports.

For HTTP (port 80/tcp):

sudo ufw allow 80/tcp

For HTTPS (port 443/tcp):

sudo ufw allow 443/tcp

For a custom port, such as an application listening on 8080/tcp:

sudo ufw allow 8080/tcp

Blocking a Specific IP

If you identify a malicious IP address or one generating unwanted traffic, you can block it entirely. This is a useful temporary measure to mitigate targeted attacks.

sudo ufw deny from 192.168.1.100

Allowing a Specific IP on a Specific Port

For highly sensitive services, you might want to restrict access to a single IP address. For example, to allow a MySQL database (port 3306) to be accessed only by an application server at 10.0.0.5:

sudo ufw allow from 10.0.0.5 to any port 3306

Rate Limiting with UFW

UFW offers a built-in rate-limiting feature to help mitigate brute-force attacks, particularly on services like SSH. The limit command restricts connections from a single IP if it attempts to connect too frequently within a short timeframe. For example, to rate-limit SSH:

sudo ufw limit ssh

This rule will block an IP if it tries to establish more than 6 connections within 30 seconds. It’s an excellent preventive measure.

Viewing Active Rules

It’s crucial to be able to view the current firewall status and configured rules. The ufw status verbose command gives you a comprehensive overview:

sudo ufw status verbose

This output displays active rules, default policies, and the firewall’s status (active/inactive). [Read also: Hardening SSH on Linux: 10 Essential Configurations]

Deleting a Rule

Network requirements change, and you may need to remove obsolete or unnecessary rules. You can delete a rule by specifying it in full or by referencing its numbered index. To delete a specific rule (for example, the one allowing HTTP):

sudo ufw delete allow 80/tcp

To delete a rule by its number, first list the rules with their index numbers:

sudo ufw status numbered

Then, delete the rule by specifying its number (for example, to delete rule number 3):

sudo ufw delete 3

UFW Logging — Enabling and Reading Logs

Monitoring firewall logs is critical for security. UFW logs show which connections were blocked or allowed, providing valuable insights into unauthorized access attempts or connectivity issues. You can enable UFW logging at various levels (low, medium, high, full):

sudo ufw logging on

UFW logs are written to /var/log/ufw.log (or /var/log/syslog, depending on your logging setup). You can read them using tail or grep:

tail -f /var/log/ufw.log
grep UFW /var/log/syslog

UFW Application Profiles

UFW can work with application profiles, making rule management easier for common services. Many applications ship with a UFW profile that defines the necessary ports. You can view available profiles with:

sudo ufw app list

To enable a profile, such as OpenSSH:

sudo ufw allow OpenSSH

This is functionally equivalent to sudo ufw allow ssh but uses the service name. It’s a more semantic way to manage your rules.

Common Errors and Troubleshooting

Even with a straightforward tool like UFW, mistakes happen. The most common one—as mentioned—is locking yourself out of SSH. If this happens and you have physical or virtual console access, you can disable UFW with sudo ufw disable and start over. Another frequent mistake is failing to specify the protocol (TCP/UDP) for a port, which can result in rules being less restrictive than intended. Always specify /tcp or /udp unless you want the rule to apply to both. If a rule isn’t working as expected, check the rule order (UFW processes them top-to-bottom) and ensure no broader rules are overriding your specific ones. Finally, always refer to the official UFW documentation for the most up-to-date details: https://ubuntu.com/server/docs/security-firewall

Key Takeaways

Properly configuring UFW is a foundational step in securing any Ubuntu server. We’ve covered how to install it, set a restrictive default policy, and add specific rules for essential services like SSH, HTTP, and HTTPS. We also explored managing access from specific IPs, implementing rate limiting to thwart brute-force attacks, and monitoring firewall activity through logs.

The key takeaways are clear: always adopt a default “deny incoming” policy, allow only strictly necessary traffic, and thoroughly test rules before deploying them to production. Proactive firewall management, combined with consistent log monitoring, will keep your servers secure and resilient against external threats. Mastering UFW is an essential skill for any sysadmin configuring Ubuntu firewalls in professional environments.

Share this article:

Written by

Rosario Giordano

Rosario Giordano is a system administrator and IT consultant specializing in cybersecurity and cloud, with over 20 years of experience managing enterprise Linux infrastructures. His areas of expertise include SSH hardening, Kubernetes platforms, PostgreSQL databases, VMware/ Proxmox virtualization, and compliance with NIS2 and ISO 27001 security frameworks