Making IPTables survive a reboot – Saving and Restoring

By default Ubuntu / Debian do NOT save any IPTables settings when shutting down. I think the thought behind this is that if you screw up a config, then a restart will save you.

In some instances, such as a traffic routing VPN or you have specific firewall needs, custom IPTables rules are needed to be loaded on boot.

To see what your current IPTables rules are, use:

iptables -L

To save the IPTables to a file, such as ‘firewall.conf’ use:

iptables-save > /etc/firewall.conf

To then reload and restore the ruleset upon boot (or actually when the network goes up, which makes a lot more sense) we need to make a script in /etc/network/if-up.d/ that loads the presaved file. For example, I’ve called mine /etc/network/if-up.d/iptablesrestore and it looks like:

#!/bin/sh
iptables-restore < /etc/firewall.conf

Don’t forget to chmod +x /etc/network/if-up.d/iptablesrestore to make it executable.

Codemonkey of Node.js, Django, PHP and all things HTML5 Solving tech challenges, one bit at a time.

Posted in iptables, linux Tagged with: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*