Situatie
Tried using your first Linux command as a superuser and getting the “sudo: command not found” error? This is one of the most common problems new Linux users face after a fresh installation.
Solutie
The sudo command on Linux, a contraction of the phrase “superuser do”, is a powerful utility that allows users to execute commands with the privileges of another user, typically root. With sudo, authorized users can perform administrative tasks without needing to log in as the root user.
The way to use sudo is to begin another command with the word “sudo”. This allows you to run the command with elevated privileges. So the sudo command lets you have the required permissions of a different user and run a sensitive command. You can learn more about the sudo command from its manual page.
Why Are You Getting This Error?
There are usually two possible reasons for the “sudo: command not found” error on Linux. It’s either because sudo is not installed or the sudo directory is not present in your system’s PATH variable.
Some Linux distributions may not have the sudo package installed by default. This is true for Arch and Gentoo Linux. On Debian, if you set a root password during installation, then you’ll face the same problem.
The second reason is that sudo is installed, but its directory is not included in the PATH variable. PATH is an environment variable the Linux system uses to locate a command without specifying its full path.
When you type a command in the terminal, the system searches through the directories listed in the PATH variable and tries to find the correct executable file. If the sudo command is not in that PATH variable, the system will not find it and won’t be able to use it.
Here’s How to Fix the “sudo: command not found” Error
If you don’t have sudo installed, installing it and adding your user to the sudo/wheel group should be enough to fix the problem. To ensure whether you have sudo installed, run:
sudo -V
If you don’t have sudo installed, you should see a similar output, in which case proceed to the following method.
Install sudo on Linux
To install the sudo package, you need to become root first. To become a root user, open your terminal and run:
su -
Type in the root password and press Enter. If successful, you should see the shell prompt become white and the word “root” written at the beginning of the prompt.
As a root user, you can install any software package, including sudo. To install sudo, run this command on Debian-based distros:
apt install sudo
For RHEL-based systems, use this command:
yum install sudo
On Arch Linux, run:
pacman -S sudo
If you’re using Gentoo, then use this command:
emerge --ask app-admin/sudo
Add your user to the sudo Group
Once you’ve installed the sudo command, you need to add your non-root user to the sudo group. This is because you want to give that user superuser privileges. Then your non-root user can use the sudo command to gain elevated privileges.
To add a user to the sudo group, run this command on a Debian-based distro:
usermod -aG sudo username
Make sure to replace “username” in the command with the correct username.
If you’re using Arch Linux, Fedora, or other RHEL-based distros, then use the following command:
usermod -aG wheel username
Finally, switch back to the non-root user with:
su username
Now you can try a command beginning with sudo to test whether it’s working. For example, let’s try to update the packages on the system. To do that, use:
sudo apt update
It works as expected.
Try Adding sudo to the PATH Variable
If you already have sudo installed, but it’s still not working, then you need to add sudo to the PATH variable. Let’s first determine the location of the sudo command.
which sudo
As you can see, it’s in the “/usr/bin/” directory. Now let’s check if the directory is in the PATH variable or not. Run this command:
echo $PATH
In my case, the location of the sudo command is present in the PATH variable. In case it’s not, you can add it with this command:
export PATH=$PATH:/usr/bin
However, this will only add the sudo command’s directory temporarily. After you end the terminal session, things will go back to what they were.
To permanently add the directory, open the “/etc/profile” file in a text editor. To open it in the nano text editor, first become root by entering su – and use this command:
nano /etc/profile
Edit the line that declares the PATH variable. Add a colon (:) and then append the path to sudo (“/usr/bin” in this case.)
Save the file using Ctrl+O and then exit from the editor using Ctrl+X. Now try to use the sudo command to see if it’s working.
That should solve your problem and let you use the sudo command without any errors. Remember that, only the users added to the sudo group will be able to use it. If you want to grant sudo privileges to more users, check out our guide on controlling sudo access on Linux.
Leave A Comment?