usar-firewall-ufw-en-raspberry-pi

How to Use the UFW Firewall on Raspberry Pi

  • 4 min

UFW (Uncomplicated Firewall) is a tool designed to simplify firewall management on Unix and Linux systems. Its main goal is to make firewall administration accessible to users without networking or firewall experience.

UFW is a simplified user interface for iptables, which is the underlying firewall tool in most Linux distributions.

Unlike iptables, which can be complicated to configure due to its extensive syntax, UFW provides a simpler command-line interface.

Features of UFW,

  • Simple interface: Designed to be easy to use, providing simple commands for firewall configuration.
  • IP-based rules: Allows configuring rules to allow or block traffic based on IP, ports, and protocols.
  • Event logging: Can log events to monitor firewall activity.

Installing UFW on Raspberry Pi

UFW is available in the default Raspbian repositories, so its installation is quite straightforward. Let’s see the steps to install and enable UFW on your Raspberry Pi.

First, make sure your system’s package list is up to date:

sudo apt update

Now install UFW using the following command:

sudo apt install ufw

Once installed, you can enable UFW with the following command:

sudo ufw enable

This command will activate the firewall with the default configuration, which is to block all incoming traffic and allow all outgoing traffic.

Basic UFW Configuration

After enabling UFW, the next step is to configure the firewall rules. UFW allows adding rules to allow or deny traffic based on different criteria.

Some of the most common commands are:

CommandDescription
sudo ufw enableEnables UFW and activates it on boot.
sudo ufw disableDisables UFW.
sudo ufw statusShows the current status of the firewall.
sudo ufw allow <port>Allows incoming traffic on a specific port.
sudo ufw deny <port>Denies incoming traffic on a specific port.
sudo ufw resetResets all UFW rules to their default values.

Let’s look at some common configuration examples.

Be careful not to play around recklessly with UFW, or you might leave the Raspberry Pi without a connection and have to physically connect a keyboard and mouse to access it.

Monitoring and Logging

UFW also allows enabling event logging, which can be useful for monitoring access attempts and suspicious activity.

To enable logging, use the following command:

sudo ufw logging on

You can review the logs in the /var/log/ufw.log file.