iptables / ip6tables (1/3)
iptables sets rules in the Linux kernel's netfilter framework to filter, NAT or modify every IPv4 packet; ip6tables does the same for IPv6 with identical syntax. This part covers the packet path, tables, commands, targets, rule parameters, save/restore and the first group of match extensions.
97.1 What it is and where it runs
- Netfilter: tables hold chains, chains hold rules.
- IPv6 uses ip6tables: same syntax, but a completely separate ruleset.
- Linux only, package iptables (apt/dnf).
- Modern distros use the nftables backend (iptables-nft): the lab shows iptables v1.8.10 (nf_tables).
97.2 How it works: packet path, chains and tables
- Step 1
1.Packet arrives on eth0 -> PREROUTING (raw, mangle, nat: DNAT here)
- Step 2
2.Routing decision: for this host -> INPUT (mangle, filter) -> sshd / nginx
- Step 3
3.Local reply leaves through OUTPUT (raw, mangle, nat, filter)
- Step 4
4.For another host -> FORWARD (mangle, filter): router / NAT box path
- Step 5
5.Both paths end in POSTROUTING (mangle, nat: SNAT here) -> out eth0
| Table (-t) | Job | Chains |
|---|---|---|
| filter (default) | Allow / block | INPUT, FORWARD, OUTPUT |
| nat | Change addresses (DNAT, SNAT) | PREROUTING, INPUT, OUTPUT, POSTROUTING |
| mangle | Modify packet fields / marks | All five chains |
| raw | Runs before conntrack (e.g. notrack) | PREROUTING, OUTPUT |
| security | SELinux rules | INPUT, FORWARD, OUTPUT |
iptables -t filter -L -niptables -t nat -L -niptables -t raw -L -n | grep Chainiptables --table nat -S$ iptables -t nat -L -nChain OUTPUT (policy ACCEPT)DOCKER_OUTPUT 0 -- 0.0.0.0/0 127.0.0.11Chain DOCKER_OUTPUT (1 references)DNAT 6 -- 0.0.0.0/0 127.0.0.11 tcp dpt:53 to:127.0.0.11:40631DNAT 17 -- 0.0.0.0/0 127.0.0.11 udp dpt:53 to:127.0.0.11:43275$ iptables -t raw -L -n | grep ChainChain PREROUTING (policy ACCEPT)Chain OUTPUT (policy ACCEPT)The nat table already holds Docker's embedded DNS DNAT rules (127.0.0.11).
97.3 Commands: building a host firewall
iptables -A INPUT -i lo -j ACCEPTiptables -A INPUT -m conntrack \ --ctstate ESTABLISHED,RELATED -j ACCEPTiptables -A INPUT -p tcp -s 192.168.117.20 \ --dport 22 -j ACCEPTiptables -A INPUT -p tcp --dport 80 -j ACCEPTiptables -A INPUT -s 192.168.117.66 -j DROPiptables -P INPUT DROPiptables -L INPUT -n -v --line-numbersbad$ curl -s -m 3 http://192.168.117.10/n097 web$ iptables -L INPUT -n -v --line-numbersChain INPUT (policy DROP 0 packets, 0 bytes)num pkts bytes target prot opt in out source destination1 0 0 ACCEPT 0 -- lo * 0.0.0.0/0 0.0.0.0/02 8 481 ACCEPT 0 -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED3 1 60 ACCEPT 6 -- * * 192.168.117.20 0.0.0.0/0 tcp dpt:224 1 60 ACCEPT 6 -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:805 0 0 DROP 0 -- * * 192.168.117.66 0.0.0.0/0- Step 1
1.Blocked host 192.168.117.66 sends HTTP :80 to the server
- Step 2
2.INPUT checks rules top to bottom: 1 lo, 2 ESTABLISHED, 3 SSH admin: no match
- Step 3
3.Rule 4 'tcp dpt:80 ACCEPT' (everyone) matches: page returned
- Step 4
4.Rule 5 DROP is never reached: 0 pkts
- Step 5
5.Fix: -D INPUT 5, then -I INPUT 1 -s 192.168.117.66 -j DROP
iptables -D INPUT 5iptables -I INPUT 1 -s 192.168.117.66 -j DROPiptables -C INPUT -p tcp --dport 80 -j ACCEPTiptables -C INPUT -p udp --dport 53 -j ACCEPTiptables -N LOGDROPiptables -E LOGDROP LOG_AND_DROPiptables -X LOG_AND_DROPbad$ curl -s -m 3 http://192.168.117.10/[exit 28]$ iptables -L INPUT -n -v --line-numbersnum pkts bytes target prot opt in out source destination1 3 180 DROP 0 -- * * 192.168.117.66 0.0.0.0/0$ iptables -C INPUT -p tcp --dport 80 -j ACCEPT; echo exit=$?exit=0$ iptables -C INPUT -p udp --dport 53 -j ACCEPT; echo exit=$?iptables: Bad rule (does a matching rule exist in that chain?).exit=1$ iptables -X LOG_AND_DROP$ iptables -L LOG_AND_DROP -niptables: No chain/target/match by that name.| Command | Meaning |
|---|---|
| -A chain rule | Append at the end |
| -I chain [n] rule | Insert at position n (default 1) |
| -R chain n rule | Replace rule n |
| -D chain rule|n | Delete a rule |
| -C chain rule | Check if it exists (exit code 0/1) |
| -L / -S | List rules / print in save style |
| -F / -Z | Flush rules / zero counters |
| -N / -X / -E | New / delete empty / rename user chain |
| -P chain target | Policy ACCEPT or DROP |
97.4 Built-in targets and user chains
- ACCEPT lets the packet through; DROP silently drops it.
- -j USERCHAIN jumps to a user chain; RETURN goes back to the calling chain (then the policy applies if nothing else matches).
- -g (goto): a RETURN skips the chain that did the goto and returns to its caller.
- Lab: port 22 jumps to SSH_RULES; admin .20 is accepted, .21 hits RETURN and the INPUT DROP policy blocks it.
iptables -N SSH_RULESiptables -A SSH_RULES -s 192.168.117.20 \ -j ACCEPTiptables -A SSH_RULES -j RETURNiptables -R INPUT 4 -p tcp --dport 22 \ -j SSH_RULESpc$ nc -zv -w 2 192.168.117.10 22Connection to 192.168.117.10 22 port [tcp/ssh] succeeded!pc$ nc -zv -w 2 -s 192.168.117.21 192.168.117.10 22nc: connect to 192.168.117.10 port 22 (tcp) timed out: Operation in progress$ iptables -L SSH_RULES -n -v --line-numbersnum pkts bytes target prot opt in out source destination1 1 60 ACCEPT 0 -- * * 192.168.117.20 0.0.0.0/02 2 120 RETURN 0 -- * * 0.0.0.0/0 0.0.0.0/097.5 Rule parameters and other options
| Parameter | Meaning |
|---|---|
| -p icmp / ! -p icmp | Protocol, or everything but it (default all) |
| -s / -d addr | Source / destination (! inverts) |
| -i / -o eth+ | In / out interface, + is a wildcard |
| -f | Non-first IPv4 fragments |
| -c PKTS BYTES | Set a rule's counters by hand |
| -m name / -j target | Load a match extension / target of the rule |
| -v -x -n | Counters + interfaces, exact numbers, no reverse DNS |
| -w [sec] | Wait for the xtables lock (XTABLES_LOCKFILE changes the path) |
iptables -A INPUT -p icmp \ -s 192.168.117.0/24 -i eth0 -j ACCEPTiptables -A INPUT ! -i lo \ -s 127.0.0.0/8 -j DROPiptables -A INPUT -f -j DROPiptables -A OUTPUT -o eth0 -d 8.8.8.8iptables -4 -A INPUT -c 100 6000 \ -s 192.168.117.30 -j ACCEPTiptables -6 -A INPUT \ -s 192.168.117.31 -j ACCEPT$ iptables -L OUTPUT -n -vChain OUTPUT (policy ACCEPT 4 packets, 336 bytes)pkts bytes target prot opt in out source destination2 168 0 -- * eth0 0.0.0.0/0 8.8.8.8$ iptables -6 -A INPUT -s 192.168.117.31 -j ACCEPT; echo exit=$?exit=2Try `iptables -h' or 'iptables --help' for more information.$ timeout 20 iptables -L INPUTChain INPUT (policy DROP)target prot opt source destinationDROP all -- n097-bad.n097-lan anywhereSSH_RULES tcp -- anywhere anywhere tcp dpt:ssh- A rule with no -j (the OUTPUT to 8.8.8.8 rule) only counts packets.
- -6 in iptables gives an error: -4/-6 are for mixed restore files.
- Without -n, iptables does a reverse DNS lookup for every IP (names like n097-bad, ssh) and can be very slow.
97.6 ip6tables
ip6tables -A INPUT -i lo -j ACCEPTip6tables -A INPUT -p ipv6-icmp -j ACCEPTip6tables -A INPUT -m conntrack \ --ctstate ESTABLISHED,RELATED -j ACCEPTip6tables -A INPUT -s 2001:db8:117::20/128 \ -p tcp --dport 22 -j ACCEPTip6tables -A INPUT -s 2001:db8::/32 \ -p tcp --dport 80 -j ACCEPTip6tables -I INPUT 1 \ -s 2001:db8:117::66 -j DROPip6tables -P INPUT DROPbad$ curl -s -m 3 http://192.168.117.10/[exit 28]bad$ curl -s -m 3 'http://[2001:db8:117::10]/'n097 webbad$ curl -s -m 3 'http://[2001:db8:117::10]/'[exit 28]$ ip6tables -L INPUT -n -v --line-numbersnum pkts bytes target prot opt in out source destination1 3 240 DROP 0 -- * * 2001:db8:117::66 ::/03 7 552 ACCEPT 58 -- * * ::/0 ::/06 1 80 ACCEPT 6 -- * * 2001:db8::/32 ::/0 tcp dpt:80- Before the ip6tables rules the blocked host was dropped over IPv4 but fetched the page over IPv6: iptables only sees IPv4.
- Always allow ICMPv6 (protocol 58), or neighbour discovery breaks.
97.7 iptables-save and iptables-restore
iptables-save -t filter -f /tmp/rules.v4ip6tables-save -t filter > /tmp/rules.v6iptables-restore -t /tmp/rules.v4iptables-restore -t /tmp/bad.v4iptables-save -c -t filter > /tmp/counted.v4iptables-restore -c -T filter /tmp/counted.v4iptables-restore -n -v /tmp/extra.v4ip6tables-restore < /tmp/rules.v6$ iptables-save# Generated by iptables-save v1.8.10 (nf_tables) on Sun Nov 8 23:33:49 2026*filter:INPUT DROP [2:120]:SSH_RULES - [0:0]-A INPUT -s 192.168.117.66/32 -j DROP-A INPUT -p tcp -m tcp --dport 22 -j SSH_RULES-A SSH_RULES -j RETURNCOMMIT$ iptables-restore -t /tmp/bad.v4; echo exit=$?iptables-restore v1.8.10 (nf_tables): invalid port/service `eighty' specifiedError occurred at line: 11exit=2$ iptables-restore -n -v /tmp/extra.v4$ iptables -S INPUT | tail -2-A INPUT -s 192.168.117.30/32 -j ACCEPT-A INPUT -p tcp -m tcp --dport 8080 -j ACCEPT| Option | save | restore |
|---|---|---|
| -t | Only one table | Test (parse only), do not apply |
| -c | Include counters | Restore counters too |
| -f FILE | Write to a file | - |
| -T name | - | Only one table from the file |
| -n | - | No flush: just add rules |
| -w [sec] | - | Wait for the xtables lock |
97.8 Match extensions: addrtype, conntrack, dscp
iptables -I INPUT 1 -m addrtype \ --dst-type BROADCAST -j DROPiptables -I INPUT 2 -m addrtype \ --dst-type MULTICAST -j DROPiptables -I INPUT 4 -m conntrack \ --ctstate INVALID -j DROPiptables -A INPUT -p tcp --dport 22 \ -m conntrack --ctstate NEW \ --ctorigsrc 192.168.117.0/24 \ --ctdir ORIGINAL -j SSH_RULESiptables -I INPUT 5 -p icmp -m dscp \ --dscp-class EF -j ACCEPT1 2 168 DROP 0 -- * * 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type BROADCAST2 2 168 DROP 0 -- * * 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type MULTICAST$ conntrack -L -p tcp 2>&1tcp 6 118 TIME_WAIT src=192.168.117.20 dst=192.168.117.10 sport=47070 dport=22 src=192.168.117.10 dst=192.168.117.20 sport=22 dport=47070 [ASSURED] mark=0 use=1$ iptables -L INPUT 5 -n -v3 252 ACCEPT 1 -- * * 0.0.0.0/0 0.0.0.0/0 DSCP match 0x2e$ iptables -S INPUT | grep dscp-A INPUT -p icmp -m dscp --dscp 0x2e -j ACCEPT-A INPUT -p icmp -m dscp --dscp 0x2e -j ACCEPT- addrtype matches the kernel address type: LOCAL, UNICAST, BROADCAST, MULTICAST, BLACKHOLE, UNREACHABLE, PROHIBIT...; --limit-iface-in/out checks only one interface.
- conntrack is the core of a stateful firewall: NEW (first packet), ESTABLISHED, RELATED, INVALID (no known connection), UNTRACKED, SNAT/DNAT.
- Other conntrack options: --ctorigdst, --ctreplsrc, --ctorigdstport, --ctexpire, --ctstatus ASSURED|SEEN_REPLY.
- dscp matches the QoS field: ping -Q 0xb8 = DSCP 46 = class EF; --dscp-class EF and --dscp 46 save as the same rule (0x2e).
| Match (not run in lab) | Matches |
|---|---|
| -m ah / -m esp | IPsec AH / ESP SPI |
| -m bpf / -m cgroup | eBPF program / a service's cgroup |
| -m cluster | Split traffic over nodes |
| -m comment | Adds a note to the rule |
| -m connbytes / connlimit | Bytes or packets of a connection / parallel connections per IP |
| -m connlabel / connmark | Connection label / mark |
| -m cpu, dccp, devgroup, ecn | Handling CPU, DCCP ports, interface groups, ECN bits |
| -m dst, eui64, frag (IPv6) | Destination options header, MAC-based address, fragment header |
97.9 Common mistakes
- Running iptables -P INPUT DROP first over SSH: without ESTABLISHED and SSH allow rules you lock yourself out. Allow rules first, policy last.
- Appending a block rule below a broad ACCEPT: first match wins; check the counters.
- Thinking rules are permanent: they live in kernel memory and vanish on reboot; use iptables-save + iptables-restore or iptables-persistent.
- Forgetting IPv6: iptables does not touch IPv6; run ip6tables (and ip6tables-save) separately.
- Listing without -n on a busy box: reverse DNS for every IP.
97.10 Interview tips
- Table = kind of job (filter, nat, mangle, raw, security); chain = point on the packet path (PREROUTING, INPUT, FORWARD, OUTPUT, POSTROUTING).
- In every chain the first match wins; if nothing matches, the policy applies.
- DNAT happens in PREROUTING, SNAT in POSTROUTING; local traffic uses INPUT/OUTPUT, routed traffic uses FORWARD.
- Stateful base rule: -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT.
- Test a ruleset with iptables-restore -t before applying it.