Automatically restore firewall filter rules
Manual
With the manual version, you use the iptables commands and tools. The iptables-save
command reads the current firewall rules. Because the program sends them to standard output, you need to redirect the output to a file by using the (>
) redirection operator (Listing 2). This file has a specific, compact format.
Listing 2
iptables Manual Method
You can restore these rules later with the command iptables-restore
(Listing 2, last line). This approach is simple and clear-cut, but still not automated. However, you can manage that task with the variations that follow.
Automated
Runlevels contain shell scripts that run with a system runlevel change. To load the previously stored firewall rules, you need to create an appropriate shell script. In it, you can either call iptables-restore
or create a separate call to iptables in the desired order for each individual rule.
The variant presented here is far more customized. It involves expanding the entries for the network interfaces in the /etc/network/interfaces
file. The entries allow you to specify a script or command that runs when the system activates or disables a network interface (Listing 3).
Listing 3
Customized
The first command in line 3 is specified after the keyword pre-up
and executes the command before activating the interface. The post-down
statement in line 4 refers to the time after disabling the interface. Therefore, the firewall only processes certain rules when a particular interface is switched on.
Resourceful Debian developers also identified the problem and devised a solution, which they dubbed iptables-persistent
[5]. It has been included since Debian 5 "squeeze" and combines the variants featured in Listing 3.
For this to happen, iptables-persistent
creates two files – /etc/iptables/rules.v4
for IPv4 and /etc/iptables/rules.v6
for IPv6 – and stores the current firewall rules during package configuration. Additionally, it creates a suitable init script named /etc/init.d/netfilter-persistent
, which you can call via service netfilter
with the usual switches: start
, stop
, reload
, and restart
.
Future Useful Information
In the context of iptables, several small programs facilitate everyday admin life. They include iptables-apply
and iptables-xml
, both of which are part of iptables, as well as iptables-converter
.
The iptables-apply
shell script helps you test firewall rules remotely. It allows you to roll back rule changes if they would take down the current connection.
To do this, the script asks whether the changes to the rules are okay. If you do not respond within a certain period of time, it rolls back the changes. This reduces the risk that you lock yourself out when changing the firewall rules, although you still have the option of communication via the serial interface, but only if the remote computer is set up for it, because the firewall rules do not take it into consideration under normal circumstances.
Access would also be possible via a KVM switch, which acts as a toggle switch that connects a set of devices (keyboard, video, mouse) to more than one computer. In this way, you can control multiple computers with one keyboard, mouse, and monitor.
The iptables-xml
script contains firewall rules in XML format. Several graphical tools for firewalls understand and process this format. Figure 1 shows an example of an XML file generated by this tool.

The iptables-converter
program is useful, as well. It converts iptables
statements to the format that iptables-save
and iptables-restore
use. Thus, you can convert existing shell scripts with sequences of iptables
statements directly to the required format. The graphical fwbuilder
(Firewall Builder) tool [6] also reads the files and generates customized package checks (Figure 2).

Thanks
The author thanks Werner Heuser and Axel Beckert for their criticism and suggestions in the run-up to this article.
Infos
- Netfilter: http://www.netfilter.org
- ebtables: http://ebtables.netfilter.org
- Squid: http://www.squid-cache.org
- Debian firewall wiki: https://wiki.debian.org/DebianFirewall
- Saving iptables firewall rules permanently: https://www.thomas-krenn.com/en/wiki/Saving_Iptables_Firewall_Rules_Permanently
- Firewall Builder: http://www.fwbuilder.org
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Direct Download
Read full article as PDF:
Price $2.95
News
-
KaOS 2022.06 Now Available With KDE Plasma 5.25
The newest iteration of KaOS Linux not only adds the latest KDE Plasma desktop but sets LibreOffice as the default.
-
Manjaro 21.3.0 Is Now Available
Manjaro “Ruah” has been released and includes the latest Calamares installer, GNOME 42, and much more.
-
SpiralLinux is a New Linux Distribution Focused on Simplicity
A new Linux distribution, from the creator of GeckoLinux, is a Debian-based operating system with a focus on simplicity and ease of use.
-
HP Dev One Linux Laptop is Now Available for Pre-Order
The System76/HP collaboration Dev One laptop, geared toward developers, is now available for pre-order.
-
NixOS 22.5 Is Now Available
The latest release of NixOS with a much-improved package manager and a user-friendly graphical installer.
-
System76 Teams up with HP to Create the Dev One Laptop
HP and System76 have come together to develop a new laptop, powered by Pop!_OS and aimed toward developers.
-
Titan Linux is a New KDE Linux Based on Debian Stable
Titan Linux is a new Debian-based Linux distribution that features the KDE Plasma desktop with a focus on usability and performance.
-
Danielle Foré Has an Update for elementary OS 7
Now that Ubuntu 22.04 has been released, the team behind elementary OS is preparing for the upcoming 7.0 release.
-
Linux New Media Launches Open Source JobHub
New job website focuses on connecting technical and non-technical professionals with organizations in open source.
-
Ubuntu Cinnamon 22.04 Now Available
Ubuntu Cinnamon 22.04 has been released with all the additions from upstream as well as other features and improvements.