How to add user to sudoers on Linux
This post will cover multiple procedures on how to add user to sudoers on Linux or in other words, how to give sudo privileges to a user on Linux. The steps showcased here can be applied for almost any Linux distribution.
We’re also going to showcase how to add a missing sudoers file and sudoers group in Linux, which by default may occur on some minimal Linux installations(usually on the first and fresh install).
- These steps require that you initially have either root access or access to a sudo user on your Linux machine, otherwise you won’t be able to execute any of these, meaning you won’t be able to add user to sudoers file or sudo group.
- Double check if sudo and sudoers file does exists.(Sudo group on Red Hat based distributions is called wheel)
First sign that’ll tell your user is not in the sudoers file or in the sudo group is when system returns an error once you try to run the sudo command stating that – “user is not in the sudoers file”.
Sudo group and sudoers file
Configuration files for “sudo” group and “sudoers” file are located by default in the “/etc” directory.
To see if “sudo/wheel” group exists, run a command such as “cat”to print the contents for the file:
To check if sudoers file is present, you can navigate to the “/etc” directory and use list tool to find it or you can use the “file” and/or the “find” tool:
file /etc/sudoers find /etc/sudoers
If by any chance these turn out to be missing, then we’ll need to install the “sudo” toolset. This is covered at the last paragraph of this article.
Add user to sudoers on Linux
Add user to sudo by using usermod
Add user to sudoers on Debian based distributions – Debian/Ubuntu/Mint/Pop OS
usermod -aG sudo username
Add user to sudoers on Red Hat based distributions – CentOS/Fedora/Alma Linux/Rocky Linux
usermod -aG wheel username
Add user to sudo manually by using text editor
To add manually the user to the sudoers files, run on of the commands with the text editor(visudo will run the default text editor on your system):
visudo Or nano /etc/sudoers
Then in the file, add the user as shown as on the picture above with the following arguments:
username ALL=(ALL:ALL) ALL
Save the file, exit and logout and login again as the sudo user in order for the changes to be applied.
Test the sudo permissions by running a simple sudo command. You should an output like in the picture bellow if you have successfully added the user to sudo.
How to install sudo
In some minimal Linux installations, such as Debian, the “sudo” toolset will not be installed by default. That includes the “sudo” group and the “sudoers” file too.
In this paragraph we’ll demonstrate how to add user in sudoers on a minimal Debian install and how to add missing sudoers file, but this also can be applied for other Linux distributions. First step is to log in as a root user.
Next, run the commands to update the system and install the sudo package.
Install sudo on Debian based distributions – Debian/Ubuntu/Mint/Pop OS
apt update apt install sudo
Install sudo on Red Hat based distributions – Red Hat/Fedora/CentOS/Alma Linux/Rocky Linux
dnf update dnf install sudo
Once the sudo package is installed, it will also create the sudo group and the sudoers file too. We can now use two methods to add user in sudoers.
- First is the with usermod command again:
usermod -aG sudo username
- Second method is with a text editor or by running the “visudo” command which will open the sudoers file and manually add the user there to have sudo privileges
Visudo Or Nano /etc/sudoers
To summarize what we have covered in this article is – multiple steps how to add user to sudoers(by utilizing the sudo group and the sudoers file) and also how to install sudo in order to add sudo group and to add sudoers file.
Giving a non root user sudo privileges is a standard practice and is recommended to use sudo user instead of root user.
Thank you very much for your time…