Inserting rules in IPTables

A very small tip regarding IPTables. Those who had attended RHCE Training or cleared RHCE Certification knows what is IPTables.

I hope you all know what IPTables is and the difference between APPEND and INSERT rules in IPTables.

IPTables always scan the rules from top to bottom. And if the match is found in a rule then rest of the rules in the chain are ignored.

So sometimes it might happen, that you want to INSERT a rule at a specific position to make sense. So here is how to do it.

First check the line number where you want to insert the rule by giving –

iptables -L -n –line-number

Then insert the rule at the desired position using the command, here I want to insert some rule at position number 2 –

iptables -I INPUT 2 -s -j REJECT

You can then confirm again by issuing –

iptables -L -n –line-number

Here is the explained screenshot from my machine.

“Like” if you like it.

Inserting rules in IPTables

Inserting rules in IPTables by Network NUTS