Firewall in Linux: ufw and nftables
Updated November 1, 2026
A firewall decides which network connections to allow and which to drop; ufw is a simple way to manage it.
Common cases
sudo ufw status # is it on, and what rules
sudo ufw enable # turn the firewall on
sudo ufw allow 22/tcp # allow ssh
sudo ufw allow 80,443/tcp # allow the web
sudo ufw deny 23 # block a port
sudo ufw delete allow 80 # remove a ruleSample output
$ sudo ufw enable
Firewall is active and enabled on system startup
$ sudo ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), deny (routed)
New profiles: skip
To Action From
-- ------ ----
22/tcp ALLOW IN Anywhere
80,443/tcp ALLOW IN Anywhere
...How it works: ufw on top of nftables
The kernel holds the actual rules in
nftables(formerlyiptables), but its syntax is verbose.ufw(uncomplicated firewall) is a simple wrapper: you say 'allow port 22' and it generates the engine's rules. The basic approach is to deny all incoming traffic and open only the ports you need (ssh, web). Before you turn the firewall on for a remote server, always allow22/tcp, or you lock yourself out of ssh. To see what is listening on ports, usess -tuln.
Common mistakes
ufw will not even read its own state without root. A harmless ufw status as a normal user answers ERROR: You need to be root to run this script and exits 1. That sets it apart from ss and ip, which show state to anyone: the ufw rules sit in /etc/ufw/ readable by root only.
Rules can be added while the firewall is off, and Rules updated does not mean the port is open. sudo ufw allow 80,443/tcp honestly answers Rules updated and Rules updated (v6), yet sudo ufw status right after prints Status: inactive and not one rule in the table. The rules are only saved so far; they start working after ufw enable.
Deleting matches the whole rule text, not the port number. After sudo ufw allow 80,443/tcp the command sudo ufw delete allow 80 prints Could not delete non-existent rule twice and still exits 0, so a script counts the deletion as done. For the same reason ufw allow 22 and ufw allow 22/tcp are two different rules: the first opens TCP and UDP both. Deleting by number is safer: sudo ufw status numbered, then sudo ufw delete 2.
In the book
This topic is covered in full in the chapter Host security.