Linux Iptables Delete postrouting Rule Command

I am a new Linux server sysadmin. I need to delete POSTROUTING rule. How do I delete postrouting rule on Linux server?

You need to use iptables and ip6tables command. These commands are used to set up, maintain, and inspect the tables of IPv4 and IPv6 packet filter rules in the Linux kernel. Let us see how to use the iptables command to to delete postrouting rule on Linux system. You must be root user to run this command.

Step #1: List postrouting rules

The syntax is as follows:
iptables -t nat -v -L POSTROUTING -n --line-number
# iptables -t nat -v -L -n --line-number
Sample outputs:

Fig.01: Linux iptables list nat rules command


  1. -t nat : Select nat table.
  2. -v : Verbose output.
  3. -L : List all rules in the selected chain i.e show all rules in nat table.
  4. -n : Numeric output. IP addresses and port numbers will be printed in numeric format
  5. --line-number : When listing rules, add line numbers to the beginning of each rule, corresponding to that rule’s position in the chain. You need to use line numbers to delete nat rules.

Step #2: Delete postrouting nat rule (version 1)

The syntax is:
iptables -t nat -D POSTROUTING {number-here}
To delete rule # 5 i.e. the following rule:

5       40  3360 SNAT       all  --  *      *            to:

Type the following command:
iptables -t nat -D POSTROUTING 5
iptables -t nat --delete POSTROUTING 5
Verify it, enter:
# iptables -t nat -v -L POSTROUTING -n --line-number
Sample outputs:

Chain POSTROUTING (policy ACCEPT 94 packets, 6392 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1       10   762 MASQUERADE  all  --  *      eth1           
2        0     0 MASQUERADE  all  --  *      eth1           
3        0     0 MASQUERADE  all  --  *      eth1            
4        0     0 MASQUERADE  all  --  *      eth1 

Another syntax to remove specific postrouting rules from iptables (version 2)

Say, you execute the following postrouting command:
# iptables -t nat -A POSTROUTING -o eth1 -s -j MASQUERADE
To delete, run the same above commands but replace the “-A” with “-D
iptables -t nat -D POSTROUTING -o eth1 -s -j MASQUERADE
Another example, run the same commands but replace the “-I” with “-D“. For example:
# iptables -t nat -I POSTROUTING -s -j SNAT --to
# iptables -t nat -D POSTROUTING -s -j SNAT --to
# iptables -t nat --delete POSTROUTING -s -j SNAT --to
The -D or --delete option delete one or more rules from the selected chain. There are two versions of this command, the rule can be specified as a number in the chain (version 1) or a rule to match (version 2) as described above.

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.


How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...