UFW: Complete guide to configuring the simple firewall on Linux

Introduction

In the world of Linux server administration, security is an undisputed priority. One of the most accessible tools for managing network traffic is UFW, short for Uncomplicated Firewall. Its design focuses on providing a simple and friendly interface over the powerful iptables, allowing novice and experienced administrators to define filtering rules without needing to memorize complex syntax. In this article we will explore step‑by‑step how to install, configure, and optimize UFW on any Debian‑ or Ubuntu‑based distribution, as well as on other systems that support it via compatible packages. By the end, you will be able to protect your system with an effective and easy‑to‑maintain firewall.

What is UFW?

UFW is an abstraction layer built on top of iptables, the packet‑filtering framework present in the Linux kernel. While iptables requires knowledge of chains, policies, and match specifications via an extensive syntax, UFW translates those operations into intuitive commands such as ufw allow or ufw deny. This reduces the learning curve and minimizes configuration errors that could leave the system exposed. Additionally, UFW includes predefined profiles for common services such as SSH, HTTP, HTTPS, and SMTP, which speeds up the deployment of web servers, databases, or email servers.

Installation

On most Debian‑based distributions, UFW is included in the official repositories. To install it, simply run:

  • sudo apt update
  • sudo apt install ufw

On RHEL‑ or Fedora‑based systems, the package can be found in EPEL or in the RPM Fusion repositories. The equivalent command would be:

  • sudo dnf install ufw

Once installed, the service is not activated by default. It is advisable to check its status with:

  • sudo ufw status verbose

If the output shows Status: inactive, the firewall is ready to be configured but is not yet filtering traffic.

Basic Configuration

Before enabling UFW, it is essential to define a default policy that determines what happens to traffic that does not match any explicit rule. The most secure configuration consists of denying all incoming connections and allowing outgoing ones:

  • sudo ufw default deny incoming
  • sudo ufw default allow outgoing

Then enable the services you need. For example, to allow SSH access from any address:

  • sudo ufw allow ssh

If you want to restrict SSH to a specific IP or subnet, use:

  • sudo ufw allow from 203.0.113.0/24 to any port 22 proto tcp

For standard web services, open ports 80 and 443:

  • sudo ufw allow http
  • sudo ufw allow https

After adding all necessary rules, activate the firewall:

  • sudo ufw enable

UFW will display a warning message indicating that the command may interrupt existing SSH connections; confirm with y if you are sure your SSH rule is correctly defined.

Advanced Rules

UFW allows creating more specific rules by combining interfaces, protocols, and connection states. For example, to limit ICMP traffic (ping) only to the local network:

  • sudo ufw allow in on eth0 from 192.168.1.0/24 to any proto icmp

If you need to redirect ports or perform NAT, UFW does not handle it directly; in those cases you must resort to iptables or tools like firewalld. However, UFW does support rate limiting to protect against brute‑force attacks:

  • sudo ufw limit ssh/tcp

This rule allows a maximum of six connection attempts within thirty seconds before blocking the source address.

Another useful feature is the creation of application profiles. Profiles are stored in /etc/ufw/applications.d/ and define the ports and protocols associated with a service. To list the available profiles:

  • sudo ufw app list

And to get details of a profile, for example Apache:

  • sudo ufw app info Apache

This simplifies enabling complex services without having to remember each port.

Logging and Monitoring

To troubleshoot issues or audit access, UFW can log packets that match certain rules. Logging is sent to the traditional logging system (syslog/journald). To enable logging of all denied rules:

  • sudo ufw logging on
  • sudo ufw logging low

The available logging levels are: off, low, medium, high, and full. Each level increases verbosity, so in production environments low is typically used to avoid filling the disk with excessive information.

To view the log in real time, you can use:

  • sudo journalctl -k | grep UFW

Or check the file /var/log/ufw.log if your rsyslog configuration directs it there. Analyzing these logs helps detect port‑scan attempts, malicious traffic, or misconfigurations that block legitimate connections.

Disabling and Resetting UFW

If you need to temporarily stop the firewall for any reason, simply run:

  • sudo ufw disable

This clears the active rules but preserves the configuration in the files under /etc/ufw/. To return to a clean state, as if you had never modified anything, execute:

  • sudo ufw reset

The reset command deactivates the firewall, removes all user‑defined rules, and restores the default policies. It is useful before running security tests or after migrating to another filtering solution.

Best Practices

To maintain an effective and easy‑to‑manage firewall, follow these recommendations:

  • Document each rule added, indicating its purpose, date, and responsible person.
  • Periodically review the file /etc/ufw/user.rules to remove obsolete rules.
  • Use application profiles whenever they are available; they reduce the chance of typos in port numbers.
  • Keep the system updated; kernel updates can affect iptables behavior and, consequently, UFW.
  • Combine UFW with other security layers such as fail2ban to automatically block IP addresses showing malicious behavior.
  • On servers exposed to the Internet, consider using full traffic logging (full) only during audit periods and revert to low afterward.

Conclusion

UFW represents an ideal balance between power and simplicity for managing firewalls on Linux. By abstracting the complexity of iptables behind clear commands and predefined profiles, it enables administrators of all levels to implement robust security policies without spending hours learning low‑level syntax. Whether you are protecting a web server, a database, or a workstation, following the steps described in this article will give you a solid foundation to keep your Linux environment secure and under control. Remember that security is an ongoing process: review, adjust, and monitor your rules regularly to adapt to evolving threats.