24
sudo ufw enable

Firewall is active and enabled on system startup

sudo ufw status
Status: active

But after I restart the system and run sudo ufw status, I get the message:

Status: inactive

How can I solve this problem?

By the way, my /etc/ufw/ufw.conf does have ENABLED=yes.

1
  • Post output of this command: sudo service ufw start
    – zuberuber
    Commented Feb 12, 2015 at 8:09

6 Answers 6

30

It would've been very useful if you had mention at least what the distribution is or the version of the package that you are using.

However if you are running systemd try this:

sudo systemctl enable ufw

This should enable the autostart of the service in init.d. Please be sure to enable and test the service configuration beforehand, so you don't fail at booting the system the next time

1
  • 2
    This does not work on Ubuntu 16.04
    – Raaghu
    Commented Apr 3, 2018 at 10:07
7

For me, this was a conflict with firewalld, and was resolved with sudo systemctl disable firewalld

3
  • 3
    A big thank you to you! I was fighting with this problem on a suse box and your answer helped me
    – mrbolichi
    Commented Jan 9, 2022 at 13:30
  • 1
    same as the comment above, using Oracle cloud Ubuntu instances Commented Sep 21, 2022 at 8:11
  • Same as comment above, solved my problem, why I can't visit some port when I enabled it clearly in ufw on Ubuntu.
    – imikay
    Commented May 7 at 0:20
4

You could add the enable command to your rc.local script which runs on startup.

For Debian, it's located in /etc/rc.local
For Ubuntu, it's also located in /etc/rc.local
For Fedora, it's located in /etc/rc.d/rc.local

Add your command to the top of exit 0.

0
sudo ufw allow ***
sudo ufw enable
sudo systemctl disable firewalld

(just in case you have other firewall managers enabled)

0

Had the same issue on Ubuntu 20.04.1 LTS, this solution worked for me:

https://devtidbits.com/2019/07/31/ufw-service-not-loading-after-a-reboot/

0

Just a slight modification from the answer @peterh gave, for those if you don't already have the rc.local in those folders (/etc/ or /etc/rc.d/), like in Archlinux, you'll have to create your own. I did that slight bit of modification as follows:

(This is done in a systemd OS)

create a text file /etc/systemd/system/rc-local.service using whatever tool you like, I'm using vim so it'll be:

sudo vim /etc/systemd/system/rc-local.service

And then add:

[Unit]
Description=Enable /etc/rc.local


[Service]
Type=oneshot
ExecStart=/etc/rc.local start


[Install]
WantedBy=multi-user.target

After that, run:

sudo systemctl enable rc-local.service

It'll create a symlink to run the service on startup (according to one person on Archlinux forum, you don't need this command to enable it because if the file exists, the system will automatically enable it, but just in case.)

And finally you can add the rc.local script to /etc/ directory. You can read about it online and create a file of your prefrence but for the sake of simplicity I'll just use this:

sudo vim /etc/rc.local

# rc.local file
#!/bin/sh

# Enable firewall (sudo command)

ufw enable

# Must always be the last line
exit 0

Mark it as executable:

chmod +x /etc/rc.local

You can check if the file is correct and is working without having to reboot by sudo systemctl enable rc-local.service and then see its status sudo systemctl status rc-local.service if there's any indicator that the script was shutdown inactive that means the proccess was successfully executed

FYI: Now you can add any command using sudo to run on startup in rc.local , just be careful

Again this answer may be excessive but I just want to elaborate a bit more from above, you may have resolved your question by now but I'm adding this for anyone who just happen to stumble across this site and still don't know what to do.

That's it, have a great day!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .