Firewall in Linux: ufw, firewalld and nftables

What a firewall does

A host firewall on a Linux machine decides, packet by packet, whether the machine accepts, drops or forwards them. The decision is made in the kernel by netfilter: hooks on the paths a packet takes (incoming, outgoing, forwarded) call the rules of the loaded tables.

Nobody writes those rules by hand on a modern system. Four front ends are in use:

A firewall filters traffic, it does not encrypt it: that is the work of a VPN tunnel, and the setup on Linux is the article VPN in Linux .

Every command on this page changes the packet filter of the kernel, so every one of them needs root. They were tried as an ordinary user on Alpine Linux 3.24 and stopped at the first step: nft answers Operation not permitted (you must be root), iptables iptables v1.8.13 (nf_tables): Could not fetch rule set generation id: Permission denied (you must be root), and ufw ERROR: You need to be root to run this script. No output from a root shell is quoted below: what it prints is described in words. The firewall-cmd and systemctl commands were not run at all: Alpine has no firewalld package and no systemd, while Debian, Ubuntu and the .rpm systems have both.

Which front end is running

Ask the service manager before you write a rule: two front ends on one machine fight over the same tables.

systemctl is-active ufw firewalld nftables

The low-level view is the same on every distribution: nftables prints its ruleset, and iptables prints its chains.

sudo nft list ruleset

sudo iptables -S

An empty ruleset with the policy ACCEPT means nothing is filtered: a fresh install usually looks like that, and a server on a public address should not stay that way.

ufw

ufw keeps a list of rules and translates it into the low-level ones. It is off until you turn it on, and its default policy is the useful one: deny incoming, allow outgoing.

sudo ufw status verbose

Allow SSH before enabling, or the enable closes the session you are working in. Then allow what the machine serves and nothing else.

sudo ufw allow 22/tcp

sudo ufw allow 443/tcp

A rule can name a service instead of a port, and it can be limited to a source address. The last form is the right one for a management port.

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

Turn it on, list the numbered rules and delete by number. Deleting by number is safer than by text: the text form silently adds a rule when the wording differs.

sudo ufw enable

sudo ufw status numbered

sudo ufw delete 3

The rules live in /etc/ufw/ and survive a reboot once enabled. Details for Ubuntu are on the page Firewall Ubuntu .

firewalld

firewalld groups rules into zones and applies a zone to an interface or a source network. The default zone is public, and every zone has services and ports.

sudo firewall-cmd --state

sudo firewall-cmd --list-all

A change without --permanent lives until the next reload; a change with it lives in the configuration but does nothing until the reload. Do both: add with --permanent, then reload.

sudo firewall-cmd --permanent --add-service=https

sudo firewall-cmd --permanent --add-port=8080/tcp

sudo firewall-cmd --reload

A service is a named group of ports and helper modules from /usr/lib/firewalld/services/. Opening a service is preferable to opening a bare port: the name says what the hole is for.

Ports for the services of this section: 1194/udp for OpenVPN, 51820/udp for WireGuard.

The CentOS page goes further: Firewall CentOS , and the same for Rocky .

nftables

nftables works with tables, chains and rules. A chain is attached to a hook (input, forward, output) and has a policy; the rules inside it are checked in order.

A minimal IPv4 and IPv6 filter looks like this. Create the table and the input chain with the policy drop, then allow the loopback, established connections and SSH.

sudo nft add table inet filter

sudo nft add chain inet filter input "{ type filter hook input priority 0; policy drop; }"

sudo nft add rule inet filter input iif lo accept

sudo nft add rule inet filter input ct state established,related accept

sudo nft add rule inet filter input tcp dport 22 accept

The inet family holds IPv4 and IPv6 in one table, which is why one ruleset is enough for both.

Rules added with nft add live in the kernel only until a reboot. To keep them, write the ruleset to the file the service reads: /etc/nftables.conf on Debian and Ubuntu, /etc/sysconfig/nftables.conf on the .rpm systems.

sudo sh -c "nft list ruleset > /etc/nftables.conf"

That is the Debian and Ubuntu path; on the .rpm systems the same command writes /etc/sysconfig/nftables.conf.

sudo systemctl enable --now nftables

nft add appends to the end of the chain, where a drop policy waits. For a rule that must come before a broader one, use nft insert.

iptables

iptables has the same idea with older names: tables (filter, nat), chains (INPUT, FORWARD, OUTPUT) and rules. The command is still worth knowing, because a lot of instructions on the internet are written in it.

sudo iptables -L -n -v

-L lists, -n keeps addresses and ports numeric instead of resolving names, -v shows the counters of packets and bytes that each rule matched.

sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

-A appends, so the rule lands after the existing ones; -I inserts at the top. Rules are not permanent:

sudo iptables-save > /etc/iptables/rules.v4

Whether that file is read at boot depends on the distribution and the package: iptables-persistent in Debian and Ubuntu, iptables-services in the .rpm systems.

Firewall per distribution

The distribution pages carry the screenshots and the longer examples:

Distributions
Firewall Ubuntu
Firewall Debian
Firewall CentOS
Firewall Rocky

The firewall of Windows is a different program with its own rules: see Firewall Windows .

Article author: Arthur Isaev