On this pageWhich iptables do you have?
iptables is still in countless scripts, tutorials and production servers, but on current distributions the iptables command is usually a front end that writes nftables rules. The syntax is the same, the rules simply land in the newer framework. This guide covers the everyday work: listing rules in a readable way, deleting exactly the right one, flushing without cutting your own connection, making rules survive a reboot, and living next to UFW, firewalld and Docker.
Every change below takes effect immediately. Over SSH, keep your session open, set up the rollback described further down before you experiment, and test with a second session after each change.
Which iptables do you have?
sudo iptables -V
The answer ends in (nf_tables) or (legacy). With nf_tables, which is the default on Ubuntu, Debian and the RHEL family, your iptables rules are nftables rules underneath and also appear in sudo nft list ruleset. With legacy, they live in the old kernel tables. Mixing the two backends on one machine creates two separate rule sets that both apply, which makes debugging miserable, so stick to one.
Also check whether something else manages the firewall. If sudo ufw status says active, or systemctl is-active firewalld says active, make changes through those tools: rules you add by hand to their chains are overwritten at their next reload.
List the rules
sudo iptables -L -n -v --line-numbers
sudo iptables -S
sudo iptables -t nat -L -n -v
sudo ip6tables -S
-Lprints each chain as a table.-nskips reverse DNS lookups, which otherwise make listing slow;-vadds packet and byte counters plus interfaces;--line-numbersnumbers the rules for deletion.-Sprints the rules as the commands that would recreate them, which is the easiest format to copy, compare and delete from.-t natshows the NAT table. The default table isfilter;nat,mangleandraware separate.- IPv6 rules are a completely separate set, managed with
ip6tables. A server with careful IPv4 rules and an empty IPv6 table is common, and wide open.
The counters tell you which rules actually match traffic. A rule whose counters stay at zero for weeks is a candidate for removal. Reset them with sudo iptables -Z to start a fresh measurement.
Chain INPUT (policy DROP 1204 packets, 61328 bytes)
num pkts bytes target prot opt in out source destination
1 9821 12M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
2 31 1860 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
3 412 24720 ACCEPT tcp -- * * 198.51.100.24 0.0.0.0/0 tcp dpt:22
4 977 58620 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:443
5 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080
Rules are checked from the top, and the first rule that accepts or drops a packet ends the search. The policy at the top, here DROP, applies to whatever no rule matched.
Delete a rule
By number, from the --line-numbers view:
sudo iptables -D INPUT 5
Numbers shift after every deletion, so list again before deleting a second rule, or delete from the bottom up.
By specification, which is safer in scripts because it cannot hit the wrong line. Copy the rule from iptables -S and change -A to -D:
sudo iptables -S INPUT | grep 8080
sudo iptables -D INPUT -p tcp -m tcp --dport 8080 -j ACCEPT
-C checks whether a rule exists without changing anything, which keeps scripts from adding duplicates: sudo iptables -C INPUT -p tcp --dport 8080 -j ACCEPT && echo present. To add a rule in a specific position rather than at the end, use -I INPUT 3 instead of -A INPUT.
Flush without locking yourself out
iptables -F deletes the rules in a chain but leaves the chain's policy alone. If the INPUT policy is DROP, flushing removes the rule that accepted your SSH session, and the policy drops everything that follows. Reset the policies first:
sudo iptables -P INPUT ACCEPT
sudo iptables -P FORWARD ACCEPT
sudo iptables -P OUTPUT ACCEPT
sudo iptables -F
sudo iptables -X
sudo iptables -t nat -F
sudo iptables -t mangle -F
This leaves the server with no packet filter at all, so use it only as a step right before loading a complete new rule set. It also removes the NAT rules Docker depends on; run sudo systemctl restart docker afterwards on a Docker host. Repeat with ip6tables for IPv6.
A rollback for remote changes
On Debian and Ubuntu, iptables-apply loads a rule file and restores the previous rules unless you confirm within a timeout:
sudo iptables-apply -t 60 /etc/iptables/rules.v4
Anywhere else, build the same safety net with a timer. Save the current rules, schedule their restore, make your changes, and cancel the timer only after a new SSH session works:
sudo iptables-save | sudo tee /root/iptables.before >/dev/null
sudo systemd-run --on-active=5min /bin/sh -c 'iptables-restore < /root/iptables.before'
# change rules, test a new SSH login, then cancel the restore:
sudo systemctl list-timers 'run-*'
sudo systemctl stop run-<id>.timer
systemd-run prints the name of the timer it creates. If you lock yourself out anyway, wait five minutes. If the server is still unreachable, the provider's out-of-band console works regardless of the firewall; on CloudSat it is described in Regain access with the recovery tools.
Make the rules survive a reboot
Rules added on the command line live in memory only. On Debian and Ubuntu, iptables-persistent restores saved rules at boot:
sudo apt install iptables-persistent
sudo netfilter-persistent save
The rules are stored in /etc/iptables/rules.v4 and /etc/iptables/rules.v6. Run netfilter-persistent save again after every change you want to keep, and keep the files in version control if more than one person edits them.
On the RHEL family, firewalld is the default firewall, and Red Hat marks the iptables tools as deprecated from RHEL 9 on in favor of nftables and firewalld. If you maintain an existing iptables setup there, the iptables-services package provides the boot-time service and stores rules in /etc/sysconfig/iptables; disable firewalld first so the two do not fight.
Next to UFW, firewalld and Docker
UFW writes its own chains, such as ufw-before-input, through iptables, so manage those rules with ufw only. firewalld talks to nftables directly, so its rules may not show up in iptables -L at all; sudo nft list ruleset shows everything.
Docker publishes container ports through its own chains in the nat and filter tables, so rules in INPUT do not filter traffic to published ports. Docker leaves the DOCKER-USER chain to you for restrictions. This caveat, and how each firewall front end handles it, is covered in UFW, firewalld or nftables.
Translate a rule set to nftables
When you are ready to move, the iptables package includes translators that print the nftables version of existing rules:
iptables-translate -A INPUT -p tcp --dport 443 -j ACCEPT
sudo iptables-save > /root/rules.v4
iptables-restore-translate -f /root/rules.v4 > /root/rules.nft
Review the output before loading it with sudo nft -f. Rules that use rare extensions are marked as untranslated and need manual work.
FAQ
Are iptables rules applied in order?
Yes. Each packet walks the chain from the top, and the first rule with a terminating target such as ACCEPT, DROP or REJECT decides. If nothing matches, the chain's policy applies.
Should I use DROP or REJECT?
DROP discards the packet silently, so the client waits until it times out. REJECT answers with an error, so the client fails at once with "connection refused". Internet-facing policies usually drop. On internal networks, rejecting makes mistakes faster to diagnose, because a blocked connection fails immediately instead of hanging.
Why do my rules disappear after a reboot?
They were never saved, the restore service is not enabled, or another firewall manager loaded its own rules at boot and replaced them. Check systemctl status netfilter-persistent on Debian and Ubuntu.
Is iptables still worth learning?
Enough to read and safely change existing setups, which you will meet for years. For new servers, use a front end such as UFW or firewalld, or write nftables directly.
Run it on CloudSat. Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. See plans or deploy a server.




