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
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
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.