iptables moving rule in a list

user1814662 picture user1814662 · May 12, 2013 · Viewed 25.4k times · Source

i have 2 rules of iptables

iptables -A INPUT -s 5.5.5.5 -j DROP
iptables -A INPUT -s 6.5.5.5 -j ACCEPT 

is there a function or a command that will swap the rules to be like this:

iptables -A INPUT -s 6.5.5.5 -j ACCEPT 
iptables -A INPUT -s 5.5.5.5 -j DROP

Answer

d3vkit picture d3vkit · Jul 10, 2013

First check the line number:

iptables -nL --line-numbers

Delete based on line:

iptables -D INPUT {line}

Insert where you would like it to be:

iptables -I INPUT {line} -i lo -p tcp --dport {port} -j ACCEPT -m comment --comment "This rule is here for this reason"

Found at these sources:

Delete Rule

Insert Rule