Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iptables moving rule in a list

Tags:

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 
like image 746
user1814662 Avatar asked May 12 '13 19:05

user1814662


2 Answers

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

like image 57
d3vkit Avatar answered Oct 13 '22 05:10

d3vkit


We had an issue with the order of some rules, and the most efficient way I found to change this was with two tools:

  1. iptables-save
  2. iptables-restore

First dump the rules into a file:

sudo iptables-save > /root/iptrules.txt 

Then edit the file with your favorite text editor:

sudo vim /root/iptrules.txt 

Make the necessary movements and then restore the rules:

sudo iptables-restore < /root/iptrules.txt 
like image 22
ms geek Avatar answered Oct 13 '22 05:10

ms geek