How to run iptables automatically after reboot on Debian

Last updated on November 1, 2020 by Dan Nanni

If you have customized iptables rules, and would like to load the customized iptables rules persistently across reboots on Debian, you can leverage the if-up.d scripts that are located in /etc/network/if-up.d. On Debian, any script that is marked as executable and placed in /etc/network/if-up.d gets executed when a network interface is brought up.

In order to run iptables automatically after reboot on Debian, do the following.

First, customize iptables as you wish, and then save the current iptables rule-set using iptables-save command as follows.

$ sudo iptables-save > /etc/firewall.conf

The above command will dump the current iptables rule set into /etc/firewall.conf. As shown below, the rule set stored in /etc/firewall.conf can then be restored with iptables-restore command.

Now create the following if-up.d script called run-iptables, which restores the saved iptables rules.

$ sudo vi /etc/network/if-up.d/run-iptables
#!/bin/sh
iptables-restore < /etc/firewall.conf

Finally, make the run-iptables script executable.

$ sudo chmod +x /etc/network/if-up.d/run-iptables

From this point on, whenever system boots up and network interfaces are activated, the iptables rules stored in /etc/firewall.conf will be loaded into the system.

Another way to automatically load the iptables rules stored in /etc/firewall.conf is to add iptables-restore < /etc/firewall.conf command to /etc/rc.local. /etc/rc.local is a place where any startup services can be added.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal (Credit Card) or Bitcoin (1M161JGAkz3oaHNvTiPFjNYkeABox8rb4g).

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean