I have a chain appended with many rules like:
> :i_XXXXX_i - [0:0]
> -A INPUT -s 282.202.203.83/32 -j i_XXXXX_i
> -A INPUT -s 222.202.62.253/32 -j i_XXXXX_i
> -A INPUT -s 222.202.60.62/32 -j i_XXXXX_i
> -A INPUT -s 224.93.27.235/32 -j i_XXXXX_i
> -A OUTPUT -d 282.202.203.83/32 -j i_XXXXX_i
> -A OUTPUT -d 222.202.62.253/32 -j i_XXXXX_i
> -A OUTPUT -d 222.202.60.62/32 -j i_XXXXX_i
> -A OUTPUT -d 224.93.27.235/32 -j i_XXXXX_i
when I try to delete this chain with:
iptables -X XXXX
but got error like (tried iptables -F XXXXX before):
iptables: Too many links.
Is there a easy way to delete the chain by once command?
You can't delete chains when rules with '-j CHAINTODELETE' are referencing them. Figure out what is referencing your chain (the link), and remove that. Also, flush then kill.
This is potentially off-topic, but it's what I did after I found this post! For some use cases the iptables -D option might be useful. Since it allows you to clear out referring rules added programmatically with -A (if you know precisely how you added them).
E.g
can be reversed with
You need two steps, but this does it in one command.
Create a file, and place this in it.
Save the file as "clear-all-rules". Now, do this command:
Now you can clear it anytime with just one command.
Here's an alternate plan. It involves three commands, not one, but with luck, it should work.
Dump your
iptables
ruleset to a file:Remove ALL uses of (and references to) the offending chain:
Then reload the ruleset:
In the iptables man file there is an option
-S
By using
iptables -S | grep <CHAINNAMEHERE>
. For examples:you can then see which rules are blocking the deletion of the chain from the table. Go through each rule (except the
iptables -N <CHAINNAMEHERE>
and delete the rule by using the-D
optionFor example
iptables -D FORWARD -i eth0 -j TRAFFICLOG
. After you have deleted each rule for your chain flush the chain with the-F
option,iptables -F <CHAINNAMEHERE>
.Then delete your chain with the
-X
option,iptables -X <CHAINNAMEHERE>
Iptables is a complicated tool set so an ideal tutorial is needed. You can try one out at www.iptables.info
Something along these lines will get all of them in a single line without taking iptables down in any way.
This will spit out chains and delete them
I have found that you can remove the rules and chain by editing the rules file in /etc/iptables/rules.v4. If you delete the unwanted chain in this file and then reload iptables, you should no longer see the chain when doing a iptables -L.