Fix UFW not enabled after a reboot on Ubuntu 20.04 LTS

 Wed, 25 Jan 2023 07:43 UTC

Fix UFW not enabled after a reboot on Ubuntu 20.04 LTS
Image: CC BY 4.0 by cybrkyd


After a reboot on Ubuntu 20.04 LTS, UFW fails to load.

$ sudo ufw status

#output
Status: inactive

Several fixes are possible. The easiest is to instruct systemd to load the ufw.service after the netfilter-persistent service has been loaded.

Open the ufw.service file:

$ sudo nano /lib/systemd/system/ufw.service

Add the line After=netfilter-persistent.service into the [unit] block. The completed ufw.service file should be:

[Unit]
Description=Uncomplicated firewall
Documentation=man:ufw(8)
DefaultDependencies=no
Before=network.target
After=netfilter-persistent.service

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/lib/ufw/ufw-init start quiet
ExecStop=/lib/ufw/ufw-init stop

[Install]
WantedBy=multi-user.target

Reboot and run sudo ufw status to check that UFW is enabled.