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
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.