Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you edit a rule in iptables? [closed]

I have a rule in iptables that looks like this:

DROP       all  --  5.158.238.32         anywhere 

But I would like to change it to be:

DROP       all  --  5.158.0.0/16         anywhere

How do I do this?

I've found info on how you add rules but this seems to append rules to the end of the list and for some reason the rule doesn't take effect unless it's higher up.

I've also found info on editing a file but my distro (debain) doesn't seem to have a file any of the locations mentioned in the articles - is there a file I can edit somewhere?

Any pointers in the right direction would be much appreciated.

Cheers

Ben

like image 801
CMSCSS Avatar asked Nov 01 '15 18:11

CMSCSS


1 Answers

Run iptables -L --line-numbers, which will give you all the current rules as well as their rule numbers. Once you have identified the line number of the rule you would like to replace, run iptables -R <chain> <rulenum> <new rule def>. In your case, the output to the first would be something like this (greatly truncated):

Chain INPUT (policy ACCEPT)
num  target     prot opt source       destination
....
12   DROP       all  --  5.158.238.32 anywhere
...

and to replace it, you would run:

iptables -R INPUT 12 -s 5.158.0.0/16 -j DROP

Hope this makes sense. Good luck!

like image 151
Joel C Avatar answered Sep 22 '22 17:09

Joel C