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.
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
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.
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
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
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
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.
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
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
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"
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
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!!
I am trying to deploy django app.
When I print
apt-get update
I see
W: Unable to read /etc/apt/apt.conf.d/ - DirectoryExists (13: Permission denied)
W: Unable to read /etc/apt/sources.list.d/ - DirectoryExists (13: Permission denied)
W: Unable to read /etc/apt/sources.list - RealFileExists (13: Permission denied)
E: List directory /var/lib/apt/lists/partial is missing. - Acquire (13: Permission denied)
E: Unable to read /var/cache/apt/ - opendir (13: Permission denied)
E: Unable to read /var/cache/apt/ - opendir (13: Permission denied)
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?
When I print sudo apt-get update
I see
-bash: sudo: command not found
I tried to use su
instead of sudo
.
But it is strange.
For example I print su apt-get update
And nothing happens
I just see a new line,
(uiserver):u78600811:~$ su apt-get update
(uiserver):u78600811:~$
The same if I try to install some packages.
What do I do?
If it is useful info – I am using Debian
(uiserver):u87600811:~$ uname -a
Linux infong1559 3.14.0-ui16294-uiabi1-infong-amd64 #1 SMP Debian 3.14.79-2~ui80+4 (2016-10-20) x86_64 GNU/Linux
asked Mar 30, 2017 at 20:18
4
By default sudo is not installed on Debian, but you can install it. First enable su-mode:
su -
Install sudo by running:
apt-get install sudo -y
After that you would need to play around with users and permissions. Give sudo right to your own user.
usermod -aG sudo yourusername
Make sure your sudoers file have sudo group added. Run:
visudo
to modify sudoers file
and add following line into it (if it is missing):
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
You need to relogin or reboot device completely for changes to take effect.
answered Feb 21, 2018 at 12:27
Maksim LuzikMaksim Luzik
2,3331 gold badge8 silver badges14 bronze badges
12
su
and sudo
are two different, but related commands. It is unusual for sudo
not to be installed, but it may simply not be in your Path. Try /usr/bin/sudo command
.
If indeed sudo
is not available, you need as you surmised to use su
, but it does not work in the same way as sudo
. The simplest way to use it is to simply run:
su -
This will ask you for the root
user’s password, at which point you should probably apt install sudo
, log out of the root shell, and then proceed as normal.
Mind that unlike sudo
, which asks you for your password, su
will ask you for root‘s password.
answered Mar 30, 2017 at 20:28
DopeGhotiDopeGhoti
72.8k8 gold badges96 silver badges133 bronze badges
6
Since it’s a commercial server you won’t have access to root account nor be able to operate with root privileges. This means you won’t be able to run sudo
nor install packages. What you can try to do is:
-
Check if you have access to a compiler and compile what you want for yourself and in your home space.
-
Check if you can run a virtual machine. This might let you run your private instance of an OS, on which you would install packages.
answered Mar 30, 2017 at 21:04
0
In a new Debian server install I too found out that sudo
is not installed by default, but it can be done as root:
$ su root
# apt install sudo
What puzzled me is that I still got errors with visudo
and usermod
:
# visudo
bash: visudo: command not found
# apt install visudo
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package visudo
Actually, visudo
is included with the sudo package, it was just not in the PATH
for root:
# dpkg -S visudo
sudo: /usr/sbin/visudo
sudo: /usr/share/man/man8/visudo.8.gz
So I added it to the root’s ~/.bashrc
.
PATH=$PATH:/usr/sbin
Now it can find visudo
and usermod
which can be used to setup sudo access.
answered Oct 8, 2021 at 16:16
NagevNagev
4095 silver badges10 bronze badges
sudo
, Linux’s most useful command, is occasionally missing from several Linux distributions, most notably docker containers and lightweight Linux distributions.
sudo
is a command that allows users to run commands with root access. It is the most helpful command included in almost all major Linux distributions. Yes almost all. Several Linux distros, particularly docker images, do not ship the sudo
package by default. If you see the error bash: sudo: command not found
it simply means that sudo is not installed.
Table of Contents
bash: sudo: command not found
In most circumstances, starting your package manager and installing sudo
will be enough. However, installing a new package necessitates the use of root
capabilities. Log in as the root user and install sudo
.
For Ubuntu or its derivatives
$ apt install sudo
Fedora or its derivatives
$ dnf install sudo
For RHEL or its derivatives
$ yum install sudo
Arch Linux or its derivatives
$ pacman install sudo
For Docker images
It may not be possible if there is no root account. When you build a docker container from a docker image, the image may or may not have a root user. So we will have to create a custom image with sudo pre-installed. Don’t worry, it is easy.
Create custom Docker image
A Dockerfile is required to construct a custom docker image. To create a Docker container you need to define everything in a Dockerfile using a specific syntax. Next you can use the “docker build” command to generate an image from that Dockerfile. For example if you want to use a Docker image that is hosted on Docker Hub you can specify it as the base image in your Dockerfile and then add the necessary commands to install additional tools such as sudo.
Create Dockerfile
touch Dockerfile
Open the Dockerfile in your favorite text editor.
nano Dockerfile or gedit Dockerfile
# This is the base image we will use to create our custom docker image
FROM ubuntu:latest
#
# the maintainer of an image
LABEL maintainer="[email protected]"
#
# Install the tools (sudo)
RUN apt-get update && apt-get upgrade -y && apt install sudo tasksel -y
We used the RUN label in the third part, which is marked with “Install the tools”, and any command after RUN will be executed before generating our custom image. So, by typing a command here, we can install any package we wish for. If we keep writing commands, we can even spin up a full server.
For more knowledge on Dockerfile, refer to the docker’s official documentation.
Save and close the Dockerfile. Now, open the command line and navigate to the directory containing the Dockerfile and run the following command –
sudo docker build -t image-name:tag .
Remember that the docker build command only accepts the directory, not the Dockerfile. If you specify in the command the Dockerfile, you’ll get the error – unable to prepare context: context must be a directory: /home/sandy/Dockerfile
. So you must be inside the directory containing Dockerfile and use . to specify the current directory or the full path to the Directory containing Dockerfile.
And that’s it. docker build
will start building the command. It will take a few seconds. Once done our custom docker image is saved in our local image cache. We can now use the image-name:tag to create a docker container that has sudo pre-installed.
Mohd Sohail is a web developer and a Linux sysAdmin. He also loves to write how-to articles, applications reviews and loves to use new Linux distributions.
895 Posts
Debian, Linux, Ubuntu
- 25.09.2022
- 3 329
- 2
- 19.02.2023
- 8
- 8
- 0
- Содержание статьи
- Установка sudo
- Добавление пользователя в группу sudo
- Комментарии к статье ( 2 шт )
- Добавить комментарий
Используя операционную систему Linux, можно столкнуться с отсутствием программы sudo. В таком случае, при выполнении команды sudo, будет возвращена ошибка “sudo: command not found” в английской версии, или “команда sudo не найдена” в русской версии. В нашем материале мы разберем какие действия необходимо сделать, чтобы установить sudo в системе.
Установка sudo
Первым делом, установим программу sudo через стандартный менеджер пакетов в используемом дистрибутиве Linux.
Команды ниже, следует выполнять из под учетной записи суперпользователя (root), выполнив из под него вход в систему, или же, запустив сеанс суперпользователя с помощью команды su -
(обязательно с символом минуса).
CentOS и основанные на нем дистрибутивы (Fedora, RHEL):
yum install sudo
Debian и основанные на нем дистрибутивы (Linux Mint, Ubuntu):
apt install sudo
Добавление пользователя в группу sudo
Добавим пользователя в группу sudo:
usermod -aG sudo [ИМЯ ПОЛЬЗОВАТЕЛЯ]
В большинстве случаев, данных действий должно хватить для того, чтобы добавленный в группу пользователь мог начать работу с sudo. Так же, не стоит забывать о том, что ранее залогиненому пользователю нужно будет перезайти в систему.
I am trying to deploy django app.
When I print
apt-get update
I see
W: Unable to read /etc/apt/apt.conf.d/ - DirectoryExists (13: Permission denied)
W: Unable to read /etc/apt/sources.list.d/ - DirectoryExists (13: Permission denied)
W: Unable to read /etc/apt/sources.list - RealFileExists (13: Permission denied)
E: List directory /var/lib/apt/lists/partial is missing. - Acquire (13: Permission denied)
E: Unable to read /var/cache/apt/ - opendir (13: Permission denied)
E: Unable to read /var/cache/apt/ - opendir (13: Permission denied)
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?
When I print sudo apt-get update
I see
-bash: sudo: command not found
I tried to use su
instead of sudo
.
But it is strange.
For example I print su apt-get update
And nothing happens
I just see a new line,
(uiserver):u78600811:~$ su apt-get update
(uiserver):u78600811:~$
The same if I try to install some packages.
What do I do?
If it is useful info – I am using Debian
(uiserver):u87600811:~$ uname -a
Linux infong1559 3.14.0-ui16294-uiabi1-infong-amd64 #1 SMP Debian 3.14.79-2~ui80+4 (2016-10-20) x86_64 GNU/Linux
asked Mar 30, 2017 at 20:18
4
By default sudo is not installed on Debian, but you can install it. First enable su-mode:
su -
Install sudo by running:
apt-get install sudo -y
After that you would need to play around with users and permissions. Give sudo right to your own user.
usermod -aG sudo yourusername
Make sure your sudoers file have sudo group added. Run:
visudo
to modify sudoers file
and add following line into it (if it is missing):
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
You need to relogin or reboot device completely for changes to take effect.
answered Feb 21, 2018 at 12:27
Maksim LuzikMaksim Luzik
2,3331 gold badge8 silver badges14 bronze badges
12
su
and sudo
are two different, but related commands. It is unusual for sudo
not to be installed, but it may simply not be in your Path. Try /usr/bin/sudo command
.
If indeed sudo
is not available, you need as you surmised to use su
, but it does not work in the same way as sudo
. The simplest way to use it is to simply run:
su -
This will ask you for the root
user’s password, at which point you should probably apt install sudo
, log out of the root shell, and then proceed as normal.
Mind that unlike sudo
, which asks you for your password, su
will ask you for root‘s password.
answered Mar 30, 2017 at 20:28
DopeGhotiDopeGhoti
72.8k8 gold badges96 silver badges133 bronze badges
6
Since it’s a commercial server you won’t have access to root account nor be able to operate with root privileges. This means you won’t be able to run sudo
nor install packages. What you can try to do is:
-
Check if you have access to a compiler and compile what you want for yourself and in your home space.
-
Check if you can run a virtual machine. This might let you run your private instance of an OS, on which you would install packages.
answered Mar 30, 2017 at 21:04
0
In a new Debian server install I too found out that sudo
is not installed by default, but it can be done as root:
$ su root
# apt install sudo
What puzzled me is that I still got errors with visudo
and usermod
:
# visudo
bash: visudo: command not found
# apt install visudo
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package visudo
Actually, visudo
is included with the sudo package, it was just not in the PATH
for root:
# dpkg -S visudo
sudo: /usr/sbin/visudo
sudo: /usr/share/man/man8/visudo.8.gz
So I added it to the root’s ~/.bashrc
.
PATH=$PATH:/usr/sbin
Now it can find visudo
and usermod
which can be used to setup sudo access.
answered Oct 8, 2021 at 16:16
NagevNagev
4095 silver badges10 bronze badges