TL;DR

sudo iptables-save shows everything, it’s adequately readable/searchable and useful to add rules. What else?

Sometimes (well, most times πŸ˜…) I get stuck in understanding how the networking goes in a machine and I start suspecting that iptables might be playing some trick.

Now, I know very very little about it, but I know that you can do all sorts of modifications/redirection/etc. and sometimes it’s just right to look into the whole thing for something. Possibly that can be searched with a regular expression.

The normal printing command is good for a single table, e.g. the default one (i.e. filter):

sudo iptables -vnL 

Alas, this does not print everything. And you also have to remember all the possible tables:

sudo iptables -t nat -vnL

Too hassle, enter iptables-save instead:

$ sudo iptables-save
# Generated by iptables-save v1.6.0 on Fri Nov 13 22:43:22 2020
*nat
:PREROUTING ACCEPT [154:68117]
:INPUT ACCEPT [71:40896]
:OUTPUT ACCEPT [511:29049]
:POSTROUTING ACCEPT [561:31249]
:DOCKER - [0:0]
-A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER
-A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER
-A POSTROUTING -s 172.17.0.0/16 ! -o docker0 -j MASQUERADE
-A POSTROUTING -s 172.17.0.3/32 -d 172.17.0.3/32 -p tcp -m tcp --dport 4000 -j MASQUERADE
-A DOCKER -i docker0 -j RETURN
-A DOCKER ! -i docker0 -p tcp -m tcp --dport 4000 -j DNAT --to-destination 172.17.0.3:4000
COMMIT
# Completed on Fri Nov 13 22:43:22 2020
# Generated by iptables-save v1.6.0 on Fri Nov 13 22:43:22 2020
*filter
:INPUT ACCEPT [117714:157800599]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [67225:7112525]
:DOCKER - [0:0]
:DOCKER-ISOLATION-STAGE-1 - [0:0]
:DOCKER-ISOLATION-STAGE-2 - [0:0]
:DOCKER-USER - [0:0]
-A FORWARD -j DOCKER-USER
-A FORWARD -j DOCKER-ISOLATION-STAGE-1
-A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -o docker0 -j DOCKER
-A FORWARD -i docker0 ! -o docker0 -j ACCEPT
-A FORWARD -i docker0 -o docker0 -j ACCEPT
-A DOCKER -d 172.17.0.3/32 ! -i docker0 -o docker0 -p tcp -m tcp --dport 4000 -j ACCEPT
-A DOCKER-ISOLATION-STAGE-1 -i docker0 ! -o docker0 -j DOCKER-ISOLATION-STAGE-2
-A DOCKER-ISOLATION-STAGE-1 -j RETURN
-A DOCKER-ISOLATION-STAGE-2 -o docker0 -j DROP
-A DOCKER-ISOLATION-STAGE-2 -j RETURN
-A DOCKER-USER -j RETURN
COMMIT
# Completed on Fri Nov 13 22:43:22 2020

If a table has a rule, it will print it out. It’s what is used for backup, so it has all the needed info, with the added bonus that the syntax in a line can be used directly to create/recreate rules.

Super-nifty!