How to fix "sudo command not found error" with examples [Update 2022]

There are many scenarios when you get "sudo command not found error" and you keep looking for solutions on the internet to fix it. One of the common examples, when you have installed Debian with a minimal package and trying to run sudo. For me whenever I try to ssh into any of the remote servers and run sudo, I get this "bash: sudo commands not found error". So I decided to write a post, which covers all the scenarios of this famous error.

Below mentioned errors will be covered in this post. so if you are getting any of the following errors, this post will definitely solve your error -

  • sudo command not found mac
  • sudo command not found ubuntu
  • sudo command not found debian
  • zsh sudo command not found
  • sudo command not found windows
  • sudo command not found centos
  • bash: /usr/bin/sudo: No such file or directory

These are the common scenarios that result in this error -

  • You have installed a linux distribution with minimal package like Debian
  • Sudo is not installed
  • Path is not setup for sudo command
  • You are trying to ssh a remote server first time and sudo is not working

Before getting into the solutions, if you are a newbie let's understand why sudo is used?

Why sudo is used in Linux before every command?

Sudo is one of the famous prefixes to any command you run in the Linux world. It allows running any Linux command with elevated privileges to run administrative tasks. Also, the operations which are only permitted by a root user can be done using a normal user with sudo rights. The "sudoers" file controls, who can use the sudo command to gain elevated access and location is /etc/sudoers in all Linux distributions.

How to fix sudo command not found error in Debian/Ubuntu like Distros

As we discussed already, there may be many reasons to encounter this error. As you will see in the image, running the "sudo ifconfig -a" command results in "bash: /usr/bin/sudo: No such file or directory error". I am running this command in Ubuntu 21.04 installed as VM.

bash sudo command not found error in Ubuntu

Let's deep dive and find the fixes for different scenarios -

Solution-1 Check and install sudo package

This is very uncommon when sudo is not installed by default with Linux installation. Still, you may get this situation when you have installed the Debian Linux with the minimal package. Follow these steps to fix it -

Step1.1 List installed packages in Ubuntu and look for the "sudo" package

$ apt list --installed | grep -i sudo
check installed packaged list for sudo

If you can find package name sudo with "installed" status as shown in the image. Then you can directly move to solution-2 to fix the "sudo: command not found error".

If you don't have the sudo package installed, run the following command to install it in Ubuntu or Debian based distros. You would need root access to achieve this.

Step1.2 Switch to root user

$ su root

Enter "root" password, when prompted.

If you don't have a root password, then follow this link to reset your root password.

Step1.3 Run apt command to install sudo package in Ubuntu/Debian

# apt install sudo

Refer to the following image for reference.

install sudo to fix sudo command not found in ubuntu

Step1.4 Give sudo rights to your own user by adding it to the sudo group

# usermod -aG sudo yourusername

For example, I will add my user "dev" to sudo group -

# usermode -aG sudo dev
add user to sudo group

Step1.5 Verify your user is added to the sudo group

Open /etc/passwd file and check whether your user is added to the sudo group or not.

# cat /etc/group | grep -i username

I will check for my user "dev" in this example.

 # more /etc/group | grep -i username 
check user has been added to sudo group

Step1.6 Make sure your sudoers file have sudo group added

  • Run visudo command to modify sudoers file and add the following line into it (if it is missing):
# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL
Enable sudo group to execute any command in sudoers file

Press Ctrl + X and press "Y" to save the file and exit from the nano editor.

Exit from the root shell and check the sudo command functionality.

  • Run sudo -h command. Now you will not get any "sudo command not found error" as it's fixed now.
sudo commands help

Solution2 - Setup path variable to fix sudo command not found errors

If you have a sudo package installed as explained in Step 1.1 and still you are getting sudo command not found error. It means you don't have "/usr/bin/" setup in the PATH variable or your /etc/environment file got messed up.

To add sudo in your Path variable, add /usr/bin/ directory by following the method.

Step2.1 Switch to root user

$ su root

Enter root user password, when asked.

Step2.2 Run /usr/bin/sudo command to check whether it's working or not

# /usr/bin/sudo -h

Step2.3 Edit /etc/environment file and add "/usr/bin" directory in PATH

