Why UFW has to be (re)started at boot time if it's only iptables rule manager?

README from source package says:

When installing ufw from source, you will also need to integrate it into your
boot process for the firewall to start when you restart your system. Depending
on your needs, this can be as simple as adding the following to a startup
script (eg rc.local for systems that use it):

# /lib/ufw/ufw-init start

For systems that use SysV initscripts, an example script is provided in
doc/initscript.example. See doc/upstart.example for an Upstart example. Consult
your distribution's documentation for the proper way to modify your boot
process.

On my system I have this:

# /etc/ufw/ufw.conf
#

# Set to yes to start on boot. If setting this remotely, be sure to add a rule
# to allow your remote connection before starting ufw. Eg: 'ufw allow 22/tcp'
ENABLED=yes

So, why does simple iptables rule manager need to be started at boot time? Is there any secret to that, or it merely checks if all rules are in place ?


Solution 1:

When system is booted, there is no firewall rules. Kernel is not saving those to anywhere.

If you don't start UFW (or some other firewall manager), your iptables is empty. Also, default rule for each chain (FORWARD, INPUT, OUTPUT) is ACCEPT, so everything is allowed.

So UFW is not just checking, it's actually putting those rules in place, and creating additional chains it uses for rule management.

Solution 2:

When you run iptables (the lowlevel tool for manipulating netfilter firewall rules in Linux), the rule is loaded into the kernel only, and therefore are in RAM. When you shutdown or reboot, the kernel is reinitialized since RAM is not persistent memory, and it defaults to ACCEPT for all tables (or no firewall).

Therefore to have a firewall available after booting, you must load iptables rules into the kernel. You can do this manually by adding iptables rules to a script that is run on boot, such as /etc/rc.local, or use a tool to do the for you. ufw is one such tool and when enabled it will take care of loading firewall rules on boot for you, if it is integrated into the boot process like the README says. If ufw is packaged for your distribution, like it is in Ubuntu, then the packaging will take care of that for you. Simply put, after installing ufw via apt-get in Ubuntu, you don't have to do anything more to integrate it into the system other than run 'sudo ufw enable'.

Solution 3:

To save ufw status you have to run this command:

sudo invoke-rc.d iptables-persistent save

then you rules will be saved and loaded after reboot.