Short answer
/usr/sbin, and on Debian a normal user’s PATH has no sbin folder, so bash: ufw: command not found appears even when ufw is installed. Run it as sudo ufw status, or as root after su - (plain su keeps your PATH). If you get sudo: ufw: command not found, ufw is not installed: sudo apt install ufw.Where to start
- If you typed
ufwwithout sudo as a normal user on Debianuse sudo, /usr/sbin is not on your PATH - If you became root with
suand it still failsuse su - instead of su - If the answer is
sudo: ufw: command not foundufw is not installed - If you are on UbuntuUbuntu ships ufw, so check the image
- If you are on Rocky Linux, AlmaLinux or RHELuse firewalld, or ufw from EPEL
- If you are on Alpine or inside a containerapk, or the host’s firewall
Check what the shell can see
First find out whether ufw exists at all and whether this shell can reach it. None of these lines changes anything:
command -v ufw # path of the ufw this shell would run
ls -l /usr/sbin/ufw # is the file there at all?
dpkg -l ufw # Debian, Ubuntu: is the package installed?
echo "$PATH" | tr ':' '\n' # the folders this shell searches, in orderOn one of our Debian 12 servers ufw is installed. Given the PATH a normal user gets on Debian, which has no sbin folder in it, bash and sh still cannot find it, and both exit with status 127:
$ env -i PATH=/usr/local/bin:/usr/bin:/bin bash -c 'ufw status'; echo $?
bash: line 1: ufw: command not found
127
$ env -i PATH=/usr/local/bin:/usr/bin:/bin sh -c 'ufw status'; echo $?
sh: 1: ufw: not found
127The file is there, the package is installed, and the same shell finds ufw as soon as /usr/sbin is on PATH:
$ ls -l /usr/sbin/ufw
-rwxr-xr-x 1 root root 4959 May 18 2023 /usr/sbin/ufw
$ dpkg -l ufw | tail -1
ii ufw 0.36.2-1 all program for managing a Netfilter firewall
$ env -i PATH=/usr/local/bin:/usr/bin:/bin bash -c 'command -v ufw; echo $?'
1
$ env -i PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin bash -c 'command -v ufw'
/usr/sbin/ufwIn an interactive bash the message reads bash: ufw: command not found, in an SSH login shell it starts with -bash:, and zsh prints zsh: command not found: ufw. Status 127 is the shell’s code for a command it could not find, see exit code 127. Two results tell you where to go next:
lsfinds the file,command -vdoes not: ufw is installed and only your PATH is missing/usr/sbin. Read the next two sections.- Neither finds anything: the package is not installed. Install it, or use the firewall your system ships with.
Debian keeps /usr/sbin off a normal user’s PATH
Debian gives root and everyone else different PATHs. /etc/profile, which bash and sh read when they start as login shells, sets them like this on our Debian 12 server, and Debian 13 ships the same two lines:
$ grep -n PATH /etc/profile
5: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
7: PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
9:export PATHLine 5 is for root (user ID 0), line 7 for everyone else. /etc/login.defs holds the same pair as ENV_SUPATH and ENV_PATH, which su reads:
$ grep -E '^ENV_(SU)?PATH' /etc/login.defs
ENV_SUPATH PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ENV_PATH PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/gamesThe sbin folders hold system administration programs, and ufw is one of them: it changes the kernel’s packet filter, which only root may do. So the fix is not to hunt for the program but to run it as root:
sudo ufw status verbose # the normal way
sudo -i # or a root login shell, then: ufw statusWith secure_path set in /etc/sudoers, as in Debian’s default file, sudo does not use your PATH for the command it starts: the value of secure_path becomes the PATH, and on our server it includes /usr/sbin (reading the file needs root):
$ sudo grep secure_path /etc/sudoers
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"su versus su -: root, and still not found
This one catches people who are already root. On Debian, su comes from util-linux, and without the dash it leaves most of your environment alone: by default it sets only HOME and SHELL (plus USER and LOGNAME when the target is not root). It sets PATH only if ALWAYS_SET_PATH is yes in /etc/login.defs (PAM settings can also change it), and on our server that line does not exist. The root shell you get from plain su therefore still searches your user folders, and ufw is not found although you are root.
su # root, but PATH is still yours: ufw not found
su - # root login shell: PATH from ENV_SUPATH, ufw found
id -u # 0 means you are root
echo "$PATH" | tr ':' '\n' # a root login shell lists /usr/sbinsu - (the same as su -l or su --login) starts a login shell: it clears the environment, sets PATH and changes to root’s home directory. On Debian the PATH comes from ENV_SUPATH, and the login shell then reads /etc/profile, whose root line has the sbin folders as well. sudo -i gets you to the same place: it runs root’s shell as a login shell, which reads the same files.
sudo: ufw: command not found means ufw is not installed
When sudo itself gives the answer, PATH is no longer the explanation: sudo searched secure_path, /usr/sbin included, and found nothing. The package is missing. Debian does not install ufw by default. Its priority is optional, and Debian Policy defines standard as the set that is installed when you select nothing else:
$ apt-cache show ufw | grep -E '^(Package|Version|Priority|Section):'
Package: ufw
Version: 0.36.2-1
Section: admin
Priority: optionalIt is in Debian’s main repository, so installing takes two commands:
sudo apt update
sudo apt install ufw
sudo ufw status verbose # Status: inactive, until you enable itInstalling ufw does not switch it on. Debian’s package ships /etc/ufw/ufw.conf with ENABLED=no, and the comment above that line says what matters on a server: add a rule for your remote connection before you start ufw. Allow SSH first, then enable. The whole order of steps is in UFW firewall setup.
No sudo on the machine at all? Minimal images sometimes come with root only. Log in as root and drop sudo from the commands, or create a sudo user first.
Ubuntu ships ufw
Ubuntu’s server documentation calls ufw the default firewall configuration tool, and it is part of the standard images: the package manifest of Canonical’s Ubuntu 24.04 server cloud image lists ufw 0.36.2-6. It is initially disabled, as on Debian. Two things differ from Debian:
- The PATH on a fresh Ubuntu system normally includes
/usr/sbinalready; Ubuntu’s wiki shows/usr/local/sbin:/usr/local/bin:/usr/sbin:...as the typical value. A normal user who typesufw statusthen reaches ufw and getsERROR: You need to be root to run this scriptinstead of command not found. The fix is the same:sudo. - A real command not found on Ubuntu points to a container or minimal image, or ufw was removed.
sudo apt install ufwbrings it back.
Rocky Linux, AlmaLinux and RHEL: use firewalld
These systems do not ship ufw. Rocky Linux’s documentation calls firewalld the recommended and included firewall, AlmaLinux’s wiki says firewalld is enabled by default in a standard installation (some cloud images leave it out: sudo dnf install firewalld), and the firewalld project lists Red Hat Enterprise Linux 7 and later among the systems where it is the default firewall management tool. The BaseOS and AppStream repositories of Rocky 9 contain no ufw package at all. The firewalld versions of the usual ufw commands:
| ufw | firewalld |
|---|---|
sudo ufw status | sudo firewall-cmd --state |
sudo ufw status verbose | sudo firewall-cmd --list-all |
sudo ufw allow 80/tcp | sudo firewall-cmd --permanent --add-service=http |
sudo ufw allow 8080/tcp | sudo firewall-cmd --permanent --add-port=8080/tcp |
--permanent writes a rule to the saved configuration only; run sudo firewall-cmd --reload afterwards to load it. If you want ufw anyway, it comes from EPEL: Fedora’s package database lists it for EPEL 8, 9 and 10 at version 0.35, older than the 0.36.2 on Debian 12. On Rocky and Alma, sudo dnf install epel-release followed by sudo dnf install ufw gets it. Two firewall managers writing rules into the same packet filter make it hard to tell which rule wins, so stop firewalld first (sudo systemctl disable --now firewalld) and allow SSH in ufw before you enable it. On most of these servers, staying with firewalld is the simpler path.
Alpine and containers
Alpine
Alpine packages ufw in its community repository (0.36.2, the same version as Debian 12), so apk add ufw as root installs it. The order of steps is the same as everywhere else: SSH rule first, then enable.
Inside a Docker container
Container images are cut down to the minimum. The package manifests of the Debian 12 Docker image and of Canonical’s Ubuntu 24.04 base image list no ufw, no iptables and no sudo, so ufw: command not found inside a container is expected. Do not install it there. Rules inside a container would not protect the host, and the firewall for a server belongs on the host itself.
Docker’s published ports skip ufw
The opposite surprise comes once ufw works on a host that also runs Docker. Docker’s documentation is blunt about it: with Docker’s default iptables setup, published container ports are routed in the nat table, before packets reach the INPUT and OUTPUT chains that ufw uses, so your ufw configuration is effectively ignored for them. A container started with -p 8080:80 answers on port 8080 from the internet even when ufw denies 8080.
If only the host itself, or a reverse proxy on it, should reach the container, publish the port on localhost. This is the example from Docker’s port publishing page:
docker run -p 127.0.0.1:8080:80 -p '[::1]:8080:80' nginxOn Docker older than 28.0 (Debian 12’s own docker.io package is 20.10), machines on the same local network can still reach a port published on localhost, so use Docker 28 or newer for this.
Then check from another machine which ports really answer. Which port belongs to which service is in common ports, and if the docker command itself is missing, see docker: command not found.
Confirm the fix
sudo ufw status verbose # prints a status: ufw is installed and runs as root
dpkg -l ufw # Debian, Ubuntu: a line starting with ii means installed
command -v ufw # as a normal user on Debian: no output, and that is fineOn Rocky and Alma, rpm -q ufw tells you whether the EPEL package is installed and sudo firewall-cmd --state whether firewalld is running. Once ufw answers, the next steps (default policy, which ports to open, limiting SSH) are in the UFW setup guide, and fail2ban handles repeated login attempts that a static rule cannot tell apart from real users.
Want a server to practise on? Our Standard KVM plans run Debian or Ubuntu with full root access, which is all a ufw setup needs.
How we tested
- OS image
- Debian 12 (bookworm)
- Versions
- ufw 0.36.2-1GNU bash 5.2.15dash (/bin/sh)sudo 1.9.13p3util-linux 2.38.1
- Tested on
- Where
- One of our own Debian 12 servers, with ufw installed
- Method
- ufw status was run only under env -i with PATH=/usr/local/bin:/usr/bin:/bin, so the shell could not find it; ufw never ran with /usr/sbin on PATH and no firewall rule was read or changed. Also run as shown: command -v, ls -l, dpkg -l, apt-cache show and grep on /etc/profile, /etc/login.defs and /etc/sudoers. The root check and the SSH prompt are quoted from ufw's source on the same server.
- From vendor sources, not run
- su(1) from util-linux and sudoers(5) on manpages.debian.org (bookworm), Debian Policy on priorities, Debian's ufw.conf and the trixie /etc/profile on sources.debian.org, Ubuntu Server firewall docs, the Ubuntu wiki PATH page and the Ubuntu 24.04 cloud image manifest, the Rocky Linux firewalld guide and Rocky 9 repository listings, the AlmaLinux wiki, firewalld.org, the Fedora package database (EPEL), the Alpine package index, the Debian and Ubuntu container image manifests, Docker's packet filtering and port publishing pages (all read 2026-11-08)
Frequently asked
Why does Debian say ufw: command not found when ufw is installed?
ufw is installed in /usr/sbin, and Debian's /etc/profile gives normal users a PATH without any sbin folder (/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games). Run it as root with sudo ufw status, or from a root login shell. Adding /usr/sbin to your own PATH only changes the error to ERROR: You need to be root to run this script.
What does sudo: ufw: command not found mean?
That ufw is not installed. sudo replaces your PATH with secure_path from /etc/sudoers, which on Debian includes /usr/sbin, so sudo would find ufw if it were there. Debian gives ufw priority optional and does not install it by default: run sudo apt update and sudo apt install ufw, then allow SSH before sudo ufw enable.
Why is ufw not found after su but found after su -?
Plain su keeps your own PATH and by default sets only HOME and SHELL, so the root shell still has no sbin folders. su - starts a login shell that clears the environment and takes root's PATH from ENV_SUPATH in /etc/login.defs and from /etc/profile, both of which include /usr/sbin. sudo -i gets you to the same place.
Does ufw work on Rocky Linux, AlmaLinux or RHEL?
They ship firewalld instead, and Rocky 9's BaseOS and AppStream repositories have no ufw package. Use firewall-cmd, for example sudo firewall-cmd --permanent --add-service=http followed by sudo firewall-cmd --reload. ufw 0.35 is available from EPEL 8, 9 and 10; if you install it, stop firewalld first and allow SSH before you enable ufw.