# vi /etc/environment
Add path variable to fix sudo command not found Debian

Alternatively, you can also run export command to set up the path variable

export PATH=$PATH:/usr/bin

In case you are getting visudo and usermod command not found errors also. Make sure you add /usr/sbin in the PATH variable as shown in the last step.

Fix "sudo command not found error" in Fedora/AlmaLinux/Rocky Linux Distros

This section covers sudo error for all the Linux distros based on RedHat like Fedora, AlmaLinux, CentOS and Rocky Linux. I have Rocky Linux Installed as a virtual machine. The procedure is the same as explained earlier, Look for the package, install it and set the path variable, if required. But the commands will differ now.

Solution3- Install sudo and add user to "Wheel" group to fix sudo errors

You have the option to set up sudo during installation in RedHat based Linux distros. But if you forgot to do that, You must have access to the root user to set up sudo for your user.

Step3.1 - Login with root user and check for sudo package installation

Follow this link to reset your root password, in case you forgot or don't have it.

Once you are done with login as root, open Terminal and type

# yum list installed | grep -i sudo

If you get the sudo package installed already skip to the next step3.3.

Step3.2 Install sudo package

You can use the "yum" or "dnf" command to install any package from the repository.

Update all old packages and refresh the repository.

# dnf update 

Then install the sudo package using the dnf or yum command.

# yum install sudo

or
 
# dnf install sudo
Install sudo to fix sudo command not found in Centos or Rocky Linux

Step3.3 Add user to the wheel group

The wheel group is already set to provide sudo access to run all commands. So is the quickest way is to add your user to this group. Switch to the root user first using su command

su -

Now run usermod command to add your user in the wheel group.

# usermod -a -G wheel "username"

For example, I will add my user "dev" as an example

 # usermod -a -G wheel "dev" 
Add user to wheel group to fix zsh sudo command not found error in Fedora

Step4.4 Check with the id command

Next, run the id command to see whether your user is part of the wheel group or not.

# id "username" 

Example: My user dev is part of 10(wheel) the group.

# id dev
check Id has been added to wheel group

Congrats! you must have fixed your "sudo command not found error " now and you can run all your privileged commands using sudo.

Frequently asked questions on sudo

Q1: Why is sudo not working?

These are many reasons when sudo may not work -

1. You don't have a sudo package installed
2. If the sudo package is installed then the path variable is not set up correctly
3. Your system got broken and all your packages and permissions got messed up
4. Your user is not added to the sudo or wheel group based on your Linux distro.

Q2: How do I enable sudo?

If the sudo package is already installed based on your Linux distro run the following command. Replace "dev" with your username.

for Ubuntu and Debian based distros -

$ su root

Then run the usermod command to add your user to the sudo group

# usermode -aG sudo dev

For RedHat based distros -

Switch to root (superuser)

$ su -

Add to the wheel group.

# usermod -a -G wheel dev

Q3: How do I find sudo commands?

Open terminal and type sudo -h. It will show you all the sudo command options and arguments. You can also type man sudo to get the manual of the sudo command.

Q4: How do I fix sudo command not found Mac?

Run the following sequence of commands to fix the sudo command not found error in macOS.

1. Check the syntax of command and spelling of sudo, so that you are sure, you are not running the misspelt command

2. switch to root user
$ su -

3. Run the following command

In the case of OS prior to Catalina

# visudo /etc/sudoers

in the case of Catalina

# visudo /private/etc/sudoers

4. Add your user as follows in sudoers file -

username ALL=(ALL) ALL

replace username with your user. e.g John

Save and exit sudoers file

5. reopen the terminal and it will fix all your zsh: sudo command not found error in macOS.

Q5: Do I need to install sudo?

By default all the Linux or Unix distros have sudo installed. But in case you don't, then you must install sudo. it is the recommended way to use root privileged commands (only when required) and helps to avoid mistakes that can break your system.

Ending Note

I have covered all the possible solutions to fix the sudo command not found error and I hope this tutorial is helpful to you. In case, none of the solutions works for you and your system has broken severly, Either re-install your system or let me know the issue via your comments.

I will try to help you to the best of my knowledge.

Keep Learning!!