How to Fix ‘Unit ufw.service is masked’ Error in Ubuntu

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 firewalld or iptables-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 ufw instead 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.

  1. Open your terminal or connect to your Ubuntu server via SSH.
  2. Execute the unmask command with superuser privileges:
    sudo systemctl unmask ufw
  3. Systemd should return confirmation: Removed /etc/systemd/system/ufw.service.
  4. Next, reload the systemd manager configuration to acknowledge unit changes:
    sudo systemctl daemon-reload
  5. Start the firewall service:
    sudo systemctl start ufw
  6. 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.

  1. Inspect where the UFW service unit link currently points:
    ls -l /etc/systemd/system/ufw.service
  2. If the output shows ufw.service -> /dev/null, delete the symlink manually:
    sudo rm /etc/systemd/system/ufw.service
  3. 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
  4. 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.

  1. Check if firewalld is installed and active:
    sudo systemctl status firewalld
  2. If firewalld is active and you prefer to use UFW, stop and disable it:
    sudo systemctl stop firewalld
    sudo systemctl disable firewalld
  3. 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.

  1. Important: If operating over remote SSH, confirm SSH traffic is explicitly permitted:
    sudo ufw allow OpenSSH
  2. Enable UFW to start automatically on system boot:
    sudo ufw enable
  3. Verify active firewall rules and operational status:
    sudo ufw status verbose
  4. The output will display Status: active alongside your defined ingress and egress rules.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.