To make iptables rules persistent across reboots, you can follow these steps depending on your Linux distribution:
For Debian/Ubuntu:
Install
iptables-persistent:sudo apt-get install iptables-persistentSave Current Rules: During installation, you will be prompted to save your current
iptablesrules. If you want to save them later, you can run:sudo netfilter-persistent saveAutomatically Load Rules on Boot: The
iptables-persistentpackage automatically loads the saved rules on boot.
For CentOS/RHEL:
Install
iptables-services:sudo yum install iptables-servicesSave Current Rules: Save your current rules using:
sudo service iptables saveEnable the Service: Ensure the
iptablesservice is enabled to start on boot:sudo systemctl enable iptables
For Other Distributions:
You may need to check the specific method for your distribution, but generally, you can save the rules to a file and create a script to load them on boot.
Example Script:
You can create a script to restore rules at startup:
Create a script file, e.g.,
/etc/iptables/restore-rules.sh:#!/bin/bash iptables-restore < /path/to/your/rules.v4Make it executable:
sudo chmod +x /etc/iptables/restore-rules.shAdd it to your system's startup scripts (e.g., using
cronor systemd).
By following these steps, your iptables rules should persist across reboots.
