Score:0

With `iptables` does the chains order matter?

mx flag

Does the order matter? for example:

iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A FORWARD -p tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 22 -j ACCEPT

If so would forward be in the correct order to come second in this iptables rule chain if it's read chronologically or is this rather subjective?

Anton Danilov avatar
cn flag
It's totally wrong. The order of rules is important. Read the iptables tutorial, please.
Score:1
jp flag

The order of iptables commands doesn't affect how the pre-defined chains are processed. The default chains INPUT, FORWARD and OUTPUT aren't processed sequentially and a packet going through INPUT chain won't hit FORWARD and OUTPUT chains, a packet going through FORWARD won't get into OUTPUT and INPUT chains etc. See iptables tutorial.

Score:0
np flag

You are messing the order of rules in one chain, which is important, with different chains. With your example it's irrelevant because rules in INPUT, FORWARD and OUTPUT chains are processed for totally different packets.

But if you add rules to the same chain, then the order gets absolutely important:

iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j REJECT

That way 1st rule to ACCEPT will be added 1st. Once a packet matches that rule, it is ACCEPTed and leaves the chain. I.e. the REJECT rule will never work in the example above as the packet will always first match ACCEPT rule.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.