How to Fix Linux sudo: command not found Error
In this tutorial, you'll learn about How to Fix Linux sudo: command not found Error. We cover key concepts, practical examples, and best practices.
You type sudo and get command not found — sudo is not installed on the system, or the PATH does not include /usr/bin or /usr/local/bin.
The Problem
bash: sudo: command not found
Step-by-Step Fix
Step 1: Check if sudo is installed
which sudo
Or:
ls /usr/bin/sudo
Step 2: Install sudo as root
Switch to root:
su -
Then install sudo:
Debian/Ubuntu:
apt update && apt install sudo
RHEL/Fedora:
dnf install sudo
Alpine:
apk add sudo
Step 3: Add user to sudoers
As root:
usermod -aG sudo username
Or on RHEL-based:
usermod -aG wheel username
Step 4: Verify sudo group membership
groups username
Step 5: Ensure /usr/bin is in PATH
export PATH=$PATH:/usr/bin:/usr/sbin
Add to ~/.bashrc:
echo 'export PATH=$PATH:/usr/bin:/usr/sbin' >> ~/.bashrc
Step 6: Test sudo
sudo whoami
Expected:
root
Step 7: Configure sudo without password (optional)
visudo
Add:
username ALL=(ALL) NOPASSWD: ALL
Prevention Tips
- Install sudo during OS installation
- Use
su -as fallback when sudo is unavailable - Add users to the sudo/wheel group after account creation
- Include sudo in base Docker images
- Do not modify sudoers file directly — use
visudo
Common Mistakes with sudo command
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
These mistakes appear frequently in real-world LINUX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro