Iptables: How to save and restore rules at boot & shutdown
So you learned how to write your own iptables, unfortunately if you got it configured just the way you want and you reboot, your rules are lost. These few steps will correct that in no time.
First thing first, you want to save your iptables rule set.
sudo sh -c “iptables-save > /etc/iptables.rules
You will also want to save a backup of this ruleset in your home directory just in case the /etc/iptables.rules file gets over written.
pre-up iptables-save < /home/username/iptables.rules
Then modify the /etc/network/interfaces configuration file to apply the rules automatically. You will need to know the interface that you are using in order to apply the rules - if you do not know, you are probably using the interface eth0.
sudo vi /etc/network/interfaces
When in the file, search for the interface you found, and at the end of the network related lines for that interface, add the line:
pre-up iptables-restore < /etc/iptables.rules
You can also prepare a set of down rules and apply it automatically using the above steps, except add this to the end of the network lines:
post-down iptables-save -c > /etc/iptables.rules
A fully working example using both from above:
auto eth0 iface eth0 inet dhcp pre-up iptables-restore < /etc/iptables.rules post-down iptables-save -c > /etc/iptables.rulesSave the file, reboot your computer and do a iptables -L to check if the rules are what you set them to be.
Related Posts
Tags: iptables
[...] Also remove firestarter or whatever graphical firewall frontend you’re using. Learn to control iptables at boot up and shutdown. It’ll be much [...]