On this pageThe short answer
UFW, firewalld and nftables are not three competing firewalls. On a current Linux server all of them end up programming the same packet filter in the kernel, netfilter, through its nftables engine. What differs is the interface you maintain: a short list of allow rules, a zone model with a built-in undo, or a rule file you write yourself. Pick the one that matches how you work, use only that one, and the kernel does the rest.
The short answer
| UFW | firewalld | nftables | |
|---|---|---|---|
| Ships with | Ubuntu (installed, inactive) | RHEL family, Fedora | Every current distribution; Debian's documented default |
| Model | Ordered allow and deny rules | Zones, services, runtime vs permanent | Tables, chains, sets and maps |
| Configuration | ufw commands, files in /etc/ufw/ |
firewall-cmd, XML in /etc/firewalld/ |
/etc/nftables.conf |
| Best for | One server, a few ports | Several interfaces, cautious changes | Rules in Git, NAT, rate limits, large lists |
| Learning curve | Minutes | An afternoon | A weekend |
How they fit together
nftables replaced iptables as the kernel's filtering framework years ago. UFW still calls iptables commands, but on current Ubuntu those commands are a compatibility layer that writes nftables rules (sudo iptables -V prints nf_tables). firewalld talks to nftables directly. You can always inspect the real result with sudo nft list ruleset, whichever front end you chose.
The one hard rule: do not run two managers at once. UFW and firewalld will both rewrite rules and neither knows about the other, which produces exactly the kind of firewall nobody can reason about at 3 a.m.
Before you change a firewall over SSH
A default-deny policy applied before the SSH rule is the classic lockout. Three habits prevent it:
- Keep your current session open and test changes from a second terminal.
- Add the SSH rule before you switch the default policy to drop.
- Schedule an automatic rollback before risky changes, then cancel it once you have tested.
sudo systemd-run --on-active=5min /usr/sbin/ufw disable
sudo systemctl list-timers 'run-*' # note the unit name it printed
sudo systemctl stop run-r1a2b3c.timer # cancel once the new rules work
For firewalld, schedule /usr/bin/firewall-cmd --reload instead, which throws away runtime changes. For nftables, schedule /usr/sbin/nft flush ruleset, which leaves the server open until you fix the file.
UFW: the shortest path to a sane policy
UFW is a rule list with sensible defaults. A web server needs six commands:
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw limit OpenSSH
sudo ufw allow 80,443/tcp
sudo ufw enable
sudo ufw status verbose
limit allows SSH but refuses an address that opens six or more connections within 30 seconds, a cheap brake on password guessing. To accept SSH only from your office, use a source rule instead: sudo ufw allow from 198.51.100.24 to any port 22 proto tcp. IPv6 is covered as long as IPV6=yes is set in /etc/default/ufw, which is the default.
UFW's limits show when you need more than allow and deny. NAT, port forwarding and custom chains mean editing /etc/ufw/before.rules by hand, at which point you are writing iptables syntax inside a tool meant to hide it.
firewalld: zones and a built-in undo
firewalld groups rules into zones and assigns each network interface to one zone. Most single-NIC servers live entirely in public. Its best feature for remote work is the split between runtime and permanent configuration: a change without --permanent takes effect immediately but disappears on the next reload or reboot.
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --zone=public --list-all
sudo firewall-cmd --zone=public --add-service=https # runtime only, test it
sudo firewall-cmd --runtime-to-permanent # keep what works
If a runtime change cuts you off, a reboot from the provider console restores the last permanent state. You can also give a runtime rule an expiry with --timeout=10m.
To restrict SSH to one address, add a rich rule and then remove the open service:
sudo firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="198.51.100.24/32" service name="ssh" accept'
sudo firewall-cmd --permanent --zone=public --remove-service=ssh
sudo firewall-cmd --reload
On RHEL-family systems the default public zone usually allows cockpit as well. Remove it with --remove-service=cockpit if you do not use the web console.
nftables: one file, full control
Writing nftables yourself means one readable file that you can review, diff and keep in Git. The inet family covers IPv4 and IPv6 in the same table:
#!/usr/sbin/nft -f
flush ruleset
table inet filter {
set admin_v4 {
type ipv4_addr
elements = { 198.51.100.24 }
}
chain input {
type filter hook input priority filter; policy drop;
ct state established,related accept
ct state invalid drop
iif "lo" accept
meta l4proto { icmp, ipv6-icmp } accept
ip saddr @admin_v4 tcp dport 22 accept
tcp dport { 80, 443 } accept
}
chain forward {
type filter hook forward priority filter; policy drop;
}
}
Accepting ICMPv6 is not optional: IPv6 neighbor discovery runs over it, and blocking it breaks IPv6 connectivity in confusing ways. Check the syntax before loading, then enable the service so the rules survive a reboot:
sudo nft -c -f /etc/nftables.conf
sudo nft -f /etc/nftables.conf
sudo systemctl enable --now nftables
flush ruleset wipes every table, including ones created by Docker, libvirt or fail2ban. On a server that runs those, replace that line with table inet filter followed by delete table inet filter, which resets only your own table on each load.
Docker and other programs that write rules
Docker programs its own rules for published container ports, and that traffic is handled before UFW's input rules ever see it. A ufw deny 8080 therefore does not protect a container published on 8080. Publish internal services on loopback (-p 127.0.0.1:8080:80), filter in Docker's DOCKER-USER chain, or rely on a firewall outside the server. On CloudSat, the cloud firewall filters inbound traffic before it reaches the VM, so it keeps working even when a rule inside the server is wrong.
fail2ban and CrowdSec also add rules, but they integrate with all three tools and simply insert bans.
The same tasks in three syntaxes
Most day-to-day work comes down to a handful of operations. Here is how each tool expresses them, so you can judge which style you would rather read in six months:
| Task | UFW | firewalld | nftables rule in the input chain |
|---|---|---|---|
| Allow HTTPS | ufw allow 443/tcp |
firewall-cmd --permanent --add-service=https |
tcp dport 443 accept |
| SSH from one address | ufw allow from 198.51.100.24 to any port 22 proto tcp |
The rich rule shown above | ip saddr 198.51.100.24 tcp dport 22 accept |
| Block one address | ufw insert 1 deny from 203.0.113.50 |
firewall-cmd --permanent --zone=drop --add-source=203.0.113.50 |
ip saddr 203.0.113.50 drop |
| Open a port range | ufw allow 40000:40100/tcp |
firewall-cmd --permanent --add-port=40000-40100/tcp |
tcp dport 40000-40100 accept |
| Show the rules | ufw status numbered |
firewall-cmd --list-all |
nft list ruleset |
Order matters in UFW and nftables: the first matching rule wins, which is why the UFW block rule is inserted at position 1. firewalld decides by zone instead, and a source assigned to the drop zone is dropped before the public zone's services are considered.
Which one should you use?
- Ubuntu or Debian, one server, a handful of ports: UFW. You will be done in five minutes and anyone can read the rules.
- RHEL, AlmaLinux, Rocky Linux or Fedora: firewalld, since it is already the default and the runtime model makes remote changes safer.
- A VPN gateway, NAT, port forwarding, long block lists, or rules managed in Git: nftables directly.
Whichever you choose, verify from outside after every change. A firewall that looks right on screen and one that behaves right on the network are not always the same thing.
FAQ
Is iptables still worth learning?
Only to read old scripts. The iptables command on current distributions usually translates into nftables rules, and new rule sets should target nftables or one of its front ends.
How do I see what the firewall drops?
Turn on logging: sudo ufw logging on for UFW (entries land in the kernel log, readable with journalctl -k), or sudo firewall-cmd --set-log-denied=all for firewalld. In nftables, add a rule such as log prefix "nft-drop: " drop at the end of the input chain.
Does a host firewall stop DDoS attacks?
No. A volumetric flood fills the network link before your rules run. Host firewalls control which services are reachable; flood filtering has to happen upstream, at the provider's network edge.
Run it on CloudSat. Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. See plans or deploy a server.




