How to Fix “sudo: command not found” Error on Linux?


In Linux and other Unix-based operating systems, “sudo” stands for “Superuser Do,” and it is a command that allows users to execute commands with the privileges of the superuser or the root user.

In some Linux systems, you might face the issue “sudo: command not found.” “sudo is not installed” or the “path environment variable is not set” are the basic reasons for this error.

This post will address the reasons that cause the “sudo: command not found error” and the solutions to rectify the error. As a reference, the Ubuntu 22.04 system will be used for practical demonstration.

Why Does the “sudo: command not found” Error Occurs?

Several reasons invoke the error; let’s have a look at this one by one:

Sudo Tool is not Installed

You will get this error if you try to run the sudo command without the sudo utility. This is usually foreseen in the older Linux systems or systems not equipped with the sudo tool.

Path Variable Not Set

If the path environment variable is not set, your system cannot locate the error, failing to execute the sudo command.

Environment Not Permitted

If you are working in a restricted environment, such as a locked-down system or a container, it’s possible that the “sudo” command is intentionally restricted or not available due to security or administrative policies.

Another reason is that you are misspelling the word sudo, which is easy to fix by rechecking the spellings of the sudo keyword. The error on the screen looks like as shown below:

sudo command not found error

Let’s dig into the possible solutions one by one.

Solution 1: Install Sudo Utility

The primary and most common reason is that the sudo is not installed on your system. Let’s see how it can be installed:

Step 1: Check the Availability of the sudo

Before getting into the installation phase, first check whether sudo is installed on your system or not by using the command:

$ which sudo

which sudo

As can be seen in the output that there is no directory pointing toward the sudo, which means that sudo is not installed.

Step 2: Install sudo

First, you need to switch to the root user using the command:

$ su -

switching user

After that, use one of the commands as per your Linux distribution to install the sudo utility on your systems:

Debian and All its Derivatives

# apt install sudo

installing sudo utility

Arch and All its Derivatives

# pacman -S sudo

CentOS/RedHat and Their Derivatives

# yum install sudo

Fedora and its Derivatives

# dnf install sudo

Note: If you want to get the sudo utility on the macOS, use the Homebrew manager:

$ brew install sudo

Step 3: Verify the sudo Command

Once the sudo utility is installed, switch back to the normal user from the root user (using the exit keyword) and then use the sudo command as we did here:

# exit
$ sudo apt update

check sudo command not found error

The command will be executed, as seen in the above output.

Solution 2: Set/Update the Path Variable

If the first solution has not worked for you, you must add/update the path variable for the sudo executable. If you haven’t yet, perform the steps listed below:

Step 1: Open the .bashrc File

Use any editor (as a root user), i.e., nano, vim, to open the .bashrc profile:

# nano .bashrc

Step 2: Add the Path Variable

Scroll down in the file and add the following line (the /usr/bin is the directory containing the sudo executable):

export PATH="/usr/bin:$PATH"

editing bashrc file
Note: Usually, the directory containing the sudo executable is “/usr/bin.” It may be different in your case; if it is, ensure to replace it with the “/usr/bin” in the above command.

Now, reload the current shell’s environment via the following command:

$ source .bashrc

sourcing the bashrc File

Now, the issue will be solved. If you are still facing it, contact your Administrator, as you may have been granted limited access.

Wrap Up

The “sudo: command not found” error in Linux (Other Unix-based systems, i.e., macOS) can occur for several reasons, such as the sudo utility not being installed or the path environment variable not being set.

To fix this, you must install the sudo utility using the command per your Linux distribution. Moreover, set/update the path variable to redirect the path to the directory which contains the sudo executable. All these solutions are practically demonstrated in this guide.
Keep visiting the Linux Genie for more troubleshooting guides.

Print Friendly, PDF & Email
Categories