How to Fix ‘sudo: command not found’ Error in Linux

Typing an administrative command prefixed with sudo only to receive bash: sudo: command not found or sh: sudo: not found is a common obstacle on minimal Linux installations, newly deployed Debian servers, Proxmox containers (LXC), and lightweight Docker containers. The sudo (Superuser Do) utility delegates root privileges to designated system users safely. While desktop Linux distributions such as Ubuntu and Fedora bundle sudo by default, minimal server images omit the package entirely to minimize container footprints and reduce the attack surface. Alternatively, the error can appear if your user’s $PATH environment variable has been corrupted or misconfigured, hiding standard system binary directories. This guide explains how to restore the sudo command and configure administrative privileges correctly.

Why Does ‘sudo: command not found’ Occur?

The absence of the sudo binary typically stems from two core causes:

  • Uninstalled sudo Package: In minimal Debian server setups, Arch Linux baseline installs, Alpine containers, or official Docker images, the sudo binary is deliberately excluded out of the box.
  • Misconfigured $PATH Environment Variable: The binary exists in /usr/bin/sudo, but the active user’s environment variable does not include /usr/bin or /usr/sbin.
  • Damaged Binary Permissions or Symlinks: Accidental deletion, permission alterations, or broken symbolic links within the system binary path.

Method 1: Switch to Root and Install sudo on Debian/Ubuntu Systems

If you have direct access to the root account password (standard in Debian netinst installations), switch to root and install sudo directly.

  1. Switch to the root user environment using su:
    su -
    Note: Using su - (with the hyphen) is critical because it loads root’s full environment variables, including /sbin and /usr/sbin.
  2. Enter the root password when prompted.
  3. Update your local package repository index:
    apt update
  4. Install the sudo package:
    apt install sudo -y
  5. Add your regular user account to the sudo group:
    usermod -aG sudo your_username
  6. Exit the root shell:
    exit
  7. Log out and log back in (or run newgrp sudo) to apply group membership changes, then test with sudo whoami.

Method 2: Install sudo on RHEL, CentOS, Rocky Linux, or Fedora

On Red Hat Enterprise Linux and its downstreams, administrative privileges are governed by the wheel administrative group.

  1. Elevate to root:
    su -
  2. Install sudo via dnf:
    dnf install sudo -y
  3. Add your regular user to the wheel group:
    usermod -aG wheel your_username
  4. Log out and log back in to test sudo privileges.

Method 3: Install sudo in Docker Containers (Debian, Ubuntu, Alpine)

Minimal container base images exclude sudo to reduce image size. When building development containers or executing commands inside a running container, install sudo as root.

  1. In Debian / Ubuntu Containers:
    apt-get update && apt-get install -y sudo
  2. In Alpine Linux Containers:
    apk update && apk add sudo
  3. In Dockerfile Best Practice:

    If creating a non-root developer container, include the following instructions in your Dockerfile:

    RUN apt-get update && apt-get install -y sudo     && echo "developer ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/developer     && chmod 0440 /etc/sudoers.d/developer

Method 4: Fix Corrupted $PATH Environment Variable

If sudo is already installed on the disk but your shell cannot find it, your $PATH variable is misconfigured.

  1. Check the location of the sudo binary using whereis:
    whereis sudo
  2. If the output points to /usr/bin/sudo, inspect your current path:
    echo $PATH
  3. If /usr/bin is missing from the output, export the standard path temporarily:
    export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH"
  4. To make this permanent, edit your shell configuration file (~/.bashrc or ~/.profile):
    nano ~/.bashrc
    Add the line export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH" to the bottom of the file, save, and reload using source ~/.bashrc.

Sudo Installation and Configuration Matrix

Distribution Root Switch Command Package Installation Command Administrative Group
Debian / Ubuntu Minimal su - apt update && apt install sudo -y sudo
RHEL / Rocky / AlmaLinux su - dnf install sudo -y wheel
Alpine Linux su - apk add sudo wheel
Arch Linux Minimal su - pacman -Sy sudo wheel

Frequently Asked Questions

Why does Debian not install sudo by default?

During the initial Debian installation wizard, if you enter a root password, Debian assumes you wish to maintain traditional Unix root/user separation and omits sudo. If you leave the root password blank during installation, Debian installs sudo automatically and grants full administrative privileges to the initial user account.

What is the difference between su and sudo?

The su (Switch User) command switches the entire shell session to another user (typically root), requiring that user’s password. The sudo command executes a single administrative command using the invoking user’s own password, providing granular privilege delegation and detailed audit logging in /var/log/auth.log.

Get the best tech tips delivered straight to your inbox.

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