What the sudo: command not found Error Means
The error sudo: command not found (or bash: sudo: command not found) appears in a Linux terminal when you try to run a command with sudo, but the system cannot find the sudo executable file in the directories listed in the PATH environment variable.
Typical output:
$ sudo apt update
bash: sudo: command not found
This means either the sudo package is not installed on your system, or the PATH variable is configured incorrectly, and the system does not search for executables in standard directories like /usr/bin or /bin.
Common Causes
- The
sudopackage is not installed. Some minimal or specialized Linux installations (e.g., certain containers, Docker images, or systems where the root user is used by default) may not includesudoin the base package set. - Incorrect
PATHvariable. ThePATHvariable defines the list of directories where the system looks for executable commands. If the standard directory/usr/bin(wheresudousually resides) has been removed fromPATHorPATHhas been overridden incorrectly (e.g., in~/.bashrcor~/.profile), the command will not be found. - Corruption or deletion of the
sudobinary file. The file/usr/bin/sudomight have been accidentally deleted or corrupted due to a system update failure or manual intervention.
Solutions
Solution 1: Check and Fix the PATH Variable (Most Common Case)
This is the first thing to check. It's possible sudo is installed, but the system "doesn't see" it.
- Check your current
PATH. Run:echo $PATH
Note the list of directories separated by colons. The standard path forsudois/usr/bin. Ensure this directory appears in the output. Example of a correctPATH:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games - Temporarily add
/usr/bintoPATHfor the current terminal session to immediately test if this resolves the issue:export PATH=$PATH:/usr/bin
After this, try thesudocommand again. If it works, the problem is definitely withPATH. - Restore
PATHpermanently. Open your shell's configuration file. Forbash, this is typically~/.bashrcor~/.profile.nano ~/.bashrc
Find the line starting withPATH=. If it doesn't exist, add this to the end of the file:export PATH=$PATH:/usr/bin
Important: Do not overwritePATHcompletely (e.g.,PATH=/usr/bin), but append to the existing value using$PATH:, or you will lose all other paths. - Apply the changes. Save the file (
Ctrl+O,Enter,Ctrl+Xinnano) and run:source ~/.bashrc
Thesudocommand should now work in the current terminal.
Solution 2: Install the sudo Package (If It's Missing)
If sudo is genuinely not installed, you need to install it. Since installing packages usually requires root privileges and you don't have sudo, you can:
- Log in as root (if you know the root password) using the
sucommand:su -
Enter the root password. You will then get a#prompt. - Install
sudousing your distribution's package manager:- For Debian/Ubuntu and derivatives:
apt update apt install sudo - For RHEL/CentOS/Rocky/AlmaLinux:
yum install sudo # or for newer versions dnf install sudo - For Arch Linux:
pacman -S sudo
- For Debian/Ubuntu and derivatives:
- Configure access for your user (if you are not already in the
sudoorwheelgroup):- In Debian/Ubuntu, add the user to the
sudogroup:usermod -aG sudo your_username - In RHEL/CentOS, add the user to the
wheelgroup:usermod -aG wheel your_username
Replaceyour_usernamewith your actual username. - In Debian/Ubuntu, add the user to the
- Log out of the root session (
exit) and try usingsudoagain. You may need to log out of your system completely and log back in for the group changes to take effect.
Solution 3: Restore a Corrupted or Deleted Binary
If the file /usr/bin/sudo is missing or has incorrect permissions, the command will fail even with a correct PATH and an installed package.
- Check for the file and its permissions:
ls -l /usr/bin/sudo
Expected output (permissions may vary slightly):-rwsr-xr-x 1 root root 146736 Jan 15 2024 /usr/bin/sudo
Note thesin the permissions (rws). This is the special SETUID bit, which allows the program to run as the owner (root). Its absence can cause issues, but not a "command not found" error. - If the file is missing, reinstall the
sudopackage as described in Solution 2. This is the most reliable way to restore the original binary. - If permissions are incorrect (e.g., no
sor owner is notroot), fix them after reinstalling:chmod 4755 /usr/bin/sudo chown root:root /usr/bin/sudo
Solution 4: Temporary Workaround — Use su
If you cannot fix sudo quickly but need to execute commands as root, use su (switch user).
- Switch to the root user:
su -
Enter the root password. - Execute the necessary commands. Remember that working as root can accidentally damage your system. Try to fix
sudoas soon as possible. - Return to your user: run
exit.
⚠️ Important: This is not a solution, just a temporary workaround. Working as root constantly is not secure.
Prevention
- Do not remove system directories (
/usr/bin,/bin,/sbin) from thePATHvariable manually. If you need to add your own directory, append it using:. - When editing
~/.bashrcor~/.profilebe careful with theexport PATH=...syntax. It's best to use the patternexport PATH=$PATH:/your/new/path. - Check compatibility of scripts and programs that might modify
PATH(e.g., Python version managers likepyenvor Node.jsnvm). Ensure they do not overwritePATHcompletely. - Regularly update your system (
sudo apt update && sudo apt upgradeor similar) to avoid package corruption. - When using Docker or chroot environments, explicitly install
sudoif you plan to use it, as minimal images often do not include it.