Fix: sudo: command not found on Linux
Quick Answer
How to fix 'sudo: command not found' on Linux — caused by sudo not installed, missing PATH in scripts, Docker containers without sudo, and su vs sudo confusion on minimal systems.
The Error
You run a command with sudo on a Linux system and get:
bash: sudo: command not foundOr:
sudo: command not foundOr in a shell script:
./setup.sh: line 5: sudo: command not foundThe system cannot find the sudo binary. This is different from a permissions error like Permission denied or sudo: user is not in the sudoers file — those mean sudo was found but access was denied.
Why This Happens
sudo is not installed on every Linux system by default. Common situations where it is missing:
- Minimal Docker containers (Alpine, Debian slim, Ubuntu minimal) do not include
sudo— it is not needed since the container runs as root. - Freshly provisioned Debian/Ubuntu servers from some cloud providers or VPS hosts ship without
sudo. - Alpine Linux uses
doasinstead ofsudoin many configurations. - Shell scripts run with
su— when you switch to root withsu, the PATH may not include/usr/binwheresudolives. - Non-interactive SSH sessions — the PATH set up by
.bashrcor.bash_profilemay not apply. - Docker
RUNcommands — eachRUNis executed as a new shell with a minimal environment.
Fix 1: Install sudo
If sudo is simply not installed, install it as root:
Debian / Ubuntu:
# Switch to root first
su -
# Install sudo
apt-get update && apt-get install -y sudo
# Add your user to the sudo group
usermod -aG sudo your-username
# Exit root shell
exit
# Apply the group change (log out and back in, or use newgrp)
newgrp sudoCentOS / RHEL / Fedora:
su -
dnf install -y sudo # RHEL 8+ / Fedora
# or
yum install -y sudo # RHEL 7
usermod -aG wheel your-username
exitOn RHEL/CentOS, the wheel group has sudo access by default (not the sudo group).
Alpine Linux:
# As root
apk add --no-cache sudo
# Add user to wheel group
adduser your-username wheel
# Allow wheel group to use sudo without password (optional)
echo "%wheel ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoersAfter installing and adding your user to the appropriate group, log out and log back in for the group change to take effect. Group membership is read at login — newgrp sudo applies it to the current session without re-logging.
Fix 2: Fix sudo in Docker Containers
Docker containers often run as root, so sudo is unnecessary and not installed. But if your Dockerfile or scripts use sudo, add it:
Dockerfile — Debian/Ubuntu base:
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y \
sudo \
&& rm -rf /var/lib/apt/lists/*
# Create a non-root user with sudo access
RUN useradd -m -s /bin/bash appuser \
&& echo "appuser ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
USER appuserBetter approach — run as root during build, switch to non-root for runtime:
FROM node:20-slim
# Run build steps as root (no sudo needed)
RUN apt-get update && apt-get install -y curl \
&& rm -rf /var/lib/apt/lists/*
# Switch to non-root user for the application
RUN useradd -m -u 1001 appuser
USER appuser
WORKDIR /app
COPY --chown=appuser:appuser . .
RUN npm install
CMD ["node", "server.js"]Pro Tip: In Docker, you do not need
sudoinRUNcommands — they run as root by default (before anyUSERinstruction). Addsudoonly if your application code or scripts explicitly call it at runtime as a non-root user.
Fix 3: Fix sudo in Shell Scripts
When a script is run via su, cron, or a non-login shell, the PATH may not include /usr/bin:
Check where sudo is installed:
which sudo
# /usr/bin/sudo (most systems)
ls -la /usr/bin/sudoUse the full path in scripts:
#!/bin/bash
/usr/bin/sudo apt-get update
/usr/bin/sudo systemctl restart nginxOr fix the PATH at the top of the script:
#!/bin/bash
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
sudo apt-get updateFor cron jobs, always use full paths or set PATH explicitly because cron runs with a minimal environment:
# In crontab (crontab -e):
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
0 2 * * * /usr/bin/sudo /usr/bin/apt-get update -yFix 4: Use su Instead of sudo on Systems Without sudo
On minimal systems or older distributions where sudo is not installed and you cannot install it, use su to switch to root:
# Switch to root
su -
# Run your commands as root
apt-get update
systemctl restart nginx
# Return to your user
exitThe su - (with the dash) starts a login shell as root, which sets up the correct PATH and environment. su without the dash inherits your current environment, which may have PATH issues.
Run a single command as root with su -c:
su -c "apt-get update && apt-get install -y curl" rootThis is equivalent to sudo apt-get update && sudo apt-get install -y curl.
Fix 5: Fix sudo Not Found in SSH Commands
When running commands over SSH non-interactively, the PATH is often minimal:
Broken — PATH does not include /usr/bin:
ssh user@server "sudo systemctl restart nginx"
# bash: sudo: command not foundFixed — use the full path:
ssh user@server "/usr/bin/sudo /bin/systemctl restart nginx"Fixed — force a login shell:
ssh user@server "bash -l -c 'sudo systemctl restart nginx'"The -l flag starts a login shell which sources .bash_profile and sets up the full PATH.
Fixed — use sudo -i or sudo -s in the remote command:
If the user has passwordless sudo configured, you can also use:
ssh user@server "sudo -n systemctl restart nginx"The -n flag makes sudo non-interactive — it fails immediately if a password would be required instead of hanging.
Fix 6: Fix sudo in CI/CD Pipelines
In GitHub Actions, GitLab CI, and similar platforms, runners often run as root or a restricted user:
GitHub Actions — check the runner user:
- name: Check user
run: whoami && idMost GitHub Actions runners run as a non-root user with sudo available and passwordless. If sudo is not found, install it in your workflow:
- name: Install sudo
run: |
if ! command -v sudo &> /dev/null; then
apt-get update && apt-get install -y sudo
fiFor Docker-based CI runners, add sudo to your base image as shown in Fix 2.
GitLab CI — use before_script to install dependencies:
before_script:
- apt-get update -qq && apt-get install -y -qq sudoFix 7: Verify sudo Is Configured Correctly After Install
After installing sudo, verify it works:
# Check that sudo binary exists
which sudo
# Check your group membership
groups
id
# Test sudo with a safe command
sudo echo "sudo works"
# If prompted for password and you want passwordless sudo:
sudo visudoIn visudo, add this line at the end to allow your user passwordless sudo:
your-username ALL=(ALL) NOPASSWD: ALLOr for the entire sudo group:
%sudo ALL=(ALL) NOPASSWD: ALLWarning: Passwordless sudo is convenient but reduces security. Use it only for automated scripts, CI environments, or development machines — not production servers with sensitive data.
Still Not Working?
Check if sudo is installed but not in PATH:
find / -name "sudo" -type f 2>/dev/nullIf it finds /usr/bin/sudo but which sudo returns nothing, your PATH is missing /usr/bin. Add it:
export PATH="$PATH:/usr/bin"Add this to ~/.bashrc or ~/.bash_profile to make it permanent.
Check the sudoers file for syntax errors:
sudo visudo -c # Check syntax without opening the editorA syntax error in /etc/sudoers can break sudo for all users. If you cannot use sudo at all, boot into single-user mode or use pkexec visudo to fix the sudoers file.
WSL (Windows Subsystem for Linux): If you are on WSL and sudo is not found, your WSL distribution may be in a broken state. Try resetting it or reinstalling the distribution from the Microsoft Store.
For permission errors after sudo is working (e.g., sudo: user is not in the sudoers file), the fix is adding the user to the sudo/wheel group as shown in Fix 1. For script permission errors, see Fix: bash: Permission Denied.
Solo developer based in Japan. Every solution is cross-referenced with official documentation and tested before publishing.
Was this article helpful?
Related Articles
Fix: EMFILE Too Many Open Files / ulimit Error on Linux
How to fix EMFILE too many open files errors on Linux and Node.js — caused by low ulimit file descriptor limits, file handle leaks, and how to increase limits permanently.
Fix: Cron Job Not Running on Linux
How to fix cron jobs not running on Linux — caused by PATH issues, missing newlines, permission errors, environment variables not set, and cron daemon not running.
Fix: bash: command not found
How to fix bash command not found error caused by missing PATH, uninstalled packages, wrong shell, typos, missing aliases, and broken symlinks on Linux and macOS.
Fix: Linux OOM Killer Killing Processes (Out of Memory)
How to fix Linux OOM killer terminating processes — reading oom_kill logs, adjusting oom_score_adj, adding swap, tuning vm.overcommit, and preventing memory leaks.