iptables at startup

How to start iptables rules automatically at system startup.

·1 min read
Contents

iptables rules are created and modified using iptables command, saved using iptables-save and we can load the saved files using iptables-restore command.

It is possible to create a script using the above commands to create and save an iptables file, and load it again at system startup. But there is an easier way.

`iptables-persistent` for Debian based systems

The iptables-persistent package automatically loads saved iptables rules. It does this by using the rules stored in /etc/iptables/rules.v4 for IPv4, which are generated and saved when the package is installed.

To use iptables-persistent, install the package:

bash
sudo apt-get install iptables-persistent

Note: iptables is considered legacy. Its successor, nftables, is now the default firewall framework on most modern Linux distributions (Debian 10+, RHEL 8+, Ubuntu 20.10+). nftables offers better performance, a cleaner syntax, and unified handling of IPv4/IPv6 rules. While iptables commands still work on many systems via a compatibility layer (iptables-nft), new setups should use nft directly. See the nftables wiki for migration guidance.

Keep reading