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

Spread the love

Sometimes, while performing administrative tasks like setting up a new Linux desktop for a virtual machine or modifying system files, you might encounter an error message saying sudo: command not found. This is a common and frustrating issue for new Linux users who are just starting out.

This article shows you what this error means and how to fix it in Linux using different methods.

Note: This tutorial used Ubuntu 24.04 LTS as an example, but the solutions provided are applicable to other Linux distributions as well.

Content

What Is the Sudo Command?

Let’s first understand what sudo is all about. The sudo command, short for superuser do, lets you do tasks that need special superuser (or root) privileges.

In Linux, regular user accounts have limited permissions to prevent accidental system damage. You can’t access certain files or perform critical tasks. On the other hand, the root user has no such limits and can do anything on the system.

By using sudo before a command, you temporarily elevate your privileges. This allows you to execute commands that would otherwise be restricted. For instance, if you need to install software or modify system files, sudo gives you the necessary permissions.

Using sudo is a better option than logging in as the root user because it reduces the risk of accidentally breaking the system. When you run a command with sudo, it asks for your user password (not the root password) to verify your identity. This provides an extra layer of security, where only allowed users can execute sensitive operations.

Additionally, there is also a way in which you can use sudo without a password.

What “Sudo: Command Not Found” Means

The sudo: command not found error means that your Linux system doesn’t have the sudo package available. Most Linux systems come with sudo pre-installed, but a few don’t, such as Arch and Gentoo Linux.

However, sometimes you still see the error even though your system has sudo installed. In that case, it might be because your PATH variable doesn’t contain the directory where sudo is installed. The PATH variable helps the system locate commands by searching through listed directories.

Lastly, you might see this error if your user lacks the right permissions or if the PATH isn’t set up properly. Furthermore, make sure that you have spelled sudo correctly.

Tip: You might also want to try Run0, a new privilege escalation program that serves as a great
alternative to sudo, and also learn
how to use Run0 in Linux.

How to Fix Sudo: Command Not Found Error

To fix the sudo: command not found error in Linux, you need to install sudo, add the user to the sudo group, or add the sudo executable path to the PATH variable.

Installing Sudo

To begin, let’s first check if your system has a sudo package available or not. You can easily check by querying its version number. If it shows up, then you know that sudo is installed in your system:

sudo --version

If your Linux system does not have sudo installed, you’ll need to install it to resolve the error. For this, you need to log out of your user account and log in as a root user.

To do that, simply type this command:

su -

Now, after becoming a root user, you can install the sudo package in your Linux distribution using your default package manager.

For example, you can install sudo in Ubuntu/Debian-based distributions using apt:

apt install sudo

To install sudo in CentOS/Fedora or RHEL-based distro, run this:

dnf install sudo

Use the following command to install sudo package on Arch Linux:

pacman -S sudo

Adding User to Sudo Group

After installing sudo, add your non-root user to the sudo group to enable superuser root access. For example, you can add a user to the sudo group in Ubuntu or Debian system by using this command:

usermod -aG sudo username

On Fedora/CentOS or other RHEL-based distros, try adding a user to the wheel group:

usermod -aG wheel username

Furthermore, you can verify whether the user added to the group or not by executing the below command:

groups username

After checking, switch back to the non-root user from the root using the su command followed by your username:

su username

That’s it! Now, you can easily execute all the commands as a superuser using sudo. For example, you can verify the sudo installation by updating the packages of your Linux system with this command:

sudo apt update

Add Sudo’s Directory to the Path Variable

If you have sudo installed on your system but continue to get a sudo: command not found error, then check your PATH variable and make sure it includes the directory where sudo is installed.

First, determine the location of your sudo command by returning its executable path:

which sudo

Next, use the echo command to check if your PATH variable includes this directory (/usr/bin/):

echo $PATH

Here, you see a list of many directories separated by colons. You need to look for “/usr/bin” or “/bin” in the list because they are the most common locations for sudo.

If you don’t have “/usr/bin” directory in your PATH, you can include it with this command:

export PATH=$PATH:/usr/bin

Moreover, replace “/usr/bin” with the actual directory that contains the sudo executable.

Running this command temporarily updates the PATH variable, allowing your system to find the sudo directory and resolve the bash errors in Linux. However, this modification is temporary and will disappear when you end the terminal session.

To make this change permanent for all users, you need to add the sudo directory to the system-wide PATH variable. To do that, let’s first become the root user:

su -

Open the “/etc/profile” file:

nano /etc/profile

Search for the variable path line and add your “/usr/bin” directory to the PATH variable.

Finally, save the file by pressing Ctrl + O and close the editor. Now, you can work with the sudo command without any errors.

Wrapping Up

You have successfully fixed the sudo: command not found error by installing sudo, adding your user to the sudo group, and including the sudo executable path in the PATH variable.

Furthermore, you can also explore the differences between sudo -s, su, sudo su, and sudo -i.

Image credit: Haroon Javed

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe


Haroon Javed
Contributor

Haroon is a lifelong tech enthusiast with over five years of experience writing thousands of articles about Linux, programming languages, and more. He loves exploring new technologies and experimenting with them to find innovative ways to use them. Haroon’s work has been featured on various online platforms, including HTG, Baeldung, and LinuxHint.

Leave a comment