How to Fix sudo: command not found Error in Linux?
sudo command not found
error means sudo package is not installed or PATH variable does not include sudo’s directory.
To fix this bash error, you can install sudo in linux using apt install sudo
command (apt package manager for Debian-based distributions).
2 Methods to Fix bash: sudo: command not found error
1. Install sudo Command in Linux
If you want to run a specific command as a root user or another user, you need sudo command.
The sudo command is pre-installed on most systems. But in some linux distributions such as Debian 10 and later, RHEL 8 and later, CentOS, Fedora and Arch Linux, it is removed or not installed.
You must log out of your user account and log in as root for install sudo package as the root user. Run the command below:
$ sudo su -
You can also switch to the root user. Now, you can update the package lists and install the sudo package. So, simply run:
# apt update -y
# apt install sudo -y
To install sudo on Debian-based systems, use the following command to switch to the root user and then, install sudo:
su -
apt install sudo
To install sudo on CentOS Stream, Fedora, Rocky Linux, or Alma Linux, enter:
# su -
# yum install sudo
To install sudo on Arch Linux, type:
# pacman -Sy sudo
To give all the sudo permissions, run:
$ user od -AG <username>
Then, you can check the permissions by opening the ‘sudoers’ file:
$ nano /etc/sudoers
Now, you should be able to use sudo to execute all the commands.
2. Add sudo’s Directory to PATH Variable
PATH variable is an environment variable in linux. it contains a list of paths that find executable files of commands you run.
One of the reason for bash sudo command not found error is PATH Variable can’t find directory where sudo is located.
To check your PATH run echo
command:
echo $PATH
If sudo’s directory is in your PATH, You will get one of the following directories in output:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
If sudo’s directory is not in your PATH, you can add /usr/bin
directory by following command:
export PATH=$PATH:/usr/bin
By running this command, the path variable can find the directory containing sudo. So bash error in Linux will be solved.
Add User to Sudo Group in Linux [CLI & GUI Methods]
After install sudo package and fix sudo command not found error in linux, you can add users to sudo group with command line and GUI.
1. Using Command-Line
To add a Superuser on Ubuntu and Debian, run:
sudo adduser username
Run the following command to add your user to the sudo group on Debian:
usermod -aG sudo your_username
Then, you can confirm whether the user has been added to the group by issuing the following command:
# groups your_username
Replace the name of the user you need to check that has been added to the group with your-username in the above command.
To add the user to the wheel group on Arch-based systems, type:
usermod -aG wheel your_username
And use the command below to add your users to the sudo group on Fedora and other RHEL-based distros:
usermod -aG wheel your_username
By the way, you can use a single command instead of multiple commands to create a new superuser:
sudo adduser username sudo
The verification procedure is unchanged from before.
To test if the bash error has gone, log back in as your regular user. If all have passed successfully, you should not view sudo: command not found error anymore, and fixing must have been finished at this point.
2. Using Graphical Interface
To use the GUI method to add the user to the sudo, you must first create a new user through the command line and then, follow the below steps to proceed.
Step 1. To view a list of users and find the newly created user/users, go to the Application menu and click on the Users option.
Here you can switch to another user account if you click on the Unlock option and on the root password.
Step 2. As soon as you choose it, an option to make the newly created user account an administrator account will appear. To add the account as a sudo account, toggle the button next to the Administrator label.
There, you added a sudo user using the graphical interface.
What to Do After Fixing sudo command not found in Linux?
After installing sudo and adding your user to sudo group, you can run any command without having to log in as root.
For security reasons, only the root user and the users with administrator privileges are allowed to use sudo. So, you should change file ownership and limit what the user can do with files and directories
FAQ
Are Sudo and Su the same?
No. Su and sudo both increase the current user's privileges. The primary distinction between the two commands is that su calls for the target account's password, whereas sudo calls for the current user's password. Therefore, using sudo is significantly safer because it avoids sending sensitive information.
Why command not found occur after setting a custom $PATH to run my scripts?
When sudo run commands with a fresh environment instead of inheriting modified environment variables, the ‘’command not found’’ error will be displayed.
Related Article : What is Debian OS
Related Service: Buy Linux VPS