How to Configure UFW Firewall Rules in Debian 12 Bookworm

Learn how to configure UFW firewall rules in Debian 12 Bookworm to secure your network.

Introduction

Uncomplicated Firewall (UFW) is a user-friendly front-end for managing firewall rules using iptables. It simplifies firewall management by providing an easy-to-use interface while maintaining powerful functionality. Debian 12 Bookworm, like previous Debian releases, does not enable UFW by default, but it can be installed and configured quickly.

This guide provides a step-by-step process to install, configure, and manage UFW on a Debian 12 system to secure network traffic effectively.

Prerequisites

Before proceeding, ensure you have the following:

  • A Debian 12 (Bookworm) system.
  • A user account with sudo privileges.
  • Access to a terminal or SSH session.

Step 1: Install UFW

Debian 12 does not come with UFW pre-installed, but you can install it from the official repository using:

sudo apt update
sudo apt install ufw -y

After installation, check the status of UFW:

sudo ufw status verbose

If UFW is inactive, you will see an output like:

Status: inactive

Step 2: Enable UFW

Before enabling UFW, ensure that SSH access is allowed if you are configuring it remotely. Otherwise, you may be locked out of your server.

To allow SSH access:

sudo ufw allow OpenSSH

Now, enable UFW:

sudo ufw enable

Confirm its status:

sudo ufw status verbose

Expected output:

Status: active

Step 3: Configuring UFW Rules

Allowing Specific Services

UFW comes with predefined application profiles stored in /etc/ufw/applications.d/. To list available applications:

sudo ufw app list

For example, to allow HTTP and HTTPS traffic:

sudo ufw allow 'Apache Full'

Alternatively, you can specify individual ports:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

Allowing and Denying Specific IP Addresses

To allow a specific IP to connect to all ports:

sudo ufw allow from 192.168.1.100

To allow an IP on a specific port:

sudo ufw allow from 192.168.1.100 to any port 22

To deny an IP address:

sudo ufw deny from 192.168.1.200

Allowing or Denying Port Ranges

To allow a range of ports, use the following syntax:

sudo ufw allow 1000:2000/tcp
sudo ufw allow 1000:2000/udp

Managing Incoming and Outgoing Traffic

By default, UFW blocks incoming connections and allows outgoing ones. To change this:

sudo ufw default deny incoming
sudo ufw default allow outgoing

To deny all outgoing traffic (use cautiously):

sudo ufw default deny outgoing

Step 4: Deleting or Resetting Rules

To remove a specific rule, first, list all rules with numbers:

sudo ufw status numbered

Then delete a rule by number:

sudo ufw delete <rule-number>

To reset UFW to its default state:

sudo ufw reset

Step 5: Logging and Monitoring UFW

Enable logging for better visibility into firewall activities:

sudo ufw logging on

To check firewall logs:

sudo journalctl -u ufw --no-pager | less

Step 6: Disabling UFW

If you need to disable UFW temporarily, use:

sudo ufw disable

To completely remove UFW:

sudo apt remove --purge ufw -y

Conclusion

UFW provides a straightforward way to manage firewall rules on Debian 12 Bookworm. By configuring UFW properly, you can enhance your system’s security while ensuring necessary services remain accessible. Regularly review firewall rules to keep your system protected from unauthorized access.

By following the steps in this guide, you now have a fully functional and secure UFW setup on Debian 12.