Updating Iptables rule using --line-number Option

There was a requirement for me to change an existing iptables rule with a new IP Address range. So, for this --line-numbers which comes with iptables proves to be very handy.

The requirement for me was to change the existing  range to  network in an iptables rule.

First I will grep  for the rule with the --line-numbers option.

$ sudo iptables -L -n --line-numbers | grep 10.200
16   ACCEPT     all  --           state NEW

$ sudo grep 10.200 /etc/sysconfig/iptables
-A INPUT -s -m state --state NEW -j ACCEPT

So, this gives me the line number where the rule existed. Since we have the line number, it will become easy for us to change the  rule by using the line number of the rule as shown below

$ sudo iptables -R INPUT 16 -s -m state --state NEW -j ACCEPT

Now, Lets list the iptables rules once again . We can see the Network range has been updated .
$ sudo iptables -L -n --line-numbers | grep 10.200
16   ACCEPT     all  --           state NEW


Popular posts from this blog

PSSH : Parallel SSH to execute commands on a number of hosts

How to add check_http as a service in Nagios Monitoring using NRPE

Configuring Nagios to monitor services using NRPE