How To Save iptables Rules Persistently?

The iptables rules are used to filter network traffic according to the different restrictions. The iptables store the rules in the system memory and do not save these rules persistently to the disk as a file by default. But there are different methods to save iptables rules persistently to a disk.

List iptables Tules

Before saving the iptables rules we can list current iptables rules with the iptables command. The sudo command is used to provide the root privileges to access firewall rules. The -L , -n and -v options provided. The -L option is used to list iptables rules, -n option is used to provide rules with line numbers.

$ sudo iptables -L -n -v
List iptables Tules

Save iptables Tules To iptablesRules.v4 File

The iptables-save command can be used to generate the iptables rules in a strcutured way which can be redirected into a specific file. But the iptables-save command should be executed as root. First, we change the current login shell to the root user like below.

$ sudo su

Alternatively, we can also log in with the root user via the bash or GUI desktop environment.

Now we run the iptables-save command and this command creates the iptables tules in a raw way to save into the persistent iptables configuration file named iptablesRule.v4 . Even there is no standard path to save iptables rules the /etc/iptables/ is one of the most popular paths.

$ iptables-save > /etc/iptables/iptablesRule.v4

Restore iptables Tules From iptablesRules.v4 File

Saved iptables rules can be restored easily by using the iptables-restore command providing the saved rule file. In the following example, we restore the persistent iptables rules file.

$ iptables-restore < /etc/iptables/iptablesRule.v4

Leave a Comment