Saving iptables rules in Linux

Saving iptables rules in Linux
June 29, 2018 No Comments Uncategorized Jiby Mathew

iptables is a user space application program that allows a system administrator to configure the tables provided by the Linux kernel firewall (implemented as different Netfilter modules) and the chains and rules it stores.
By default iptables rules gets get flushed upon reboot.

Follow the steps bellow to make iptables rules reload after a reboot.

To view current iptables rules on your server, issue the following command as root:

iptables -L

Save iptables rules to a file:

iptables-save > /etc/iptables.rules

To make sure that the rules are applied at boot, we will create a new file

vim /etc/network/if-up.d/loadiptables

paste the below content to the file

#!/bin/bash
 /sbin/iptables-restore < /etc/iptables.rules
exit 0

This load the configuration when the network interface is up.

We can do the same to save the iptables configuration automatically on shutdown by creating the following file
vim /etc/network/if-down.d/saveiptables

#!/bin/sh
 iptables-save > /etc/iptables.rules
exit 0

Changing the permission so the files can be executed

chmod +x /etc/network/if-down.d/saveiptables
chmod +x /etc/network/if-up.d/loadiptables

This is done on Debian OS.

Tags
About The Author

Leave a reply

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