Firewall: ufw, firewalld, iptables, nftables
Opening and closing ports on Ubuntu (ufw), RHEL/Fedora (firewalld) and with raw iptables/nftables.
ufw (Ubuntu, Debian)
Status with rule numbers
sudo ufw status numberedAllow SSH before enabling (or you lock yourself out)
sudo ufw allow OpenSSH && sudo ufw enableOpen 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-allOpen 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 --reloadClose 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-numbersNAT rules (port forwarding, Docker)advanced
sudo iptables -t nat -L -n -vAllow a portadvanced
sudo iptables -A INPUT -p tcp --dport <port> -j ACCEPT
Save rules so they survive reboot (Debian)advanced
sudo netfilter-persistent saveWhole nftables rulesetadvanced
sudo nft list rulesetModern distros run iptables on top of nftables; this shows what is really applied.
Is the port really open from outside?
nc -zv <host> <port>