Managing network firewall rules on an Ubuntu server or desktop using UFW (Uncomplicated Firewall) is standard procedure for system administrators. However, attempting to enable or check the firewall using commands like sudo systemctl start ufw or sudo ufw enable can sometimes trigger the error Failed to start ufw.service: Unit ufw.service is masked. In the systemd init architecture, masking a unit is a deliberate administrative action that links the service unit file directly to /dev/null, making it completely impossible to start, enable, or trigger the service either manually or automatically through dependencies. When UFW is masked, your server is left without its intended iptables/nftables firewall protection until the mask is cleared.
Why Is ufw.service Masked in Ubuntu?
Services rarely mask themselves automatically. The masked state typically occurs under specific administrative or cloud-init scenarios:
- Conflicting Firewall Management Utilities: Installing alternative firewall suites (such as
firewalldoriptables-persistent) that automatically mask UFW during package installation to prevent rule collisions. - Cloud Provider VM Provisioning: Cloud VPS hosting providers (such as DigitalOcean, Linode, or AWS EC2) masking local host firewalls in their pre-baked OS images to prioritize external cloud security groups.
- Accidental Masking via Terminal: Running administrative maintenance scripts or hardening commands that executed
systemctl mask ufwinstead of disabling the service. - Corrupted or Broken Symlinks: The systemd configuration directory retaining invalid links in
/etc/systemd/system/after an interrupted distribution upgrade.
Step 1: Unmask UFW Using Systemctl
The standard method to restore a masked systemd service is using the native systemctl unmask command.
- Open your terminal or connect to your Ubuntu server via SSH.
- Execute the unmask command with superuser privileges:
sudo systemctl unmask ufw - Systemd should return confirmation: Removed /etc/systemd/system/ufw.service.
- Next, reload the systemd manager configuration to acknowledge unit changes:
sudo systemctl daemon-reload - Start the firewall service:
sudo systemctl start ufw - Check the operational status:
sudo systemctl status ufw
Step 2: Manually Remove Stuck Mask Symlinks in /etc/systemd/system
If systemctl unmask returns an error or claims the unit is still masked, a hardcoded symbolic link pointing to /dev/null may remain in the systemd configuration directory.
- Inspect where the UFW service unit link currently points:
ls -l /etc/systemd/system/ufw.service - If the output shows
ufw.service -> /dev/null, delete the symlink manually:sudo rm /etc/systemd/system/ufw.service - Also check for any masked links located in the multi-user target directory:
sudo rm /etc/systemd/system/multi-user.target.wants/ufw.service 2>/dev/null - Reload the systemd daemon:
sudo systemctl daemon-reload
Step 3: Check for Conflicting Firewall Services
If alternative firewall daemons are running, enabling UFW can cause conflicting network filter rules and dropped SSH connections.
- Check if
firewalldis installed and active:sudo systemctl status firewalld - If firewalld is active and you prefer to use UFW, stop and disable it:
sudo systemctl stop firewalld sudo systemctl disable firewalld - Re-enable the original UFW service:
sudo systemctl enable ufw sudo systemctl start ufw
Step 4: Re-Enable UFW and Verify Active Rules
Once unmasked, activate UFW and verify that your network rules (such as OpenSSH on port 22) are intact before disconnecting your session.
- Important: If operating over remote SSH, confirm SSH traffic is explicitly permitted:
sudo ufw allow OpenSSH - Enable UFW to start automatically on system boot:
sudo ufw enable - Verify active firewall rules and operational status:
sudo ufw status verbose - The output will display Status: active alongside your defined ingress and egress rules.