keepcalculator

06 Numbers and Powers

Configure UFW Firewall on Debian: Step-by-Step

Card No. 06·87393 · Updated · Standard formula, shown below

To configure the UFW (Uncomplicated Firewall) on Debian, you install it with apt, set default policies to deny incoming and allow outgoing traffic, then enable specific rules for services you need, and finally enable the firewall with sudo ufw enable. This guide walks through the entire process, from installation to advanced rules, and explains common pitfalls so you can secure your Debian system confidently.

What Is UFW and Why Use It on Debian?

UFW is a front-end for the traditional iptables firewall, designed to make basic host-based firewall configuration simple. Debian ships with iptables, but its raw syntax is verbose and error-prone for everyday use. UFW provides a clean command-line interface, yet it still writes standard iptables rules under the hood. It is the default firewall tool on Ubuntu, and while Debian does not enable it by default, it is available in the official repositories and works identically.

Using UFW is a good choice for a Debian server or desktop because it is easy to learn, well-documented, and integrates with systemd. It also supports IPv6, logging, and application profiles, making it suitable for both beginners and experienced administrators who want a quick, auditable rule set.

Before You Start: Prerequisites and Safety

You need root or sudo access to install and configure UFW. If you are connecting to a remote machine via SSH, be extremely careful: a mistake can lock you out. Always ensure your SSH port is allowed before enabling the firewall. Test your SSH connection in a separate terminal and keep it open until you are certain the rules are correct.

Also, check that your Debian version is supported. UFW is available in Debian 10 (buster) and later, and it works with both iptables and nftables backends (Debian 11+ uses nftables by default). You can verify your Debian version with cat /etc/debian_version.

Step-by-Step: Install and Enable UFW

  1. Update your package list: sudo apt update
  2. Install UFW: sudo apt install ufw
  3. Set default policies: sudo ufw default deny incoming and sudo ufw default allow outgoing
  4. Allow SSH (port 22) before enabling: sudo ufw allow ssh or sudo ufw allow 22/tcp
  5. Enable the firewall: sudo ufw enable
  6. Check status: sudo ufw status verbose

The default deny incoming policy blocks all inbound connections that are not explicitly allowed. The allow outgoing policy lets your system make any outbound connection (e.g., updates, DNS). After enabling, you will see a warning that SSH is allowed, which is correct.

Common UFW Rules and Examples

Here are typical rules you might add after the initial setup:

  • Allow a specific port: sudo ufw allow 80/tcp (HTTP)
  • Allow a port range: sudo ufw allow 6000:6007/tcp
  • Allow from a specific IP: sudo ufw allow from 192.168.1.100
  • Allow from an IP on a specific port: sudo ufw allow from 192.168.1.100 to any port 22
  • Deny a specific IP: sudo ufw deny from 203.0.113.5
  • Delete a rule: sudo ufw delete allow 80/tcp
  • Disable the firewall: sudo ufw disable (use only if needed)

You can also use application profiles if they exist. For example, sudo ufw app list shows available profiles like ‘OpenSSH’. Then sudo ufw allow 'OpenSSH' adds the rule. This is convenient, but profiles may not exist for every service.

How to Read the Status and Logs

The command sudo ufw status numbered shows rules with numbers, which is useful for deleting specific rules. The verbose output shows the default policies and whether logging is on. To enable logging, use sudo ufw logging on. Logs are written to /var/log/ufw.log and can help you diagnose blocked connections. For example, if a service is not reachable, check the log to see if the packet was dropped.

A typical log line looks like: UFW BLOCK IN=eth0 OUT= SRC=203.0.113.5 DST=198.51.100.2 PROTO=TCP DPT=22. This tells you the source IP, destination, and port that was blocked.

Common Mistakes and How to Avoid Them

The most common mistake is enabling UFW before allowing SSH, which locks you out of a remote server. Always add the SSH rule first. Another mistake is forgetting that UFW applies to IPv6 as well; if you have IPv6 enabled, you must allow the same ports for IPv6 (usually the same rule covers both, but be aware). Also, when you change a rule, you must reload the firewall with sudo ufw reload for the change to take effect (though adding/deleting rules automatically reloads).

Another pitfall is using the wrong protocol. For example, allowing 80/tcp does not allow UDP on the same port. If you need both, add separate rules or use sudo ufw allow 80 which allows both TCP and UDP. Finally, do not forget that UFW is a host firewall, not a network firewall; it does not replace a dedicated hardware firewall for a whole network.

UFW vs. nftables vs. iptables

UFW is a front-end, so it is not a direct competitor to nftables or iptables; it uses them as backends. On Debian 11 and later, UFW uses nftables by default, which is the modern replacement for iptables. If you need advanced features like stateful inspection, NAT, or complex chains, you might use nftables directly. But for most host-based filtering, UFW is simpler and less error-prone. If you already have iptables rules, UFW will respect them, but you should avoid mixing too many manual rules with UFW to prevent confusion.

Frequently Asked Questions

How do I check if UFW is active?

Run sudo ufw status. If it shows ‘Status: active’, the firewall is enabled. If it says ‘inactive’, it is not running. You can also check with sudo systemctl status ufw to see if the service is enabled at boot.

What is the default policy for incoming connections?

By default, if you run sudo ufw default deny incoming, all incoming connections are blocked unless you explicitly allow them. This is the recommended secure default. Outgoing is allowed by default.

Can I use UFW with Docker or other containers?

Yes, but you need to be careful. Docker manipulates iptables directly, and UFW may not see those rules correctly. A common workaround is to add rules to allow Docker’s bridge interface, or to disable Docker’s iptables management and manage rules manually. This is an advanced topic; for basic use, UFW and Docker can coexist, but test thoroughly.

How do I allow a port only for a specific IP?

Use the syntax: sudo ufw allow from 192.168.1.100 to any port 22 proto tcp. This allows only that IP to access port 22. You can also use a subnet like 192.168.1.0/24.

What does ‘sudo ufw enable’ do exactly?

It activates the firewall and configures it to start automatically at boot. It also enables the systemd service. You only need to run it once; after that, changes are applied immediately.

Is UFW safe to use on a desktop Debian?

Yes. On a desktop, you might want to allow local network connections (e.g., file sharing) and perhaps ping. You can add rules like sudo ufw allow from 192.168.1.0/24 to allow all traffic from your local network. Just be aware that some desktop services (like VNC) may need explicit ports allowed.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →