How to fix “sudo command not found” on Linux

In Linux, the most used keyword is the sudo keyword. Sudo represents the superuser in the Linux system which means that this user gains authority to perform various changes in the system. It allows users to run commands as another user, usually the superuser or root. While utilizing the sudo keyword to perform various tasks on your system, you may encounter the “sudo command not found” problem.

This article will act as a guide for you to learn the reasoning behind this error and also be able to fix it.

  1. What are the Possible Reasons “sudo command not found” Error on Linux? 
  2. How to Fix/Resolve the “sudo command not found” Error in Linux?
  3. Reason 1: Missing/Not Installed/Corrupted sudo Package 
  4. Solution: Install sudo Command
  5. Reason 2: Insufficient Privileges
  6. Solution: Add the User to the sudo Group 
  7. Reason 3: Overwritten or Deleted Alias or Function for sudo
  8. Solution: Remove or Restore the Alias or Function for sudo
  9. Reason 4: Modified/Missing PATH Environment Variable
  10. Solution: Add /usr/bin to the PATH Variable 
  11. Tip 1: Fix “sudo: unable to resolve host” Error in Linux
  12. Tip 2: Fix the “sudo: add-apt-repository: command not found” Error in Linux
  13. Tip 3: Fix: the “sudo apt-get command not found” Error in Linux

What are the Possible Reasons “sudo command not found” Error on Linux? 

This error occurs when the sudo program is not installed or not in the user’s path. Some possible reasons for this error are: corrupted or not installed sudo command, modified the PATH environment variable and removed the directory where sudo is located. Moreover, the user has a different shell that does not recognize sudo as a command and typo or a wrong syntax when typing sudo.

How to Fix/Resolve the “sudo command not found” Error in Linux?

To fix this error, try to reinstall the sudo package using the package manager of their Linux distribution, or manually download and install the sudo binary file from the official website. Alternatively, check and modify their PATH variable to include the directory where the sudo file is located, or switch to a different shell that allows sudo access.

There is quite a simple reasoning behind this error. In the below sections, the reasons will be elaborated on in detail and various solutions will be provided as well.

Reason 1: Missing/Not Installed/Corrupted sudo Package 

When Linux is installed onto your system, the sudo command is installed by default as well. One of the most common reasons for causing this error is that the sudo package is not installed or corrupted in Linux. 

It occurs when the user tries to execute a command with administrative privileges, but the system cannot locate the sudo binary file. In most cases, the system administrator did not install sudo, or the installation was interrupted or damaged by some external factor. 

Sometimes, the sudo tool might not exist on your system or may get removed. When this happens and you try to run any sudo command, the error is prompted as shown below:

Let’s fix this “sudo command not found” error.

Solution: Install sudo Command

Installing the sudo command onto your system is the most suitable and easiest method to resolve the “sudo command not found” error. For this, logout from the existing user and switch to the root/super user. Then, install or reinstall the sudo package using the appropriate command for your Linux distribution, such as apt, yum, Pacman, etc. 

Let follow the below step by step procedure:

Step 1: Enter Root User Mode

Firstly, you need to enter root user mode. Run this statement in the terminal to enter root user mode:

su -

A sample snippet for the root user is shown below:

Step 2: Update System Repository

Now, utilize the below command to update the system’s repository:

apt update

Step 3: Install sudo Command 

Once the repository is updated, use the following command to install the sudo command on the system. Choose the sudo command for installation according to your Linux distribution:

apt install sudo                  # Debian and All its Derivatives
pacman -S sudo                    # Arch Linux 
yum install sudo                  # CentOS/RedHat
dnf install sudo                  # Fedora and its Derivatives
brew install sudo                 # macOS

The snippet below shows the successful installation of the sudo command:

Verification

Once installed the sudo command, exit the root user by using the exit keyword then try the sudo command again. Let us update the system repository with the “sudo” privileges. This time you will not get the error as shown in the below image:

Finally, this method has resolved the “sudo command not found” issue in Linux.

Reason 2: Insufficient Privileges

Another reason is that the user is not in the sudoers file or has insufficient privileges. The sudoers file is a configuration file that specifies which users can run commands as root or other users using sudo. If the user is not listed in the sudoers file or does not have the required permissions, they will not be able to use sudo. It means the user is not a member of the sudo group, which grants permission to execute commands as the superuser. 

Solution: Add the User to the sudo Group 

To fix this error, you need to add the user to the sudo group or line, such as wheel, admin, etc. Let us follow the below steps:

Step 1: Log in as the Root User 

Log in as the root user to perform some modification for the addition of the “user” to the “sudo” group:

su -

Step 2: Add user to the sudo Group

Now, run the “usermod -aG sudo username” command by replacing “username” with the actual name of the user to add to the sudo group. In our case, the existing username is “itslinuxfoss”:

usermod -aG sudo itslinuxfoss

Step 3: Confirm Added to the sudo Group

Log out and log back in to confirm the added user to the sudo group. For this, execute the “groups username” command by replacing the added username: 

groups itslinuxfoss

