Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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  --  0.0.0.0/0            192.168.0.1           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 192.168.0.1 -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
add comment

1 Answer 1

up vote 2 down vote accepted

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

*nat
:PREROUTING ACCEPT [381:53396]
:INPUT ACCEPT [286:22260]
:OUTPUT ACCEPT [1462:92025]
:POSTROUTING ACCEPT [1450:91003]
-A POSTROUTING -s 192.168.123.0/24 ! -d 192.168.123.0/24 -p tcp -j MASQUERADE --to-ports 1024-65535 
-A POSTROUTING -s 192.168.123.0/24 ! -d 192.168.123.0/24 -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 192.168.123.0/24 ! -d 192.168.123.0/24 -p tcp -j MASQUERADE --to-ports 1024-65535 
     2  -A POSTROUTING -s 192.168.123.0/24 ! -d 192.168.123.0/24 -p udp -j MASQUERADE --to-ports 1024-65535 
share|improve this answer
add comment

Your Answer

 
discard

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.