How to Add a User to Sudoers on Ubuntu
In Linux philosophy, running privileged tasks as the root user is highly discouraged. The reason is simple - all it takes is for the root user to run a fatal command and the entire system comes apart. For this reason, you should always run system-related tasks as a regular user with sudo privileges.
For this to happen, you need to grant administrative privileges or sudo privileges to the regular user. You can achieve this in 2 ways. The first method is to add the user to the sudoers group which is already specified in the sudoers file. The second method is to manually append the user to the sudoers file that contains information such as groups and user with elevated privileges.
So what is sudo
? Sudo is a command which is short for Superuser do. You use sudo to grant regular users privileged or elevated rights to run system-related tasks. It is invoked at the beginning of every command when running a system-related task as a standard user.
In this tutorial, I will show how to add a user to sudoers on Ubuntu 20.04 LTS Linux.
1) How to add a user to sudoers group
On Ubuntu, this has to be the easiest option of granting administrative rights to a regular user. When users are added to the sudo group, they acquire the ability to invoke sudo when running system-related tasks. The usermod command is a command used to grant administrative privileges to regular login users. The usermod command is used to change a user's attribute such as shell, password expiration date, and groups to mention a few.
Lets first add a new user using adduser command which is the recommended command to create the user Debian.
The following command creates a user named 'jack':
$ sudo adduser jack
Output Adding user `jack' ... Adding new group `jack' (1001) ... Adding new user `jack' (1001) with group `jack' ... Creating home directory `/home/jack' ... Copying files from `/etc/skel' ... Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully Changing the user information for jack Enter the new value, or press ENTER for the default Full Name []: Jack Danny Room Number []: Work Phone []: Home Phone []: Other []: Is the information correct? [Y/n] Y
Run id command
to verify which groups the user belongs:
$ id jack
Now use the usermod command to add the existing user to the sudoers group, use the syntax:
$ usermod -aG sudo username
To add the user 'jack' to the sudoers group, execute the command:
$ usermod -aG sudo jack
Next, verify if the user 'jack' belongs to the sudo group using the command:
$ groups jack
or
$ id jack
From the output, user 'jack' now belongs to 2 groups, 'jack' and 'sudo' group. This confirms that we have successfully added a regular user 'jack' to the sudoers group.
Furthermore, you can switch to user 'jack' using the command:
$ su jack
Next, run the sudo whoami
command as shown. You should get the output as root.
$ su whoami
2) How to add a user to the sudoers file
The sudoers file defines the 'users' and 'groups' privileges.
The sudoers file /etc/sudoers
can be invoked using the command:
$ visudo
This opens the sudoers file using the nano text editor. If you prefer to open this file using the vim (VI Improved editor), run the command:
$ EDITOR=vim visudo
Add the line below in the file and save:
username ALL=(ALL:ALL) ALL
For our user, the command will be:
jack ALL=(ALL:ALL) ALL
Finally, save the file and close the text editor.
Conclusion
In this tutorial, we have learned 2 ways to add a user to sudoers file on Ubuntu.
Sudo command is the safest way of running commands that require root privileges. This way, you minimize the chances of making accidents that can cripple your system.
Related Read: How to Add a User to a Group in Linux