ufw & firewalld
Writing nftables by hand is the most precise option; most servers use a front end instead: ufw on Ubuntu and Debian, firewalld on RHEL, Fedora, Rocky and SUSE. Both generate netfilter rules for you, add sensible defaults (loopback, established connections, ICMP) and give you short commands for the common cases. All transcripts here are sample sessions; firewall changes need root.
ufw: the Uncomplicated Firewall
$ sudo ufw status
Status: inactive
$ sudo ufw default deny incoming
Default incoming policy changed to 'deny'
(be sure to update your rules accordingly)
$ sudo ufw default allow outgoing
Default outgoing policy changed to 'allow'
(be sure to update your rules accordingly)
$ sudo ufw limit from 198.51.100.0/24 to any port 22 proto tcp comment 'admin ssh'
Rules updated
$ sudo ufw allow 'Nginx Full'
Rules updated
Rules updated (v6)
$ sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
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 LIMIT IN 198.51.100.0/24 # admin ssh
80,443/tcp (Nginx Full) ALLOW IN Anywhere
80,443/tcp (Nginx Full (v6)) ALLOW IN Anywhere (v6)| Command | Does |
|---|---|
ufw allow 443/tcp, ufw allow from 10.0.0.0/8 to any port 5432 proto tcp |
open a port, optionally only for some sources |
ufw limit 22/tcp |
allow, but deny an address that opens 6 or more connections within 30 seconds |
ufw allow 'Nginx Full', ufw app list |
application profiles from /etc/ufw/applications.d/ |
ufw status numbered, ufw delete 3 |
list rules with numbers, delete one |
ufw --dry-run allow 8080 |
print the rules it would add, change nothing |
ufw reload, ufw disable, ufw reset |
apply edited files, switch off, start over |
limit is ufw's built-in brake on password guessing: it uses the kernel's recent match to count new connections per source address. A model of the rule (6 or more new connections within 30 seconds from one address are denied):
def ufw_limit(attempts, hits=6, window=30):
seen = {} # source -> times of recent NEW connections
for t, src in attempts:
recent = [x for x in seen.get(src, []) if t - x < window] + [t]
seen[src] = recent
verdict = "DENY " if len(recent) >= hits else "ALLOW"
print(f"t={t:>3}s {src:<13} {verdict} ({len(recent)} in last {window}s)")
bot = [(i * 2, "203.0.113.7") for i in range(7)] # a connection every 2 s
admin = [(3, "198.51.100.4"), (40, "198.51.100.4")]
ufw_limit(sorted(bot + admin))t= 0s 203.0.113.7 ALLOW (1 in last 30s)
t= 2s 203.0.113.7 ALLOW (2 in last 30s)
t= 3s 198.51.100.4 ALLOW (1 in last 30s)
t= 4s 203.0.113.7 ALLOW (3 in last 30s)
t= 6s 203.0.113.7 ALLOW (4 in last 30s)
t= 8s 203.0.113.7 ALLOW (5 in last 30s)
t= 10s 203.0.113.7 DENY (6 in last 30s)
t= 12s 203.0.113.7 DENY (7 in last 30s)
t= 40s 198.51.100.4 ALLOW (1 in last 30s)
The bot is cut off at its sixth connection; the admin, connecting twice in 40 seconds, never notices. It is crude (a patient attacker stays under the limit), which is why fail2ban reads the actual failed logins instead.
Allow SSH before ufw enable. The warning about disrupting SSH is literal: enable with a default-deny policy and no SSH rule, and the next connection fails.
ufw keeps its rules in /etc/ufw/user.rules (generated; don't edit) and lets you add raw rules in /etc/ufw/before.rules. On Ubuntu 24.04 it drives the kernel through iptables-nft, so nft list ruleset shows ufw's chains (ufw-user-input and friends).
firewalld: zones and services
firewalld groups rules into zones (trust levels) and assigns interfaces or source networks to them. It has a runtime configuration (active now) and a permanent one (on disk, loaded at boot); that split is where most firewalld confusion comes from.
$ sudo firewall-cmd --state
running
$ sudo firewall-cmd --get-active-zones
public
interfaces: eth0
$ sudo firewall-cmd --zone=public --list-all
public (active)
target: default
interfaces: eth0
services: cockpit dhcpv6-client ssh
ports:
rich rules:
$ sudo firewall-cmd --permanent --zone=public --add-service=https
success
$ sudo firewall-cmd --permanent --zone=public --remove-service=cockpit
success
$ sudo firewall-cmd --permanent --zone=internal --add-source=10.0.0.0/8
success
$ sudo firewall-cmd --permanent --zone=internal --add-port=5432/tcp
success
$ sudo firewall-cmd --reload # load permanent → runtime
success
$ sudo firewall-cmd --zone=public --list-services
dhcpv6-client https ssh| Concept | Meaning |
|---|---|
| zone | a named rule set: drop, block, public, internal, trusted, … |
| interface or source binding | traffic from eth0, or from 10.0.0.0/8, is handled by that zone |
| service | a named port list from /usr/lib/firewalld/services/*.xml (ssh, https, postgresql) |
--permanent |
write to disk only; needs --reload (or a second command without it) to take effect now |
--timeout=5m |
a runtime-only rule that removes itself: a safe way to test |
--runtime-to-permanent |
save what you tested at runtime |
| rich rules | rule family=ipv4 source address=192.0.2.0/24 service name=ssh accept for finer control |
The two-step habit: change at runtime (optionally with --timeout), test from another session, then --runtime-to-permanent.
Whatever the front end: the same checklist
| Check | Why |
|---|---|
| Is the SSH rule in place before default-deny is enabled? | lockouts |
| Does IPv6 have the same rules? | a server with a public IPv6 address and an IPv4-only firewall is wide open on v6 (both front ends handle v6 by default; hand-written rules often don't) |
| Are container ports filtered where they are forwarded? | Docker inserts its own rules ahead of ufw's; a published -p 5432:5432 is reachable even if ufw denies 5432. Publish to 127.0.0.1:5432:5432, or filter in DOCKER-USER |
| Is there a firewall outside the host too? | cloud security groups and network ACLs (AWS) are a second layer; a mistake in one is caught by the other |
| Do you verify from outside? | ss -tln on the server shows what listens (Listening Sockets); nc -zv host port or nmap from another machine shows what is reachable |
# Sample session: checking from another host which ports are actually reachable
$ nmap -Pn -p 22,80,443,5432,6379 web1.example.com
PORT STATE SERVICE
22/tcp filtered ssh
80/tcp open http
443/tcp open https
5432/tcp filtered postgresql
6379/tcp filtered redisfiltered means no answer at all (DROP); closed would mean the host answered with a reset (nothing listening, or REJECT). From an address outside ADMIN_NETS, SSH showing as filtered is exactly what you want.
Test yourself
You run firewall-cmd --permanent --add-service=https and the port is still closed. Why?
- https is not a known service
- --permanent only writes the on-disk configuration; run
firewall-cmd --reloadto apply it - firewalld needs a reboot
- The zone is wrong by default
ufw denies port 5432, but a Docker container published with -p 5432:5432 is reachable from the internet. Why?
- ufw is disabled
- Docker's NAT and FORWARD rules handle the traffic before ufw's INPUT rules see it
- PostgreSQL bypasses firewalls
- IPv6 is used
What does ufw limit 22/tcp do?
- Limits bandwidth
- Allows SSH but temporarily denies addresses opening too many connections in a short time
- Allows only one user
- Blocks port 22