The ufw command in Linux: simplified firewall for Ubuntu

Introduction

In Linux system administration, security is a constant priority. One of the most critical aspects is protecting network traffic through a firewall. In Ubuntu, the most accessible tool for this purpose is UFW (Uncomplicated Firewall), which provides a simple interface over the complex iptables. This article will guide you from installation to advanced configuration of UFW, allowing you to protect your server or desktop effectively and without complications.

What is UFW?

UFW is an abstraction layer designed to simplify firewall rule management on Debian- and Ubuntu-based systems. Instead of writing long iptables strings, UFW lets you define policies with intuitive commands like allow, deny, and reject. Although its goal is simplicity, UFW retains the full power of iptables under the hood, making it an ideal choice for both beginners and experienced administrators seeking speed and clarity.

Basic Installation and Activation

In most Ubuntu installations, UFW comes preinstalled. If it is not present, you can install it with:

sudo apt update
sudo apt install ufw

Once installed, the firewall is disabled by default to avoid accidental lockouts during configuration. To enable it, run:

sudo ufw enable

To check its status, use:

sudo ufw status verbose

This command will show whether UFW is active, the default policies, and any exceptions you have defined.

Default Rules

Before creating exceptions, it is useful to define the default behavior. The most common policies are:

  • Deny all incoming traffic: sudo ufw default deny incoming
  • Allow all outgoing traffic: sudo ufw default allow outgoing

With these foundations, you will only explicitly allow the services you need, following the principle of least privilege.

Allowing and Denying Specific Services

UFW allows you to open or close ports easily. For example, to enable SSH access (port 22):

sudo ufw allow 22

If you prefer to specify the protocol:

sudo ufw allow 22/tcp

To deny HTTP traffic (port 80):

sudo ufw deny 80

You can also allow port ranges, useful for applications that use multiple ports:

sudo ufw allow 6000:6007/tcp

Application Profiles

Many Ubuntu packages include predefined profiles for UFW, further simplifying configuration. To list available profiles:

sudo ufw app list

To allow a full profile, for example Apache:

sudo ufw allow 'Apache Full'

Profiles usually include several rules (ports 80 and 443 for Apache), avoiding the need to specify each port manually.

Logging and Monitoring

UFW can log blocked connection attempts, which is valuable for detecting attacks or misconfigurations. To enable logging:

sudo ufw logging on

The logging level can be adjusted with low, medium, or high. Logs are written to /var/log/ufw.log and can be reviewed with tools like grep or journalctl.

Advanced Examples

Suppose you want to allow MySQL database access only from a specific IP:

sudo ufw allow from 203.0.113.10 to any port 3306

To limit SSH traffic to an internal address range:

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

If you need to delete a rule, first list the numbered rules:

sudo ufw status numbered

Then delete the rule number, for example, 3:

sudo ufw delete 3

Troubleshooting Common Issues

If after enabling UFW you lose your SSH connection, you likely blocked port 22 without noticing. In that case, access your cloud provider’s console or a local terminal and restore access:

sudo ufw allow 22/tcp
sudo ufw reload

Another frequent situation is that an application does not work because its port is not allowed. Verify active rules with sudo ufw status and add the necessary exception.

Conclusion

UFW represents an ideal balance between power and simplicity for firewall management on Ubuntu. Its clear syntax, availability of application profiles, and logging capability make it an indispensable tool for any administrator seeking to protect their systems without wasting time on complex iptables configurations. By following the steps and examples in this article, you can define a robust security policy tailored to your environment’s needs, keeping your server or desktop safe from unauthorized access.

This post is also available in ESPAÑOL.

Leave a Reply

Your email address will not be published. Required fields are marked *

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .