Linux Command Reference ยท Networking
Linux iptables and nft commands
Reference for the Linux iptables and nft firewall commands: tables, chains and rules, list, add and delete rules, default policies, NAT and saving rules.
The Linux kernel filters packets with a framework called netfilter. You talk to it with one of two commands: the classic iptables and its newer replacement nft (nftables). Both define rules grouped into chains, grouped into tables; a packet walks the chain for its stage and the first matching rule with a final action (ACCEPT, DROP, REJECT) decides its fate.
On current distributions the iptables command is often a compatibility layer (iptables-nft) that writes nftables rules, so the two can coexist. Most administrators use a higher-level tool (ufw, firewalld) and drop to these commands to see what it did. Both need root.
sudo iptables -L -n -v sudo nft list ruleset
Try it
Changing the firewall needs root and capabilities our sandbox does not give, so this block is read-only:
$ sudo iptables -P INPUT DROP # default policy: deny $ sudo iptables -A INPUT -i lo -j ACCEPT # allow loopback $ sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT $ sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT $ sudo iptables -L INPUT -n --line-numbers Chain INPUT (policy DROP) num target prot opt source destination 1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 2 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED 3 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
The most important property, first match wins, can be modelled in Bash:
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
iptables [-t TABLE] -A|-I|-D CHAIN [RULENUM] MATCHES -j TARGET iptables [-t TABLE] -L|-S [CHAIN] [-n] [-v] [--line-numbers] iptables [-t TABLE] -P CHAIN POLICY iptables [-t TABLE] -F|-X [CHAIN] nft list ruleset nft add rule inet filter input tcp dport 22 accept nft -f /etc/nftables.conf
| iptables option | Description |
|---|---|
-A CHAIN |
Append a rule to the end of the chain. |
-I CHAIN [N] |
Insert at position N (default 1, the top). |
-D CHAIN N or the rule |
Delete by number or by repeating the rule. |
-L, -S |
List rules as a table, or as commands (-S). |
-P CHAIN POLICY |
Set the default policy (ACCEPT or DROP). |
-F, -X |
Flush all rules; delete user-made chains. |
-p, -s, -d |
Match protocol, source, destination. |
--dport, --sport |
Match ports (with -p tcp or -p udp). |
-i, -o |
Match the incoming or outgoing interface. |
-m conntrack --ctstate |
Match connection state (NEW, ESTABLISHED, RELATED). |
-j TARGET |
Action: ACCEPT, DROP (silent), REJECT (error back), LOG, MASQUERADE, DNAT. |
Tables and chains (iptables): filter has INPUT, FORWARD, OUTPUT; nat has PREROUTING, POSTROUTING, OUTPUT (used for port forwarding and masquerading); mangle and raw are for special packet handling.
Returns: 0 on success, non-zero with a message on error (bad syntax, missing permission, unknown match module).
Usage notes
- Order matters. Rules are checked top to bottom. A broad
DROPplaced before a specificACCEPTblocks it. Use-Ito put urgent rules first. - Do not lock yourself out. Before setting
INPUTtoDROPover SSH, allow port 22 andESTABLISHEDtraffic first. Test with a timed rollback (a scheduled job that flushes the rules) when working remotely. - Changes are not persistent. Rules vanish on reboot unless saved:
iptables-save > /etc/iptables/rules.v4withnetfilter-persistenton Debian, ornftables.serviceloading/etc/nftables.conf. -nmakes listing fast. Without it,iptables -Ltries to resolve every address.DROPvsREJECT.DROPmakes the sender wait for a timeout;REJECTanswers at once. Internal services usually preferREJECT; public ones often useDROP.- IPv6 is separate.
ip6tableshas its own rules;nftwith theinetfamily covers both at once. - Containers add rules. Docker and similar tools install their own chains. Read before flushing.
- Prefer a front end for daily use.
ufworfirewalldwrite correct rules for common cases; see the lesson below.