How to add users to Sudoers on Ubuntu

In the Linux philosophy, it is strongly recommended not to run privileged tasks as the root user. The reason is simple-root users only need to execute fatal commands and the entire system will crash. Therefore, you should always run system-related tasks as a normal user with sudo privileges.

To do this, you need to grant administrative rights or sudo rights to ordinary users. You can achieve this in two ways. The first method is to add the user to the sudoers group specified in the sudoers file. The second method is to manually append users to the sudoers file, which contains information such as groups and users with elevated privileges.

what is that sudo? Sudo is an abbreviated command for super user do. You use sudo to grant ordinary users privileges or elevated permissions to run system-related tasks. When running system-related tasks as a standard user, it will be called at the beginning of each command.

In this tutorial, I will show how to add users to sudoers on Ubuntu 20.04 LTS Linux.

1) How to add users to the sudoers group

On Ubuntu, this must be the easiest option to grant administrative rights to ordinary users. After adding users to the sudo group, they will get the ability to call sudo when running system-related tasks. The usermod command is used to grant administrative privileges to regular login users. The usermod command is used to change user attributes, such as the shell, password expiration date, and group.

First use the adduser command to add a new user. This is the recommended command to create a 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 Verify the user’s group:

$ id jack

Add users to sudoers ubuntu

Now use the usermod command to add existing users to the sudoers group, using the following syntax:

$ usermod -aG sudo username

To add the user “jack” to the sudoers group, execute the following command:

$ usermod -aG sudo jack

Next, use the following command to verify that user’jack’ belongs to the sudo group:

$ groups jack

Or

$ id jack

Add users to the sudoers group

From the output, the user “jack” now belongs to two groups, the “jack” and “sudo” groups. This confirms that we have successfully added the regular user “jack” to the sudoers group.

In addition, you can use the following command to switch to user “jack”:

$ su jack

Next, run sudo whoami The commands are as follows. You should get output as the root user.

$ su whoami

Sudo who

2) How to add users to sudoers file

The sudoers file defines “user” and “group” privileges.

sudoers file /etc/sudoers You can use the following command to call:

$ visudo

This will open the sudoers file using the nano text editor. If you prefer to use vim (VI improved editor) to open this file, please run the following command:

$ EDITOR=vim visudo

Add the following line to the file and save it:

username ALL=(ALL:ALL) ALL

For our users, the command will be:

jack ALL=(ALL:ALL) ALL

Finally, save the file and close the text editor.

in conclusion

In this tutorial, we learned two ways to add users to sudoers files on Ubuntu.

Sudo commands are the safest way to run commands that require root privileges. In this way, you can minimize the chance of an accident that causes the system to crash.

Related Read:  How to Add a User to a Group in Linux
Sidebar