iptables, the standard Linux firewall, does not save rules between reboots. You have to take care of this yourself. There are many ways to do this. What is the canonical way to do this? What are best practices?
I'll answer with my own solution, but I'm interested in other / better solutions.
Here are some example rules. Save them to /etc/iptables.rules
add this line at the end of /etc/network/interfaces
We use alot of iptables rules, so in order to ease administration we do the following:
I don't know if this is the best way to do this, but it has worked well for us.
While it is true this is environment and platform dependent, I have seen two good approaches, depending on the platform:
RHEL / CentOS: store all rules in a single /etc/sysconfig/iptables file which is read in by the iptables startup script.
Debian/Ubuntu: store all rules in separate service-specific files in /etc/iptables.d/ directory. For example, /etc/iptables.d/port_http, /etc/iptables.d/port_dns, where port_service maps to a service name in /etc/services.
In either case, the file or files are managed by a configuration tool like Chef or Puppet, and read in by the 'master' startup script for iptables that runs at boot time.
In addition to
iptables-save
(andiptables-restore
), complicated firewall schemes are better handled with dedicated programs. For example, we've usedshorewall
("iptables made easy") to configure iptables.Simpler tools are also available, like firestarter and kmyfirewall.
This is dependent on the distribution you use. Some distributions especially those based off a Red Hat distribution keep the iptables rules automatically but in there own special directory. I'm most familiar with RHEL and the iptables rules can be found at /etc/sysconfig/iptables. Becoming familiar with iptables rules syntax can be tricky at first but is quite easy once you get the hang of it.
The netfilter website has a lot of documentation on iptables including some introductions. If your up for reading a bit you can find a lot of good information here: http://www.netfilter.org/documentation/
This question is very close to being a dup of 4934 and it is related to 397.
I use firehol combined with a web interface that I developed to manage the configuration file.
I really like firehol, it provides a simpler syntax then using iptables directly.
We use a custom init-script, of course. I can use for-loops to iterate over a list of ports, parse other config files like the vpn-users, etc. Excellent!
And iptables-restore is surely the most "canonical" way of saving it.
What I want to add:
Please note that current version of iptables will for every single invokation ask the kernel to give it back the full list of chains. Then it will make the one change you ask it to do. Then it will upload the list again.
This is slow (O(n^2)), for us it needs 5 seconds which is too long ;-)
If you use iptables-restore, it all goes through in one quick operation.