$ command-helper

Firewall: ufw, firewalld, iptables, nftables

Opening and closing ports on Ubuntu (ufw), RHEL/Fedora (firewalld) and with raw iptables/nftables.

Requires Google sign-in

ufw (Ubuntu, Debian)

Status with rule numbers
sudo ufw status numbered
Allow SSH before enabling (or you lock yourself out)
sudo ufw allow OpenSSH && sudo ufw enable
Open a port
sudo ufw allow <port>/tcp
Allow a port only from one IP
sudo ufw allow from <ip> to any port <port> proto tcp
Delete a rule by number
sudo ufw delete <rule_number>

firewalld (RHEL, Fedora, Amazon Linux)

Active zones and their rules
sudo firewall-cmd --list-all
Open a port permanently and apply
sudo firewall-cmd --permanent --add-port=<port>/tcp && sudo firewall-cmd --reload
Allow a known service (http, https, ssh…)
sudo firewall-cmd --permanent --add-service=https && sudo firewall-cmd --reload
Close a port
sudo firewall-cmd --permanent --remove-port=<port>/tcp && sudo firewall-cmd --reload

iptables / nftables

Rules with packet counters
sudo iptables -L -n -v --line-numbers
NAT rules (port forwarding, Docker)advanced
sudo iptables -t nat -L -n -v
Allow a portadvanced
sudo iptables -A INPUT -p tcp --dport <port> -j ACCEPT
Save rules so they survive reboot (Debian)advanced
sudo netfilter-persistent save
Whole nftables rulesetadvanced
sudo nft list ruleset

Modern distros run iptables on top of nftables; this shows what is really applied.

Is the port really open from outside?
nc -zv <host> <port>