How to Add a User to Sudoers on Ubuntu 22.04 | 20.04

Ubuntu is a popular operating system widely used for servers and desktops. If you’re running a Ubuntu server, having administrative privileges to perform tasks like installing software, configuring services, and managing files is essential. In this guide, we’ll show you how to add a user to the sudoers list on Ubuntu, allowing them to run commands with superuser privileges.

What is sudo?

Ubuntu is a highly sought-after operating system commonly used across servers and desktops. Using Ubuntu, it is important to have administrative privileges to perform various tasks, such as software installation, service configuration, and file management. This guide aims to walk you through adding a user to the sudoers list in Ubuntu, allowing them to execute commands with superuser privileges.

To grasp the process of adding a user to the sudoers list, it is vital to understand what sudo is. The sudoers file on Debian-based systems like Ubuntu controls the users who can run commands with superuser privileges. Only the root user has this capability by default. However, you can add other users to the sudoers list to enable them to perform administrative tasks. This is crucial when setting up a new server, especially when multiple users need access.

Change to Root Account

Logging into the Ubuntu root account is critical when creating a new user with sudo privileges on Ubuntu. The root account is created during the installation process of Ubuntu and is accessible with a password set by the user. This password is necessary to create a new sudo user. It is important to ensure the root password is secure and only used when necessary, like when adding new sudo users.

To switch to the root account, you can use the “su” command in the terminal.

su

After entering the root password, the system will prompt confirmation before granting access to the root account. Upon verifying the password, the terminal will display the username as “root,” indicating that you have successfully logged in as the root user.

Example output of root account:

root@ubuntu-linux:/home/joshua# 

If you have forgotten your root password but have sudo access, you can reset the root password using the following command:

sudo passwd root

Create a User Account

Adding a new user with sudo privileges to a Ubuntu system starts with creating the user account. Although granting administrative privileges to an existing user account is possible, this guide will explain the steps for creating a new account from the beginning.

To create a new user account, you can use the “adduser” command in the terminal, followed by the desired username. The system will prompt you to enter additional information, such as the user’s full name, password, and contact information. Once you have provided this information, a new user account will be created.

adduser <example username>

Example with my name:

adduser josh

After executing the previously mentioned command and pressing enter, the system will prompt creating a password for the new user account. Choosing a strong password is crucial to ensure the security of the account. Using a combination of uppercase and lowercase letters, symbols, numeric values, and special characters is recommended to enhance the password’s complexity and make it harder to guess or crack.

Using a strong password is crucial since the new user account has been granted sudo privileges, which provides administrative access to the system. This level of access is powerful and could result in severe damage to the system or loss of data if unauthorized access occurs. As such, it is essential to limit access to the new user account to authorized individuals only and ensure that all users with access to the account use a strong password.

Example of the prompt:

After successfully setting the password for the new user, the system will prompt for additional information about the user, such as full name, contact details, and other relevant information. While these details are not mandatory, providing as much information as possible is recommended to represent the user within the system accurately.

However, if you choose not to provide additional information, you can skip this step by pressing the enter key and proceeding to the next stage. It is important to remember that the information provided will be used for identification purposes and to ensure the user is correctly represented within the system.

Example of the prompt:

After completing the previous step, you should type “Y” and press the enter key to add the new user to the system.

To verify that the new user has been successfully added, you can execute the following command in the terminal:

grep 'username' /etc/passwd

Replace “username” with the username of the new user you created. This command searches the system’s password file for the user’s name and displays their account information, such as their home directory and login shell.

Example output:

It’s crucial to understand that the system stores information related to new and existing users in the file located at /etc/passwd. This file references user details, such as the user name, user ID, group ID, home directory, and shell. This information serves various purposes, such as authentication, authorization, and system administration tasks. It is highly recommended to keep this file up-to-date and accurate for seamless system management.

Add New User To Sudoers Group

In the next section, having acquired knowledge about adding a user to the system, you will learn how to grant sudo privileges to the newly added or existing user. To achieve this, execute the command below:

sudo usermod -aG sudo <example username>

Example with my name:

sudo usermod -aG sudo josh

Verifying whether the user has been successfully added to the sudoers group is a good practice. You can achieve this by executing the “id” command as shown below:

id <username>

To use the “id” command, replace “<username>” with the actual username you want to verify. This command will display the user’s details, such as their user ID, group ID, and any additional groups they belong to. If the sudo group is listed, the user has been successfully granted sudo privileges.

Example with my name:

id josh

Example output:

uid=1001(josh) gid=1001(josh) groups=1001(josh),27(sudo)

You can also use the “gpasswd” command as an alternative. Below is an example of how to use the “gpasswd” command to grant sudo privileges to a user:

gpasswd -a <example username> sudo

To use the “gpasswd” command to grant sudo privileges to a user, replace “<username>” with the actual username of the user you want to add to the sudoers group. This command will add the user to the sudo group, thus granting them sudo privileges.

Example with my name:

gpasswd -a josh sudo

Example output:

adding josh to group sudo

Confirm New Sudo User

Once you have added the desired user to the sudoers group, you must test the account to ensure that sudo privileges have been granted successfully. You can use the “su” command to achieve this by switching to the user account you just created. The command is followed by the username, as shown below:

su <example username>

To test the new user’s sudo privileges, replace “<username>” with the username of the user you have granted sudo access. This command will switch the user to the newly created or existing account, allowing you to test the account’s sudo privileges.

Example with my name:

su josh

You can verify the username by executing the “sudo whoami” command. This command will display the username of the current user with elevated privileges.

sudo whoami

When you execute the “sudo whoami” command, the system will prompt you to enter the password for the sudo user you are using. After entering the correct sudo username and password, you should receive a confirmation message indicating that the user has been granted sudo privileges and can perform administrative tasks on the system.

root

Congratulations! You have successfully added a new user to the sudoers group and granted them elevated privileges on Ubuntu.

Conclusion: Adding a New User to Sudoers on Ubuntu Linux

In summary, adding a new user to the sudoers group on Ubuntu is a straightforward process involving creating the user account, setting the password, and granting sudo access. The steps involved include logging into the Ubuntu root account, creating the user account, setting the password, adding the user to the sudoers group, and confirming the user’s sudo access. The process can be completed using the usermod or gpasswd commands and testing the new user account with the sudo command.

Following these steps, you can effectively add a new user to the sudoers group on Ubuntu, granting them the necessary permissions to execute commands with elevated privileges. It is important to remember that granting sudo privileges should only be done for authorized individuals who require elevated permissions to perform system administration tasks. Additionally, it is crucial to ensure that all users with sudo access use strong passwords and that access to the account is limited to authorized individuals only.

Share to...