Skip to content

UFW firewall deep dive — beyond allow and deny

How UFW actually expresses rules in iptables/nftables, how to write rate-limited and interface-scoped policies, and how to keep an audit trail of every change.

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

  1. 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.
  2. 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’s limit verb applies the recent match: 6 hits in 30 seconds triggers a drop. For finer control, write the rule directly in /etc/ufw/before.rules using -m hashlimit.
  3. Pin rules to an interface when the host has more than one. sudo ufw allow in on eth1 to any port 5432 proto tcp. Without in on eth1, a Postgres rule on a dual-homed host accepts connections on the public NIC too — a classic Friday-evening incident.
  4. Register an application profile for every service the host offers. Create /etc/ufw/applications.d/myapp with [MyApp]\ntitle=My App\ndescription=...\nports=8080,8443/tcp. Reload with sudo ufw app update MyApp. Subsequent rules read ufw allow MyApp and survive a port renumbering.
  5. Mirror every rule for IPv6 if IPV6=yes. UFW does this automatically for symbolic rules, but custom before.rules additions need to be duplicated in before6.rules. Test with ip -6 route and nc -6 -zv <host> 22.
  6. Turn logging up to medium during a change window, back down to low for steady state. sudo ufw logging medium writes blocked packets to /var/log/ufw.log with kern.warn. Forward those lines to your log aggregator so a denied port becomes a query, not an archaeology dig.
  7. Snapshot the ruleset after every change and commit it to your config repo. sudo cp -a /etc/ufw /etc/ufw.$(date +%F) then git -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 1 while a deny-all sits at position 2 only matters for the input chain; routed packets follow a separate chain and ignore that ordering.
  • ufw disable flushes the policy but leaves the daemon-managed iptables-restore files in place — the next boot can re-enable an unintended rule if /etc/ufw/ufw.conf says ENABLED=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 ::1 works, ufw allow from ::/0 is rejected as malformed.
  • Forwarded traffic uses the ufw-user-forward chain. ufw route allow ... is required for Docker/Kubernetes hosts; plain ufw 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.