iptables and ipchains
11,907 Total views across this article’s versions; includes the historical baseline where available.
The networking overview shows how routing decisions and Netfilter hooks fit into the packet path. iptables is a userspace tool for configuring IPv4 filtering and related kernel rules. It is not the routing engine and is not specific to Ubuntu.
Check the installed version and backend before following a historical example:
iptables --version
Modern installations may use iptables syntax through the nftables compatibility backend. The xtables-nft manual explains that distinction. The examples below use iptables syntax; table and extension availability depends on the installed tools and kernel.
Terminology Confusion
The similarly named ipchains, chains, and iptables refer to different things. ipchains is an older firewall administration tool; a chain is an ordered list of rules. The chain concept continues in iptables, but the old and new tools do not have identical packet paths.
The historical ipchains input and output chains also saw forwarded traffic. In iptables, INPUT and OUTPUT are for local delivery and locally generated traffic, while FORWARD handles transit traffic. The original migration notes describe the change.
Chains and Tables
A table contains chains, and a chain contains rules. A hook is a point in the kernel’s packet-processing path. Built-in iptables chains attach to hooks; a custom chain is reached through a rule in another chain in the same table.
For example, mangle/OUTPUT and filter/OUTPUT are different chains at the same OUTPUT hook. Their common name does not make OUTPUT a container for those tables. Rules in a chain are evaluated in order, and a matching rule’s target determines what happens next.
Chains
The five conventional IPv4 hook positions are PREROUTING, INPUT, FORWARD, OUTPUT, and POSTROUTING. Ordinary incoming traffic reaches a routing decision after PREROUTING: local delivery uses INPUT, while transit traffic uses FORWARD and then POSTROUTING. Locally generated traffic has an initial route lookup, then uses OUTPUT and POSTROUTING.
Loopback, tunneling, and bridging can involve additional traversal. In particular, a locally generated packet delivered through loopback can pass through both output and input paths. Do not assume every packet can encounter only one of INPUT and OUTPUT.
| Hook | Position |
|---|---|
| PREROUTING | Before the incoming routing decision |
| INPUT | Before delivery to the local host |
| FORWARD | For traffic being routed through this host |
| OUTPUT | For locally generated traffic, after initial routing |
| POSTROUTING | Before outgoing transmission, including forwarded traffic |
Not every table has a chain at every hook. The next sections explain which chains belong to each table.
Default Chains
To list the filter table’s chains and rules, use:
iptables -t filter -L -n -v
Omitting -t filter gives the same table. A deliberately empty filter table with ACCEPT policies would show INPUT, FORWARD, and OUTPUT with no rules. Do not assume that is the state of a fresh distribution install: a firewall manager or another service may already have populated it.
PREROUTING and POSTROUTING are absent from this listing because the filter table has neither chain. This is a defined table layout, not an unexplained legacy quirk. An ACCEPT policy in this table also does not prevent a different table or later hook from dropping traffic.
How to Create Custom Chains
A custom chain groups rules for reuse and organization. Create it with -N, populate it, then reach it with a jump from another chain in the same table. Custom chains are not limited to the filter table.
With -j, reaching the end of a custom chain returns to the rule after the jump. An explicit RETURN makes the intended return point clear but is not required to prevent a hang. A terminating verdict such as DROP stops processing instead. The separate -g (goto) option has different return behavior.
Custom Chain Examples
This IPv4 example accepts incoming ICMP echo requests through a custom chain. It is a small filtering illustration, not a complete firewall. Use it only in an isolated test ruleset with an unused icmp-chain name and appropriate administrative privileges. An earlier terminating INPUT rule can prevent the appended jump from being reached.
iptables -t filter -N icmp-chain
iptables -t filter -A icmp-chain -p icmp --icmp-type echo-request -j ACCEPT
iptables -t filter -A icmp-chain -j RETURN
iptables -t filter -A INPUT -p icmp -j icmp-chain
-j and --jump are valid spellings; -JUMP is not. Accepting an echo request permits local processing. The kernel’s ICMP implementation decides whether to generate a reply, and output filtering still applies. Matching echo-reply on INPUT would instead permit replies arriving at this host; it would not create them.
The explicit RETURN handles other ICMP packets by returning to INPUT. To remove only this example, remove its reference, then its rules and chain:
iptables -t filter -D INPUT -p icmp -j icmp-chain
iptables -t filter -D icmp-chain -p icmp --icmp-type echo-request -j ACCEPT
iptables -t filter -D icmp-chain -j RETURN
iptables -t filter -X icmp-chain
Tables
Tables organize chains by function. The conventional iptables tables have the following built-in chains:
| Table | Built-in chains |
|---|---|
| raw | PREROUTING, OUTPUT |
| mangle | PREROUTING, INPUT, FORWARD, OUTPUT, POSTROUTING |
| nat | PREROUTING, INPUT, OUTPUT, POSTROUTING |
| filter | INPUT, FORWARD, OUTPUT |
| security | INPUT, FORWARD, OUTPUT |
The same chain name in two tables denotes two distinct rule lists. iptables -t mangle -A OUTPUT ... appends to mangle’s OUTPUT chain; it does not append to filter’s OUTPUT chain.
Table Priority
The table name does not set a rule’s priority within its chain. Appending with -A puts a rule at the end; inserting with -I changes its position. At a hook, kernel priorities determine the order in which the registered table chains and other operations run.
| Hook | Conventional iptables processing order |
|---|---|
| PREROUTING | raw → connection tracking → mangle → nat (destination translation) |
| INPUT | mangle → filter → security → nat (source translation) |
| FORWARD | mangle → filter → security |
| OUTPUT | raw → connection tracking → mangle → nat (destination translation) → filter → security |
| POSTROUTING | mangle → nat (source translation) |
This is the usual iptables order. Defragmentation and late connection confirmation are omitted here; raw can also be registered before defragmentation. See iptables Process Flow for those qualifications and kernel references.
Table Processing
Only the chains actually reached by a packet can evaluate it. The mangle table has a built-in chain at each of the five hooks, but this does not mean each packet traverses all five.
A target also affects traversal. LOG and MARK normally continue to later rules; ACCEPT and DROP terminate the current table traversal. ACCEPT can still be followed by processing at another table or hook. Stateful NAT establishes a binding on the first packet of a connection and uses that binding for subsequent translation.
High-Level Table Tips
Each table has a particular role. Use the table and target that match the intended operation, and verify that the packet reaches the relevant chain.
Filter
The filter table is the default selected by the iptables command. Its INPUT, FORWARD, and OUTPUT chains are the usual places for allow/drop policy. No universal set of protective rules is automatically implied by the table’s existence.
Rules can also log or jump to custom chains, so the table is not limited to a single accept/drop action for every matching rule.
Mangle
The mangle table supports specialized changes, including packet marks and some header fields. A mark can be used by an ip rule fwmark selector; it is local metadata rather than part of the transmitted IP header.
Mangle does not always evaluate every rule. A terminating verdict stops its current traversal, just as it does in other tables. Nonterminating targets such as MARK allow later rules to run. This target behavior, not a special “all rules” property, explains why several changes can apply to the same packet.
NAT
The nat table establishes address or port translation for tracked connections. The first packet selects a NAT binding; later packets use the stored binding, including the appropriate reverse translation for replies. They do not all evaluate the NAT rules again.
SNAT, DNAT, and MASQUERADE have different purposes and permitted chains. For example, MASQUERADE uses the outgoing interface’s address and is available in POSTROUTING. See the extensions manual before choosing a target.
Raw
The raw table is evaluated early in PREROUTING and OUTPUT. It can configure connection-tracking behavior, such as exempting selected packets with CT --notrack, or assigning tracking parameters.
Skipping tracking does not bypass the filter table or automatically accept traffic. It also affects features that depend on tracking, including ordinary stateful NAT and connection-state matches. Raw is not limited to a single “remove stateful inspection” function.
Security
The security table supports Mandatory Access Control rules, such as security markings used with SELinux. Its chains follow the filter table at INPUT, FORWARD, and OUTPUT.
Table and target availability depends on the kernel build, loaded modules, and security configuration. Do not assume an unsupported command will be silently ignored: inspect its exit status and error output. SELinux is a Linux security module whose enablement varies by distribution and installation; other systems use different security modules.
Packet security labels are distinct from the packet routing mark. See SECMARK and CONNSECMARK in the extensions manual.
Viewing Existing iptables Rules
List a specific table and chain, with numeric addresses and counters:
iptables -t filter -L INPUT -n -v --line-numbers
-L presents a listing; -S presents rule specifications. For the custom chain example above, before its cleanup:
iptables -t filter -S icmp-chain
The rule specifications are equivalent to:
-N icmp-chain
-A icmp-chain -p icmp -m icmp --icmp-type 8 -j ACCEPT
-A icmp-chain -j RETURN
The formatter may display the ICMP type by name or number. The INPUT chain also contains -A INPUT -p icmp -j icmp-chain, so that chain has one reference in this isolated example. Chain names are case-sensitive; ICMP-CHAIN is not icmp-chain.
| Listing column | Meaning |
|---|---|
| target | Action or custom chain reached when the rule matches |
| prot | IP protocol, such as tcp, udp, icmp, or all |
| opt | IP option indicator; usually shown as -- |
| source | Source selector; anywhere normally means no source restriction |
| destination | Destination selector; anywhere normally means no destination restriction |
Use verbose output or -S to see constraints that a short listing can obscure, such as interfaces. An apparent broad ACCEPT rule may have additional restrictions.
How to Delete Chains
Deleting a rule and deleting a chain are different operations. -D deletes a rule. -X deletes an eligible empty chain; remove references to a custom chain before deleting it. The cleanup under Custom Chain Examples demonstrates that sequence.
Deleting Rules by Chain and Number
First inspect the intended chain:
iptables -t filter -L INPUT -n -v --line-numbers
If the rule you intend to remove is currently number 3, delete that rule with:
iptables -t filter -D INPUT 3
Numbers change when rules are inserted or deleted. Inspect again before the next deletion; do not reuse stale positions.
Deleting Rules by Specification
You can instead supply a matching rule specification. For example, this deletes the first matching rule only if it already exists in filter/INPUT:
iptables -t filter -D INPUT -m conntrack --ctstate INVALID -j DROP
The table, chain, matches, and target must correspond to the rule being removed. REJECT and DROP are different targets and are not interchangeable in a deletion.
Flushing Chains (Delete All Rules)
-F deletes all rules from the specified chain. For example, this deletes filter/INPUT’s rules:
iptables -t filter -F INPUT
It does not reload a file, clear connection tracking, or change the chain’s policy. If INPUT’s policy is DROP, removing the rule that permits your administration connection can lock you out. Use a tested recovery method before changing a remote firewall.
Flush All Chains
Omitting the chain flushes the rules in all chains of the selected table:
iptables -t filter -F
This does not flush every table, delete the custom chains themselves, or reset built-in policies. Prefer the exact cleanup for the change you are undoing instead of clearing unrelated rules.
[1] Quality of Service (QoS) includes traffic scheduling and resource allocation; setting a mark alone does not implement a QoS policy.
[2] The security table is documented in the current iptables manual. Availability and behavior must be checked on the installed system; it is not defined by the absence or presence of a distribution name.
References: iptables, iptables extensions, original ipchains migration notes, and Netfilter’s usage guide.