Automatically restore firewall filter rules
Reloaded
The Linux iptables packet filter lacks an easy way to load rules automatically after restarting a system, but you can automate this process several ways.
Every administrator has to determine how to protect a network reliably against unauthorized access and ensure that the (sub)network fulfills its task as expected and is not misused as a starting point for malicious activities. At the network level, you have various ways and means at your disposal, including, for example, managing credentials for authenticating users (e.g., via PAM and LDAP), the appropriate selection of correctly configured services, and correct network device configurations.
Routers and firewalls are often used to isolate individual network segments, and the Linux kernel manages the firewall rules in the system's RAM. I demonstrate different methods for permanently storing your firewall settings by using iptables, thus removing the need to enter them again whenever you reboot.
Firewall Protection
From a network perspective, a firewall's primary objective is selecting and limiting the network packets that reach a network via a monitored interface on another network. In practice, firewalls are deployed not only as software directly on the system to be protected, but also in the form of separate hardware appliances that often combine various services under one roof.
In short, a firewall is a packet filter. Filtering is performed, for example, on the basis of (1) the IP address, (2) the connection state, (3) the MAC address, or (4) the payload of a packet.
The iptables
package by the Netfilter project [1] includes the iptables
command, which covers cases 1 and 2; for case 3, ebtables
[2] steps in. In the future, nftables
will combine the iptables, ip6tables, ebtables, and arptables projects under one roof. For case 4, evaluating and limiting user traffic, you need an application-level firewall, such as the Squid [3] proxy. In this article, I only look at iptables.
Operations
The iptables
command works in chains with rules (filters) and actions (targets). On the basis of these criteria, the software fields packages (ACCEPT
) and discards (DROP
or REJECT
) or forwards and rewrites them (MASQUERADE
).
Listing 1 shows a typical call sequence. In this example, only connections via port 22 with a source IP address between 192.168.45.0 and 192.168.45.255 are allowed to pass. The entry in line 1 clears all the existing rules; the commands in lines 2 to 4 set the default behavior.
Listing 1
Typical iptables
Line 5 adds a filtering rule to the chain for received packets. This limits the accepted packages (-j ACCEPT
) to TCP packets (-p tcp
) for the target port 22 (--dport 22
) from the subnet 192.168.45.x (-s 192.168.45.0/24
). Line 6 allows also the corresponding response packets (-j ACCEPT
) – original from port 22 here (--sport 22
) to the target network 192.168.45.x (-d 192.168.45.0/24
) and only for existing connections (--state ESTABLISHED
).
To apply these commands to IPv6, you need to use the new ip6tables
, which uses the same syntax.
Validity
As mentioned previously, the iptables rules apply only as long as the system is running. The software does not store the rules persistently; it only keeps them in main memory. If you turn off the computer or reboot it, the current firewall settings will be lost. To prevent having to enter them every time you reboot, you need to store the rules permanently and enable them automatically when the computer starts up.
The variants discussed here are based on my own experience and the firewall entries from the Debian wiki [4]. The intent was to use only built-in tools. The variations also show a selection of solutions – you can decide which best fit your purposes.
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
-
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.
-
DebConf24 to be Held in South Korea
Busan will be the location of the latest DebConf running July 28 through August 4
-
Fedora Unleashes Atomic Desktops
Fedora has combined its solid distribution with rpm-ostree system to make it possible to deliver a new family of Fedora spins, called Fedora Atomic Desktops.
-
Bootloader Vulnerability Affects Nearly All Linux Distributions
The developers of shim have released a version to fix numerous security flaws, including one that could enable remote control execution of malicious code under certain circumstances.