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-persistent -
Save Current Rules:
During installation, you will be prompted to save your currentiptablesrules. If you want to save them later, you can run:sudo netfilter-persistent save -
Automatically Load Rules on Boot:
Theiptables-persistentpackage automatically loads the saved rules on boot.
For CentOS/RHEL:
-
Install
iptables-services:sudo yum install iptables-services -
Save Current Rules:
Save your current rules using:sudo service iptables save -
Enable the Service:
Ensure theiptablesservice 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.v4 -
Make it executable:
sudo chmod +x /etc/iptables/restore-rules.sh -
Add it to your system's startup scripts (e.g., using
cronor systemd).
By following these steps, your iptables rules should persist across reboots.
