sudo command not found on linux

How to Fix: “sudo command not found” on Linux (2 reasons)

You’ve spent time installing your new Linux system, you’re ready to start installing packages for your projects, and then bam: “sudo command not found”. Nothing works. It’s a real bummer, but don’t worry, you’ve come to the right place. I’ll quickly explain how to fix this error.

In general, the error “sudo command not found” happens when the sudo package is not installed on the system. This can be fixed by switching to the root user and installing the missing package.

Not sure how to do this? Don’t worry, I’ll guide you through each step to help you identify the exact problem and fix it. In some cases, it might be something else, so I’ll quickly cover that as well.

Linux doesn’t have to be intimidating. With my e-book, Master Linux Commands, you’ll uncover the secrets of the terminal in a fun, step-by-step journey. From basics to scripts, get ready to level up your Linux skills. Oh, and did I mention the handy cheat sheet you get as a bonus?

Case 1: Sudo is not installed

There are some light Linux distributions (like the minimal version of Debian) that are installed without sudo by default. The root account is enabled, but it can’t use sudo from the default user.

In this case, if you try to use sudo as a prefix for any command, you’ll get the error:
-bash: sudo: command not found

This is the most common case, so I’ll address it right away.

How to know if sudo is installed?

If you get the same error as me in my previous screenshot, it’s most likely because sudo is not installed. But there is a command you can try to confirm that is the case.

On Linux, the command “which” tells where the executable is installed. If the command returns nothing, it means the package is not installed.

So, in my example, I can try:
which sudo
I get nothing in the result, which means sudo is not installed.

If the “which” command returns something, you are probably in the second case addressed in this article, so you can skip a few paragraphs to get right to it.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

If you’re new to the Linux command line, this article will give you the most important Linux commands to know, plus a free downloadable cheat sheet to keep handy.

How to install the sudo package

The sudo package is available in the default repository, but installing it requires administrator permissions. You can only install it after switching to the root user.

So, obviously, if you try to use “sudo apt install sudo” to install it, it won’t work :-).

As a reminder, “sudo” is used to get temporary administrator permissions, but it’s still possible to log in as root or at least switch to the administrator prompt on most distributions.

To do this, type the following command and press Enter:
su

As you can see in my screenshot, it asks for the root password. I can then use apt without sudo:
apt update

Once done, you can now use your package manager command to install the missing package:
apt install sudo

That’s it, sudo is now installed, and we are almost done.

Note: If you are not using a Debian-based distribution, you need to adjust this command to use your own package manager. For example:
– Red Hat / Fedora:
dnf install sudo
– Arch:
pacman -S sudo
– SUSE:
zypper install sudo
– Gentoo:
emerge app-admin/sudo
You get the idea.

Related: How to Fix the ‘apt-get command not found’ Error on Linux

How to allow the sudo command for a specific user

You can get back to the normal user terminal with this command:
exit
It will quit the administrator prompt, and get back to your own session.

From there, you shouldn’t get the error anymore:
sudo
And the “which” command should return something:

But we aren’t done yet, as the current user won’t have permission to use “sudo”. It’s an administrative command that requires privileges to be given individually to each trusted user.

To allow your current user to run sudo commands, you need to get back to the root session, and run this command:
/usr/sbin/usermod -aG sudo pat
Make sure to replace “pat” with your user name.

Note: the command “usermod” directly might not work because /usr/sbin is not in the PATH by default on some distributions (like the latest Debian I tested). That’s why I use the full path.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

Once done, you need to exit both sessions and reconnect to get access to sudo from the normal user. Or you can reboot the computer if it’s possible for you. On your next log-in, sudo commands will work directly and you’ll no longer need to switch to “root”.

Case 2: Sudo is not in the PATH variable

This second scenario is way less common, but as experienced with “usermod” in the previous section, it might be that sudo is installed but not in the PATH.

In short, the PATH variable on Linux tells the system where to look for the command executables. The sudo command won’t work if the executable is installed in a directory that is not included in this PATH variable.

By default, you shouldn’t have to worry about this. But I have seen this problem a few times, so I’ll include it. Don’t worry, it’s a quick fix.

How to know if you have a PATH problem?

As a general rule, there is an issue with your PATH variable if the main command (“sudo”) doesn’t work, but the full path command (“/usr/bin/sudo”) works.

I reproduced this problem on my system, here is what it looks like:

The “sudo” command returns the same error as previously (“sudo: command not found”), but when we use the full path to the executable, it exists and works as expected.

It’s pretty rare, as most executables are in this directory, which means most commands on your system likely won’t work anymore, but it’s something I have seen. Let’s discuss how to fix it.

How to resolve the issue with your PATH variable?

To fix the PATH issue causing the “sudo: command not found”, you need to add the /usr/bin folder back to your PATH variable.

You can see your current PATH variable contents with:
echo $PATH

In this example, as /usr/bin (and /bin) are missing in my PATH, so the command doesn’t work.

To fix this error, I need to add the executables folder back to the PATH variable:
export PATH=$PATH:/usr/bin

After adding /usr/bin to your PATH variable, it should be working:

Warning: This is a temporary fix for your current session. If your PATH is broken after a reboot or for each new session, you most likely need to fix your system configuration file.

For example, the global configuration on Debian is in this file:
cat /etc/profile

Make sure the main executables folders are included there. But if you didn’t edit this file, it shouldn’t be an issue. You can use Nano to edit this file from the administrator session if needed. Just be careful, doing a backup is probably a good idea.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

Related questions

Are there any alternatives to using “sudo” on Linux?

The main alternative to using “sudo” is to switch to the root prompt each time you have administrative tasks to do on your Linux system. It’s a bit less convenient, but it can help to understand that you’re switching to a mode with more permissions.

Just don’t forget to get back to your normal user after that right away. Overall, it’s safer and more convenient to install and use “sudo” instead, but the choice is yours.

Is it safe to add a user to the sudoers file?

Adding a new user to the sudoers file basically makes it a system administrator, will all privileges on the computer. It’s safe if you trust the user, just make sure the user understands what that means.

How to remove access to the sudo command for a user?

Removing the user’s access to the sudo command can be done with the deluser command:
deluser <user> sudo

You need administrative privileges to do this, so you’ll need to prefix this command with sudo or switch to the root account.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

🛠 This tutorial doesn't work anymore? Report the issue here, so that I can update it!

Reminder: Remember that all the members of my community get access to this website without ads, exclusive courses and much more. You can become part of this community for as little as $5 per month & get all the benefits immediately.

Additional Resources

Overwhelmed with Linux commands?
My e-book, “Master Linux Commands”, is your essential guide to mastering the terminal. Get practical tips, real-world examples, and a bonus cheat sheet to keep by your side.
Grab your copy now.

VIP Community
If you just want to hang out with me and other Linux fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.

Need help building something with Python?
Python is a great language to get started with programming on any Linux computer.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now.

Sharing is caring!

Similar Posts