Firewall Cheatsheet

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.

Which one is in charge?

CheckCommand
Running front-endssystemctl is-active ufw firewalld nftables
What is really in the kernelnft list ruleset
Is iptables just a shim?iptables -V — nf_tables means it writes nftables

ufw (Ubuntu / Debian)

TaskCommand
Status and rulesufw status verbose
Numbered, for deletingufw status numbered
Sensible defaultsufw default deny incoming · ufw default allow outgoing
Allow a portufw allow 8080/tcp
Allow SSH, rate-limitedufw limit 22/tcp
Allow severalufw allow 80,443/tcp
Allow from one network onlyufw allow from 10.0.0.0/8 to any port 22 proto tcp
Named app profilesufw app list · ufw allow "Nginx Full"
Deleteufw delete 3 · ufw delete allow 8080/tcp
Enable / disable / reloadufw enable · ufw disable · ufw reload
Log blocked trafficufw logging on
Start overufw 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.

TaskCommand
Is it running?firewall-cmd --state
Which zone is this interface in?firewall-cmd --get-active-zones
Everything about the current zonefirewall-cmd --list-all
Allow a servicefirewall-cmd --add-service=ssh --permanent
Allow a portfirewall-cmd --add-port=8080/tcp --permanent
Removefirewall-cmd --remove-port=8080/tcp --permanent
Apply permanent changesfirewall-cmd --reload
Keep what is running right nowfirewall-cmd --runtime-to-permanent
Source-scoped rulefirewall-cmd --add-rich-rule='rule family="ipv4" source address="10.0.0.0/8" port port="22" protocol="tcp" accept' --permanent
Self-reverting test rulefirewall-cmd --add-port=8080/tcp --timeout=5m
Cut everything, nowfirewall-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 countersiptables -L -n -v
List as the commands that made itiptables -S
Save / restoreiptables-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

PortServiceNotes
22/tcpSSHAlso carries SFTP and SCP. Rate-limit it if it faces the internet.
23/tcpTelnetPlaintext. Still alive on old switches — see the Cisco cheatsheet.
3389/tcpRDPNever expose directly. Put it behind a VPN.
5900/tcpVNCFrequently unauthenticated by default.
161/udp, 162/udpSNMP, SNMP trapsv1/v2c community strings cross the wire in clear.

Web and mail

PortServiceNotes
80/tcpHTTPUsually only kept open to redirect to 443.
443/tcp and 443/udpHTTPS443/udp is HTTP/3 (QUIC) — easy to forget, and the rule people miss.
8080, 8443/tcpAlt HTTP / HTTPSAdmin panels and app servers love these. Rarely want to be public.
25/tcpSMTPServer-to-server relay. Most ISPs and clouds block outbound 25 by default.
587/tcpSMTP submissionSTARTTLS — what mail clients should be using.
465/tcpSMTPSImplicit TLS submission.
143 / 993/tcpIMAP / IMAPS993 is the TLS one.
110 / 995/tcpPOP3 / POP3S995 is the TLS one.

Core network services

PortServiceNotes
53/udp and 53/tcpDNSUDP 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/udpDHCP server, clientBroadcast — does not cross a router without a DHCP relay.
123/udpNTPTime drift breaks TLS and Kerberos before it breaks anything obvious.
389 / 636/tcpLDAP / LDAPS636 is the TLS one.
88/tcp+udpKerberosDomain authentication.
514/udpsyslogPlaintext, fire-and-forget. 6514/tcp for syslog over TLS.
179/tcpBGPBetween routers only.

Files, printing and databases

PortServiceNotes
21/tcpFTPControl channel only; data uses 20 or a high passive range — which is exactly why FTP is miserable through a firewall.
139, 445/tcpNetBIOS, SMBFile sharing. Never expose to the internet — this is how WannaCry travelled.
2049/tcpNFSKeep it on a trusted network.
631/tcpIPP / CUPSPrinting.
3306/tcpMySQL / MariaDBBind to localhost unless you genuinely mean otherwise.
5432/tcpPostgreSQLAs above.
1433/tcpMS SQL ServerAs above.
27017/tcpMongoDBHistorically shipped with no authentication at all.
6379/tcpRedisNo auth by default. Mass-compromised for years.
11211/tcp+udpmemcachedNo auth, and a favourite UDP amplification reflector.

VPN and VoIP

PortServiceNotes
500, 4500/udpIPsec IKE, NAT-T4500 is the one used when NAT sits in the path — and it usually does.
51820/udpWireGuardDefault, and freely changed.
1194/udpOpenVPNTCP/443 is the common fallback through hostile networks.
5060 / 5061/tcp+udpSIP / SIP-TLSCall setup only — it carries no audio.
10000–20000/udpRTP mediaThe 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

QuestionHow
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
What does it look like from outside?nmap -Pn host — from another machine
What is being blocked?journalctl -k | grep -i ufw (after ufw logging on)

Do not lock yourself out

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.