On this page
- When should I use iptables directly?
- How do I install the persistence service?
- What should a basic IPv4 rule file contain?
- Why does IPv6 need its own file?
- How do I apply new rules without losing SSH?
- How do I make the rules load at boot?
- How do I inspect and change rules later?
- What breaks iptables setups most often?
- Questions
When should I use iptables directly?
Use raw iptables when you need exact control over rule order, want the same rule files on several distributions, or run software whose documentation assumes iptables syntax. On every current release the iptables command is the nftables based variant, often called iptables-nft: you type the classic syntax and the kernel stores the result as nftables. Check which variant you have:
sudo iptables -VThe output should mention (nf_tables). If you prefer a simpler front end, UFW on Ubuntu and Debian or firewalld on Rocky and AlmaLinux will serve you better. Native nftables syntax is covered in the firewall rules tutorial. Never run two of these managers at once.
How do I install the persistence service?
On Ubuntu 24.04, 22.04 and Debian 12 or 13, install the tools and the boot loader for rules:
sudo apt update
sudo apt install iptables iptables-persistentThe installer asks whether to save the current IPv4 and IPv6 rules; answering yes is harmless. The package conflicts with UFW, so apt removes UFW if it was installed. Rules will live in /etc/iptables/rules.v4 and /etc/iptables/rules.v6, loaded by netfilter-persistent.service.
On Rocky Linux 9 and AlmaLinux 9, stop firewalld first, because both would manage the same tables, then install the service package:
sudo dnf install -y iptables-nft iptables-nft-services
sudo systemctl disable --now firewalldDo the firewalld step in the same session where you load your new rules in the next sections, so the server never sits without a ruleset for long. Rules here are read from /etc/sysconfig/iptables and /etc/sysconfig/ip6tables.
What should a basic IPv4 rule file contain?
A default drop policy for incoming traffic plus explicit exceptions. Save this as /etc/iptables/rules.v4 on Debian and Ubuntu, or /etc/sysconfig/iptables on Rocky and AlmaLinux:
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
-A INPUT -m conntrack --ctstate INVALID -j DROP
-A INPUT -p icmp -j ACCEPT
-A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -j ACCEPT
-A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT
-A INPUT -p udp --dport 443 -j ACCEPT
-A INPUT -s 203.0.113.7/32 -p tcp --dport 5432 -j ACCEPT
COMMITThe order matters: the loopback and established rules come first so replies to your own outgoing connections always get through. Replace port 22 if your SSH daemon listens elsewhere.
Why does IPv6 need its own file?
ip6tables is a separate table set, and a server with an IPv6 address but no IPv6 rules is wide open on that address. IPv6 also depends on ICMPv6 for neighbour discovery: block it and the address stops working entirely. Save this as /etc/iptables/rules.v6 or /etc/sysconfig/ip6tables:
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
-A INPUT -m conntrack --ctstate INVALID -j DROP
-A INPUT -p ipv6-icmp -j ACCEPT
-A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -j ACCEPT
-A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT
-A INPUT -p udp --dport 443 -j ACCEPT
COMMITCheck the syntax of both files without applying them:
sudo iptables-restore --test /etc/iptables/rules.v4
sudo ip6tables-restore --test /etc/iptables/rules.v6How do I apply new rules without losing SSH?
Save the current state, schedule its return, load the new files, then test from a second terminal. With no browser console available, this timer is your safety net:
sudo sh -c "iptables-save > /root/fw-old.v4; ip6tables-save > /root/fw-old.v6"
sudo systemd-run --on-active=5min --unit=ipt-undo /bin/sh -c "iptables-restore < /root/fw-old.v4; ip6tables-restore < /root/fw-old.v6"
sudo iptables-restore /etc/iptables/rules.v4
sudo ip6tables-restore /etc/iptables/rules.v6Now open a new SSH connection from your computer. If it works, cancel the revert:
sudo systemctl stop ipt-undo.timerIf the new login fails, close nothing and wait: after five minutes the old rules return. On Debian and Ubuntu the iptables-apply helper offers a similar interactive check for IPv4 (sudo iptables-apply -t 60 /etc/iptables/rules.v4): it reverts unless you confirm in time.
How do I make the rules load at boot?
Enable the restore service and check that it points at your files. Debian and Ubuntu:
sudo systemctl enable netfilter-persistent
sudo netfilter-persistent save
sudo netfilter-persistent reloadThe save action writes whatever is active in the kernel back into the two files, so run it only after a successful test. Rocky Linux and AlmaLinux:
sudo systemctl enable --now iptables ip6tables
sudo sh -c "iptables-save > /etc/sysconfig/iptables; ip6tables-save > /etc/sysconfig/ip6tables"Reboot once during a quiet moment with sudo systemctl reboot, log in again and confirm the rules came back with sudo iptables -S and sudo ip6tables -S. Our reboot guide lists other checks worth doing at the same time.
How do I inspect and change rules later?
Read the live rules with counters and line numbers:
sudo iptables -L INPUT -n -v --line-numbers
sudo ip6tables -L INPUT -n -v --line-numbers
sudo nft list ruleset | lessPacket counters tell you whether a rule is ever hit. For lasting changes, edit the rule files and reload them with the timed revert from above, rather than typing iptables -A commands that vanish at reboot. To insert a temporary rule at a specific position, use sudo iptables -I INPUT 5 -p tcp --dport 8443 -j ACCEPT, then delete it by number with sudo iptables -D INPUT 5.
What breaks iptables setups most often?
Three problems cover most trouble. First, mixing back ends: rules loaded with iptables-legacy sit in a different place from iptables-nft rules, and the two lists confuse everyone. Check with sudo iptables-legacy -S 2>/dev/null; it should print nothing beyond default policies. Second, Docker: it creates its own chains, and a plain iptables-restore of your file flushes them, so restart Docker after reloading rules (sudo systemctl restart docker), or keep your custom rules in the DOCKER-USER chain. Third, forgetting IPv6, which leaves services reachable on the 2001:db8:4f2a::24 style address while IPv4 looks locked down. Test both from outside with nc -vz -4 and nc -vz -6.
Questions about iptables rules that survive a reboot
Is iptables deprecated?
The legacy back end is being phased out, but the iptables command itself remains available as iptables-nft on Ubuntu, Debian, Rocky Linux and AlmaLinux. Rules you write today keep working; they are stored as nftables underneath.
Can I keep UFW and add a few iptables rules?
Add them through UFW instead, for example in /etc/ufw/before.rules. Installing iptables-persistent removes UFW on Debian and Ubuntu, and two managers loading rules at boot leads to surprises.
What does the RELATED,ESTABLISHED rule do?
It accepts packets that belong to connections the server already knows, such as replies to your apt downloads or DNS lookups. Without it, a default drop policy would also block those replies.
How do I allow ping on IPv6 only?
The sample file already accepts all ICMPv6, which includes echo requests. Neighbour discovery needs ICMPv6 anyway, so keeping the broad rule is the safe choice.
Why did my rules vanish after a reboot?
Either the restore service is disabled or the rule files were never saved. Run systemctl is-enabled netfilter-persistent or systemctl is-enabled iptables, then check the file contents.
More in Security
- UFW firewall on Ubuntu and Debian
- firewalld on Rocky Linux and AlmaLinux
- Block brute force logins with fail2ban
- CrowdSec intrusion prevention
- Two-factor SSH logins with TOTP
- Disable or restrict root login
- Alerts for every SSH login
- Advanced OpenSSH settings
- Publish SSH host keys in DNS (SSHFP)
- Audit open ports with nmap
- File integrity checks with AIDE and Tripwire
- CSF firewall (ConfigServer Security)
- First security steps on Rocky and Alma
- Free TLS certificates with Certbot
- Self-signed certificates and a private CA
- Rate and connection limits in Nginx
- Wazuh security monitoring
- Kernel and sysctl hardening