How to Fix the “sudo: command not found” Error on Linux
Linux is an open-source operating system known for its flexibility and security, making it one of the most preferred operating systems in the world. When working with Linux, you may come across errors that need fixing to continue using this operating system optimally. One such issue is the “sudo: command not found” error. The sudo command is fundamental as it allows you to run administrative actions safely.
In this article, we’ll show you how to fix the “sudo: command not found” error on Linux.
What Causes the “sudo: command not found” Error?
The “sudo: command not found” error may occur due to several reasons, including:
- The sudo package is not installed: This issue primarily occurs when you’re trying to run sudo for administrative tasks, but the sudo package is not installed on your system.
- The sudo command is not present in the PATH variable: When you run commands in Linux, the terminal uses a PATH variable to search for available system commands. If you’re using a non-standard distribution, the PATH variable may not include the folder that contains the sudo command.
- The sudo command is not accessible: When the sudo binary is not executable, you’ll receive the “sudo: command not found” error.
Now that you understand what causes the error, let’s take a look at how to fix it.
Fixing the “sudo: command not found” Error1.
1.Install the sudo Package
If you’re unsure whether the sudo package is installed on your Linux distribution, you can install it by running the command below:
Debian/Ubuntu:
sudo apt-get install sudo
Fedora:
sudo dnf install sudo
CentOS/RHEL:
sudo yum install sudo
Once you’ve installed the sudo package, test it by running the command below:
sudo echo “Hello World”
If the terminal prompts you for the root password and “Hello World” is printed on the terminal output, then the sudo command is now working correctly.
2.Add the sudo Folder to the PATH Variable
If the sudo command is not in the PATH variable, you’ll have to add the folder manually.
To check the PATH variable’s contents, run the command below:
echo $PATH
If the output doesn’t have the /usr/bin/sudo folder, add it to the PATH variable by modifying the /etc/environment file. Open the file in your preferred text editor and append the following line at the end of the file:
/usr/bin/sudo
Save and exit the file and reload it by running the command below:
source /etc/environment
Test the sudo command using the process shown above.
3.Fix Sudo Binary Permissions
Sometimes, the “sudo: command not found” error may occur due to incorrect permissions set on the sudo binary. To fix it, follow the steps below:
Step 1: Locate the sudo binary file by running the command below:
whereis sudo
The output should be similar to the one below:
sudo: /usr/bin/sudo /usr/lib/sudo /usr/share/man/man8/sudo.8.gz
Step 2: After locating the sudo binary file, check the file permissions by running the command below:
ls -lah /usr/bin/sudo
Step 3: If the output shows that the sudo binary file is not set to executable, you can fix the permissions by changing them using the chmod command. Run the command below:
sudo chmod 4755 /usr/bin/sudo
The above command changes the file’s permissions, allowing the system to execute it correctly.