Skip to content

/errors/ufw/you-need-to-be-root-to-run-this-script

ufw: ERROR: You need to be root to run this script

The commands on this page have not been executed by our verification runner yet.updated
tool
ufw
exit code
1
versions
ufw 0.36
os
linux

ERROR: You need to be root to run this script is ufw refusing to run because the effective user is not root. ufw manages iptables/nftables rules and reads its configuration in /etc/ufw and /lib/ufw, all of which need root, so even ufw status fails without privileges. Run it with sudo: sudo ufw status verbose.

Fix

bash
sudo ufw status verbose
sudo ufw allow 22/tcp
sudo ufw enable

In a script that may run as a normal user, check first:

bash
if [ "$(id -u)" -ne 0 ]; then
  echo "run as root (sudo $0)" >&2
  exit 1
fi
ufw --force enable

--force skips the interactive "Command may disrupt existing ssh connections. Proceed with operation (y|n)?" prompt, which scripts cannot answer.

Allow one user to run ufw only

text
# /etc/sudoers.d/ufw  (edit with: sudo visudo -f /etc/sudoers.d/ufw)
deploy ALL=(root) NOPASSWD: /usr/sbin/ufw

The user then runs sudo ufw ... without a password and without getting general root access.

Sources