Iptables – the difference between iptables -X and iptables -F

iptables

In many examples, I see the following 2 consecutive commands:

iptables -F
iptables -X

From the man page, I cannot figure out the difference between flushing and deleting. Is there a difference between these two?

Best Answer

For all chains you can -F :

+---------------+       +---------------+
|               |       |               |
| Chain MyChain |       | Chain MyChain |
|     Rule 1    |  -F   |      is       |
|     Rule 2    |       |     empty     |
|     Rule 3    |  ==>  |               |
|               |       |               |
+---------------+       +---------------+

For user defined chains only (chain created with iptables -N MyChain) you can -X if it is empty :

+---------------+
|               |
| Chain MyChain |         Chain MyChain
|      is       |  -X      does not exist
|     empty     |
|               |  ==>
|               |
+---------------+

Both

iptables -F
iptables -X

are used because one can delete a user defined chain only when it is empty. Built-in chains cannot be deleted, but can be flushed.

Related Topic