iptables: command not found

This error has two common causes: iptables is not installed, or it is installed but your shell cannot find it. Check which one first.

Step 1: diagnose
command -v iptables
ls -l /usr/sbin/iptables /sbin/iptables

Both commands print nothing or "No such file": iptables is not installed, go to Fix 1.
The file exists in /usr/sbin or /sbin: it is installed but not in your PATH, go to Fix 2.
ls shows an arrow (-> /etc/alternatives/iptables): normal on Ubuntu and Debian. The file is a symlink managed by update-alternatives.

Fix 1: install iptables

SystemCommand
Ubuntu, Debiansudo apt update && sudo apt install -y iptables
RHEL 8+, Rocky, Alma, Fedorasudo dnf install -y iptables-nft
CentOS 7sudo yum install -y iptables
Alpineapk add iptables
Archsudo pacman -S iptables

Minimal server images, newer Debian releases and most container images do not include iptables. On RHEL-family systems, add iptables-services if you also want to save rules across reboots (see how to save iptables rules).

Fix 2: run it with sudo or the full path

iptables lives in /usr/sbin (or /sbin), a directory reserved for administration tools. On Debian, a normal user or a plain su session often does not have it in PATH. Any of these works:

sudo iptables -L -n
sudo /usr/sbin/iptables -L -n
su -   # note the dash: it loads root's full PATH

"sudo: iptables: command not found"

This version of the message comes from sudo and usually means the package is missing. Run ls /usr/sbin/iptables /sbin/iptables. If neither file exists, install it with Fix 1. If one exists, call it by its full path as shown above.

"iptables: not found" in scripts, Dockerfiles and Alpine

Scripts and Dockerfile RUN steps run under sh, which words the error differently: /bin/sh: 1: iptables: not found on Debian and Ubuntu images, and sh: iptables: not found on Alpine. The cause is the same as above: the package is not installed in that image, so add apt-get install -y iptables or apk add iptables to the build.

Verify the install

sudo iptables --version

For reference, this is the real output from an Ubuntu 22.04.5 LTS server with iptables v1.8.7 installed:

$ iptables --version
iptables v1.8.7 (nf_tables)
$ command -v iptables
/usr/sbin/iptables
$ ls -l /usr/sbin/iptables /sbin/iptables
lrwxrwxrwx 1 root root 26 Sep 16 09:59 /sbin/iptables -> /etc/alternatives/iptables
lrwxrwxrwx 1 root root 26 Sep 16 09:59 /usr/sbin/iptables -> /etc/alternatives/iptables

Both paths are symlinks into /etc/alternatives, which is how Ubuntu lets you switch between the nft and legacy backends.

The output ends with (nf_tables) or (legacy). Modern distributions ship iptables as a compatibility layer on top of nftables, so both variants work for the usual allow and block rules. Try listing the rules:

sudo iptables -L INPUT -n --line-numbers

On Debian and Ubuntu you can see which backend is active and switch it:

sudo update-alternatives --display iptables
sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy

Use iptables-nft and ip6tables-nft instead to switch back. Only switch if you have a reason: rules created with one backend are not visible to the other.

This is what sudo update-alternatives --display iptables prints on the same server:

iptables - auto mode
  link best version is /usr/sbin/iptables-nft
  link currently points to /usr/sbin/iptables-nft
  link iptables is /usr/sbin/iptables
  slave iptables-restore is /usr/sbin/iptables-restore
  slave iptables-save is /usr/sbin/iptables-save
/usr/sbin/iptables-legacy - priority 10
  slave iptables-restore: /usr/sbin/iptables-legacy-restore
  slave iptables-save: /usr/sbin/iptables-legacy-save
/usr/sbin/iptables-nft - priority 20
  slave iptables-restore: /usr/sbin/iptables-nft-restore
  slave iptables-save: /usr/sbin/iptables-nft-save

auto mode means the system picks the alternative with the highest priority. Here iptables-nft has priority 20 and iptables-legacy has 10, so iptables points to the nft version. The --set commands above change this to manual mode. To go back to the automatic choice, run sudo update-alternatives --auto iptables and the same with ip6tables.

Inside a Docker container

Container images are stripped down, so install iptables inside the container first. Then note that changing firewall rules from a container needs extra privileges:

docker run --cap-add=NET_ADMIN -it ubuntu bash

Without NET_ADMIN, iptables installs fine but fails with a permission error when you use it. In most cases it is better to manage firewall rules on the host instead.

Reproduce and fix it in a clean Ubuntu 22.04 container

This is the exact sequence on a clean ubuntu:22.04 image: run iptables, print the exit code, install the package, then check the version.

docker run --rm -it ubuntu:22.04 bash -c 'iptables -L; echo "exit: $?"; apt-get update -qq && apt-get install -y -qq iptables >/dev/null && iptables --version'

The real output:

bash: line 1: iptables: command not found
exit: 127
debconf: delaying package configuration, since apt-utils is not installed
iptables v1.8.7 (nf_tables)

Exit code 127 is the shell's code for "command not found", so a script can tell a missing iptables apart from a rule that failed. The debconf line about apt-utils is harmless in a minimal container. After the install, iptables --version reports v1.8.7 (nf_tables) on Ubuntu 22.04, the same as on a full server.

On macOS

macOS has no iptables, so the command will never be found there. The built-in packet filter is pf, managed with pfctl. If you are following a Linux guide, run it on a Linux server or virtual machine instead.

Other errors you may hit next

Permission denied (you must be root): add sudo. If you are already root inside a Docker container, the same message means the container lacks the NET_ADMIN capability (see above).
Can't initialize iptables table 'filter': Table does not exist (do you need to insmod?): the kernel firewall modules are missing or the environment (some containers and VPS types) does not allow iptables. Check sudo modprobe ip_tables or ask your provider.
Rules work but disappear after reboot: see saving iptables rules permanently.

After iptables is installed

Build your first rule with the iptables command generator, allow SSH without locking yourself out with allow SSH safely, or follow open port 80 on Ubuntu. On Oracle Cloud you also need the security list. To understand where each rule runs, read iptables tables and chains explained.

FAQ

Why is iptables missing on a fresh Debian or Ubuntu install?

Minimal and cloud images keep the package list short. Firewall tools are optional, so you install iptables yourself with apt.

Is iptables replaced by nftables?

Most current distributions use nftables in the kernel, and the iptables command is a compatibility wrapper (iptables-nft). Your existing iptables commands still work.

Why does it work with sudo but not without?

The binary is in /usr/sbin, which is often missing from a normal user's PATH. sudo uses a secure PATH that includes it. iptables also needs root to change rules.

What does "auto mode" mean in update-alternatives for iptables?

Auto mode means Debian and Ubuntu pick the alternative with the highest priority. iptables-nft has priority 20 and iptables-legacy has 10, so iptables points to the nft version until you set it manually.

What does exit code 127 mean when iptables fails?

Exit code 127 is the shell's code for command not found. In a script it means iptables is missing or not in PATH, not that one of your rules failed.

Does macOS have iptables?

No. macOS uses the pf packet filter and the pfctl command. iptables is a Linux tool.

Do I need iptables if I use ufw?

ufw is a front end that calls iptables or nftables underneath, so the backend must be present. Installing ufw normally pulls it in.

© PacketUtils. Tools run in your browser. Review every command before using it in production.HomeAboutContactPrivacy PolicyGitHub