UFW gets dismissed as the “easy” Linux firewall, but underneath the friendly verbs sits a fully expressive iptables/nftables policy with hooks for rate limiting, application profiles, before/after rule chains, and IPv6 parity. This article is for operators who already type ufw allow 22 in their sleep and now need to write policies that survive an audit, an upgrade, and a co-worker.
How to verify
Start by inspecting the current effective policy, not just the high-level summary:
sudo ufw status verbose
sudo ufw show raw
sudo iptables -S | head -40
sudo iptables -L ufw-user-input -n -v --line-numbers
sudo journalctl -u ufw --since "1 hour ago"
ufw show raw prints the actual iptables ruleset UFW generated. If a packet is doing something surprising, that is the only output that matters.
What’s happening
UFW is a Python frontend that translates declarative rules into iptables (or nftables on newer Ubuntu/Debian) rules. Every ufw command edits a file under /etc/ufw/ and then re-renders the chains. The four files that matter are /etc/ufw/before.rules, /etc/ufw/after.rules, /etc/ufw/user.rules, and the IPv6 counterparts. Rules added via the CLI land in user.rules. Rules that must run before user rules (NAT, conntrack, ICMP) belong in before.rules. UFW also reads /etc/default/ufw for global toggles like DEFAULT_INPUT_POLICY, IPV6=yes, and MANAGE_BUILTINS=no.
The ordering inside the user chain is significant: UFW evaluates top to bottom and stops on the first match. ufw insert 1 ... puts the rule at the top; ufw delete N removes by number. Application profiles in /etc/ufw/applications.d/ group ports under a label so operators can write ufw allow OpenSSH instead of memorising port lists for every service.
The procedure
- Set the defaults explicitly before adding rules.
sudo ufw default deny incoming && sudo ufw default allow outgoing && sudo ufw default deny routed. The third one matters as soon as the host forwards packets — a Docker host without it leaks egress through the bridge. - Restrict SSH to your bastion range with a rate limit.
sudo ufw limit proto tcp from 203.0.113.0/24 to any port 22. UFW’slimitverb applies therecentmatch: 6 hits in 30 seconds triggers a drop. For finer control, write the rule directly in/etc/ufw/before.rulesusing-m hashlimit. - Pin rules to an interface when the host has more than one.
sudo ufw allow in on eth1 to any port 5432 proto tcp. Withoutin on eth1, a Postgres rule on a dual-homed host accepts connections on the public NIC too — a classic Friday-evening incident. - Register an application profile for every service the host offers. Create
/etc/ufw/applications.d/myappwith[MyApp]\ntitle=My App\ndescription=...\nports=8080,8443/tcp. Reload withsudo ufw app update MyApp. Subsequent rules readufw allow MyAppand survive a port renumbering. - Mirror every rule for IPv6 if
IPV6=yes. UFW does this automatically for symbolic rules, but custombefore.rulesadditions need to be duplicated inbefore6.rules. Test withip -6 routeandnc -6 -zv <host> 22. - Turn logging up to
mediumduring a change window, back down tolowfor steady state.sudo ufw logging mediumwrites blocked packets to/var/log/ufw.logwithkern.warn. Forward those lines to your log aggregator so a denied port becomes a query, not an archaeology dig. - Snapshot the ruleset after every change and commit it to your config repo.
sudo cp -a /etc/ufw /etc/ufw.$(date +%F)thengit -C /etc/config-snapshots add -A && git commit -m 'ufw: open 5432 from db-bastion'. Without this, no one can answer “when did port X open?” three months later.
Common pitfalls
- Inserting a rule with
ufw insert 1while a deny-all sits at position 2 only matters for the input chain; routed packets follow a separate chain and ignore that ordering. ufw disableflushes the policy but leaves the daemon-managediptables-restorefiles in place — the next boot can re-enable an unintended rule if/etc/ufw/ufw.confsaysENABLED=yes.- Application profiles do not survive a UFW package reinstall on every distro; back up
/etc/ufw/applications.d/separately. - IPv6 rules must match IPv6 source/destination syntax —
ufw allow from ::1works,ufw allow from ::/0is rejected as malformed. - Forwarded traffic uses the
ufw-user-forwardchain.ufw route allow ...is required for Docker/Kubernetes hosts; plainufw allow ...does nothing for routed packets.
In the engagements we run, the firewall ruleset is treated as code: versioned, peer-reviewed, and replayable on a rebuild. That discipline is part of Managed Operations — every UFW change leaves a paper trail and a ticket, not just a config delta.