Search code examples
iptables

How to remove iptables rule


I have this rule in my iptables:

sudo iptables -t nat -nvL --line-numbers

Chain PREROUTING (policy ACCEPT 14 packets, 1950 bytes)
num   pkts bytes target     prot opt in     out     source               destination         

Chain INPUT (policy ACCEPT 14 packets, 1950 bytes)
num   pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 577 packets, 41182 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REDIRECT   tcp  --  *      lo      0.0.0.0/0            0.0.0.0/0            tcp dpt:80 redir ports 8090

I tried to delete it using:

sudo iptables -D OUTPUT 1

by I got this error:

iptables: Index of deletion too big.

So after some searching on the web, I found out that should be able to delete all the rules for the chain like this:

sudo iptables -F OUTPUT

The output of this command is nothing, but when I rerun thesudo iptables -t nat -nvL --line-numbers command to list the existing rules afterwards, nothing got deleted. What am I missing?


Solution

  • Your rule was defined in table nat, so you must add -t nat explicitly.

    sudo iptables -D OUTPUT 1 -t nat 
    

    If you haven't specific the table name, the default action will use '-t filter' implicitly.