Step 4: Reinstall sudo Command

Now, reinstall the sudo command in the root user to fix all dependencies via the below command:

apt install --reinstall sudo

After reinstalling the sudo package, you may need to log out for the changes to take effect.

Step 5: Verify Full sudo Access

Verify that you can run sudo commands by typing the “sudo -l” command. It displays a message like “User username may run the following commands on this host: (ALL: ALL) ALL”, indicating that you have full sudo access:

sudo -l

Finally, the error has been resolved on Linux and runs commands as the superuser when needed.

Reason 3: Overwritten or Deleted Alias or Function for sudo

Another reason for causing this error is that the alias or function for sudo is overwritten or deleted. An alias or a function creates a shortcut or a custom command for a longer or more complex command. Sometimes, an alias or a function for sudo may be overwritten or deleted by mistake or by malicious code, causing the original sudo command to be unavailable.

Solution: Remove or Restore the Alias or Function for sudo

To resolve this error, users need to remove or restore the alias or function for sudo using the “unalias” or “unset” command or editing the .bashrc file.

You can use the unalias or unset command to remove the alias or function, depending on how sudo was defined. 

Remove the Alias for sudo

To remove the alias, use the “unalias” command with the sudo. If sudo was defined as an alias, run the below script:

unalias sudo

Remove the Function for sudo

If sudo was defined as a function, users can run the below script by using the “unset” command with the “f” flag for forcefully:

unset -f sudo

Restore the Alias or Function

To restore the alias or function, find out where it was originally defined and source that file again. For instance, if sudo was defined in the “.bashrc” file, you can run:

source ~/.bashrc

Reinstall/Install sudo Command

To reinstall/install the “sudo” command, use the apt package manager for all Debian-based systems as below:

apt install --reinstall sudo

Verification

After reinstalling the sudo package, users may need to log out and execute the “sudo” command for the changes to take effect. Let us update the system repositories via the below command:

sudo apt update

That is all from this solution to fix errors.

Let us dig into another reason and solution to tackle the error.

Reason 4: Modified/Missing PATH Environment Variable

The PATH environment variable does not contain the directory where sudo is located. The PATH environment variable is a list of directories that the shell searches for executable files when a command is entered. If the PATH does not contain the directory where sudo is located, usually “/usr/bin”, the shell will not be able to find and execute the sudo command. 

Let us see the screenshot of the error: 

This means that the shell cannot find the sudo executable file in the PATH variable. 

Solution: Add /usr/bin to the PATH Variable 

To fix this, users need to add /usr/bin to the “PATH” variable using the export command or editing the .bashrc file. The step-by-step instruction is given below:

Step 1: See the Current Value of the PATH Variable

Open a terminal and type the “echo $PATH” command to see the current value of the PATH variable:

echo $PATH

Step 2: Add PATH Variable

To add /usr/bin to the PATH variable, edit the file ~/.bashrc, which is a configuration file for the bash shell. For example, use the nano editor and type the below command:

nano ~/.bashrc

Add this line at the end of the file. This will prepend /usr/bin to the existing PATH variable so that it will be searched first:

export PATH=/usr/bin:$PATH

Save and close the file via the “CTRL+C” and “CTRL+X” keys. 

Step 3: Reload Source File

To apply the changes, you need to either restart the terminal or source the file by typing this command:

source ~/.bashrc

Step 4: Reinstall sudo Command

To reinstall the “sudo” command, use the apt package manager for all Debian-based systems as below:

apt install --reinstall sudo

Verification

After reinstalling the sudo package, log out via the “exit” command and execute any “sudo” command for the changes to take effect:

That is all from the guide to fix “sudo command not found” in Linux.

Tip 1: Fix “sudo: unable to resolve host” Error in Linux

One cause of the error sudo: unable to resolve host in Linux is that the hostname is not correctly configured in the /etc/hosts file. To fix this, users can edit the file with a text editor and ensure that the hostname matches the one in the /etc/hostname file.

To fix the error, check out the step-by-step instructions in the “sudo: unable to resolve host” guide.

Tip 2: Fix the “sudo: add-apt-repository: command not found” Error in Linux

One of the best solutions to fix the error in Linux is to install the “software-properties-common” package, which contains the “add-apt-repository” command. For further details, go to this guide.

Tip 3: Fix: “sudo apt-get command not found” Error in Linux

It means that the apt-get package manager is not installed on your system. This can happen if you are using a Linux distribution that does not use apt-get, such as Arch Linux, Fedora, or Gentoo. To fix this error, install the appropriate package manager for your distribution, or use a different command to install software. 

To read possible solutions, check out this guide “Fix: sudo apt-get command not found”.

Conclusion 

The error “sudo command not found” arises when the system does not have the sudo utility/package/command installed on it. This can happen due to several reasons, such as a corrupted or missing sudo package, a misconfigured PATH environment variable, or restricted shell access. This “sudo command not found” error can be fixed by going into the root user mode and then installing the sudo tool using the apt repository. 

This article has provided the knowledge that is required to understand the origin of this error as well as its solutions.