Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

A while back I was watching over the shoulder of a Linux guru and he issued a command that listed all the iptables rules in the format of the append commands you would need to issue in order to build them, but despite all my googling and man page searching I can't figure out what he did.

For example if the output from iptables -nL INPUT was:

Chain INPUT (policy DROP)
num  target     prot opt source               destination
1    ACCEPT     tcp  --             tcp dpt:80

The output from the command I'm looking for would be something like:

1  -A INPUT -i eth0 -p tcp --dport 80 -d -j ACCEPT

I seem to remember that it was just another option passed into the standard iptables -L command, but I could be wrong. Does anyone know of a command to accomplish this?

share|improve this question
up vote 3 down vote accepted

It's super simple: iptables -S gives you exactly what you ask for.

share|improve this answer

You can get similar output by using iptables-save command:

:INPUT ACCEPT [286:22260]
:OUTPUT ACCEPT [1462:92025]
-A POSTROUTING -s ! -d -p tcp -j MASQUERADE --to-ports 1024-65535 
-A POSTROUTING -s ! -d -p udp -j MASQUERADE --to-ports 1024-65535 

There is no numbers and extra info, but you can write something like that:

iptables-save | grep -v -e "^[*:#]" -e "COMMIT" | cat -n

And the output:

     1  -A POSTROUTING -s ! -d -p tcp -j MASQUERADE --to-ports 1024-65535 
     2  -A POSTROUTING -s ! -d -p udp -j MASQUERADE --to-ports 1024-65535 
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.