PDA

View Full Version : How to iptables save and restore & make iptables-persistent



Fli
01-01-2016, 11:46 PM
When doing some changes in iptables, one can first save the rules so it can be restored if something go wrong.

SAVE

iptables-save > /etc/iptables/rules.v4-datehere-working;ip6tables-save > /etc/iptables/rules.v6-datehere-working

RESTORE

iptables-restore < /etc/iptables/rules.v4-datehere-working;ip6tables-restore < /etc/iptables/rules.v6-datehere-working

----

After rules are changed and verified to be working/not blocking valid traffic, it needs to be saved to survive reboot:

Make it persistent:

On CentOS, its done by command:
"service iptables save" or "iptables-save > /etc/sysconfig/iptables"

On Debian/Ubuntu, one may try:
sudo apt install iptables-persistent && sudo iptables-save|sudo tee -a /etc/iptables/rules.v4 && sudo ip6tables-save|sudo tee -a /etc/iptables/rules.v6
(system will then load that files during boot)

NOTE: some services add iptables rules upond start and so you may consider adjusting your exported /etc/iptables/rules.v* files in case you find that after reboot there are duplicate rules.

alfread
10-27-2020, 01:34 PM
The actual iptables rules are created and customized on the command line with the command iptables for IPv4 and ip6tables for IPv6. These can be saved in a file with the command iptables-save for IPv4. These files can be loaded again with the command iptables-restore for IPv4.