One kernel filter, three front-ends. Pairs with Firewalls on Linux — read that for the why. netfilter is the filter itself; ufw (Ubuntu), firewalld (Red Hat / Alma) and nft are ways of writing to it. Work out which is driving before you type. Everything here wants sudo.
ufw allow from 10.0.0.0/8 to any port 22 proto tcp
Named app profiles
ufw app list · ufw allow "Nginx Full"
Delete
ufw delete 3 · ufw delete allow 8080/tcp
Enable / disable / reload
ufw enable · ufw disable · ufw reload
Log blocked traffic
ufw logging on
Start over
ufw reset
firewalld (RHEL / Alma / Fedora)
firewalld thinks in zones, and splits runtime from permanent: changes without --permanent vanish on reload, and changes with it do not apply until you reload.
Task
Command
Is it running?
firewall-cmd --state
Which zone is this interface in?
firewall-cmd --get-active-zones
Everything about the current zone
firewall-cmd --list-all
Allow a service
firewall-cmd --add-service=ssh --permanent
Allow a port
firewall-cmd --add-port=8080/tcp --permanent
Remove
firewall-cmd --remove-port=8080/tcp --permanent
Apply permanent changes
firewall-cmd --reload
Keep what is running right now
firewall-cmd --runtime-to-permanent
Source-scoped rule
firewall-cmd --add-rich-rule='rule family="ipv4" source address="10.0.0.0/8" port port="22" protocol="tcp" accept' --permanent
Self-reverting test rule
firewall-cmd --add-port=8080/tcp --timeout=5m
Cut everything, now
firewall-cmd --panic-on / --panic-off
nftables, directly
# a minimal stateful input chain
nft add table inet filter
nft add chain inet filter input '{ type filter hook input priority 0; policy drop; }'
nft add rule inet filter input ct state established,related accept
nft add rule inet filter input iif lo accept
nft add rule inet filter input tcp dport 22 accept
nft list ruleset # read everything
nft list ruleset > /etc/nftables.conf # save it
systemctl enable --now nftables # load it at boot
Note the order: the ct state established,related accept rule comes first for a reason — it is the stateful fast path that lets replies back in, and most packets on a busy box never get compared against anything else.
Legacy iptables (for reading old docs)
Command
List with counters
iptables -L -n -v
List as the commands that made it
iptables -S
Save / restore
iptables-save · iptables-restore
Common ports
The numbers you end up looking up while writing a rule. Port space splits into three ranges: 0–1023 are the well-known ports (binding one needs root), 1024–49151 are registered, and 49152–65535 are ephemeral — the pool a client draws a source port from, which is the same pool NAT works through when it multiplexes a whole network behind one address. Firewall rules nearly always care about the destination port. To look one up on the box itself: grep -w 443 /etc/services.
Remote access and management
Port
Service
Notes
22/tcp
SSH
Also carries SFTP and SCP. Rate-limit it if it faces the internet.
23/tcp
Telnet
Plaintext. Still alive on old switches — see the Cisco cheatsheet.
3389/tcp
RDP
Never expose directly. Put it behind a VPN.
5900/tcp
VNC
Frequently unauthenticated by default.
161/udp, 162/udp
SNMP, SNMP traps
v1/v2c community strings cross the wire in clear.
Web and mail
Port
Service
Notes
80/tcp
HTTP
Usually only kept open to redirect to 443.
443/tcpand443/udp
HTTPS
443/udp is HTTP/3 (QUIC) — easy to forget, and the rule people miss.
8080, 8443/tcp
Alt HTTP / HTTPS
Admin panels and app servers love these. Rarely want to be public.
25/tcp
SMTP
Server-to-server relay. Most ISPs and clouds block outbound 25 by default.
587/tcp
SMTP submission
STARTTLS — what mail clients should be using.
465/tcp
SMTPS
Implicit TLS submission.
143 / 993/tcp
IMAP / IMAPS
993 is the TLS one.
110 / 995/tcp
POP3 / POP3S
995 is the TLS one.
Core network services
Port
Service
Notes
53/udpand53/tcp
DNS
UDP for ordinary queries, TCP for responses over 512 bytes and zone transfers. Allowing only UDP/53 breaks things in a way that looks intermittent.
67, 68/udp
DHCP server, client
Broadcast — does not cross a router without a DHCP relay.
123/udp
NTP
Time drift breaks TLS and Kerberos before it breaks anything obvious.
389 / 636/tcp
LDAP / LDAPS
636 is the TLS one.
88/tcp+udp
Kerberos
Domain authentication.
514/udp
syslog
Plaintext, fire-and-forget. 6514/tcp for syslog over TLS.
179/tcp
BGP
Between routers only.
Files, printing and databases
Port
Service
Notes
21/tcp
FTP
Control channel only; data uses 20 or a high passive range — which is exactly why FTP is miserable through a firewall.
139, 445/tcp
NetBIOS, SMB
File sharing. Never expose to the internet — this is how WannaCry travelled.
2049/tcp
NFS
Keep it on a trusted network.
631/tcp
IPP / CUPS
Printing.
3306/tcp
MySQL / MariaDB
Bind to localhost unless you genuinely mean otherwise.
5432/tcp
PostgreSQL
As above.
1433/tcp
MS SQL Server
As above.
27017/tcp
MongoDB
Historically shipped with no authentication at all.
6379/tcp
Redis
No auth by default. Mass-compromised for years.
11211/tcp+udp
memcached
No auth, and a favourite UDP amplification reflector.
VPN and VoIP
Port
Service
Notes
500, 4500/udp
IPsec IKE, NAT-T
4500 is the one used when NAT sits in the path — and it usually does.
51820/udp
WireGuard
Default, and freely changed.
1194/udp
OpenVPN
TCP/443 is the common fallback through hostile networks.
5060 / 5061/tcp+udp
SIP / SIP-TLS
Call setup only — it carries no audio.
10000–20000/udp
RTP media
The actual audio. Open SIP but not RTP and calls connect with silence — the classic one-way-audio fault.
The never-expose list
Databases and caches (3306, 5432, 1433, 27017, 6379, 11211), SMB (445), RDP (3389) and VNC (5900) should never be reachable from the internet. Several of them have historically shipped with no authentication enabled, and the rest are brute-forced continuously. Bind them to 127.0.0.1 or a private interface and reach them over SSH or a VPN. ss -tulpn tells you which of them are listening on 0.0.0.0 right now — that is the list worth checking before you finish.
Testing it honestly
Question
How
What does the front-end believe?
ufw status verbose · firewall-cmd --list-all
What is actually loaded?
nft list ruleset
What is even listening?
ss -tulpn — bound to 127.0.0.1 was never exposed anyway
Allow SSH before you set a default-deny policy and enable, never after. Then give yourself a way back: sudo systemd-run --on-active=10min ufw disable schedules the undo, and you cancel it once you are sure you are still connected. firewalld has this built in as --timeout=5m on a runtime rule. And never trust a firewall you have only tested from the machine it runs on — loopback traffic never crosses it.