How to Add a User and Grant Sudo Privileges on Ubuntu | A Safe and Efficient Setup Guide

1. Introduction

Why Manage Users on Ubuntu?

When using Linux-based operating systems like Ubuntu, you may need to add multiple users to the system and assign them different levels of permissions.
For example, if multiple family members or coworkers share a single PC, creating separate logins for each user allows them to manage their own files and settings, preserving privacy and preventing potential issues.

Additionally, restricting user access based on roles can enhance overall system security.
Linux is widely used as a server OS in enterprises, and administrators can grant administrative privileges only to necessary users while assigning general permissions to others. This reduces the risk of system failures caused by accidental misconfigurations.

The Importance and Role of Sudo Privileges

On Ubuntu, administrative tasks require the use of the “sudo” (superuser do) command.
This command temporarily grants administrative privileges, but it does not allow unrestricted execution of all commands by regular users. Instead, their permissions can be carefully configured to limit their actions within the necessary scope.

Having sudo privileges allows users to perform tasks such as:

  • Installing or removing software
  • Updating the system
  • Modifying network settings
  • Adding or deleting users and groups

These operations are critical to system stability, but improper execution could lead to unintended file deletions or security vulnerabilities.
Therefore, sudo privileges should be managed carefully and assigned only to necessary users.

Purpose of This Guide

This guide explains how to add users in Ubuntu and grant them sudo privileges in a straightforward manner.
Additionally, it covers best practices for managing users with sudo privileges and important security considerations, helping you maintain a secure system.

2. Basic User Addition Methods

Difference Between adduser and useradd Commands

To create a new user in Ubuntu, you can use either the “adduser” or “useradd” command.
Both commands serve the purpose of adding users, but they have slight differences in usage and functionality. Understanding their characteristics can help you use them effectively.

adduser Command

The “adduser” command is the recommended method for adding new users to an Ubuntu system.
It is a higher-level command than useradd and automatically creates the home directory and initial user settings.
Since it follows an interactive process, even beginners can use it easily. Below is a basic usage example:

sudo adduser username

After entering the username, you will be prompted to provide the following additional details:

  • Full Name
  • Work and Home Phone Number
  • Other Notes

Once the information is entered, the home directory is automatically created, and the basic user setup is completed.

useradd Command

On the other hand, the “useradd” command provides a lower-level approach, allowing more detailed customization when creating a user.
Unlike adduser, useradd does not perform automatic configurations such as creating a home directory unless specified with options. Below is an example usage:

sudo useradd -m username

Adding the “-m” option ensures that a home directory is created. The useradd command is more suitable for advanced use cases, such as when automating user creation via scripts.

When to Use adduser vs. useradd

For general Ubuntu environments, using the adduser command is recommended for its ease of use and automated setup. However, system administrators who need precise control over user configurations or automation may prefer useradd.
Understanding the differences between these commands can help optimize user management in Ubuntu.

3. Granting Sudo Privileges

Adding Users to the Sudo Group

On Ubuntu, the common way to grant a user administrative (sudo) privileges is by adding them to the “sudo” group.
Users in the sudo group can temporarily borrow administrative rights, enabling them to modify system settings or install software.

Command to Add a User to the Sudo Group

  1. Using the usermod Command
    Run the following command to add a user to the sudo group:
sudo usermod -aG sudo username

After executing this command, the user needs to log out and log back in for the changes to take effect.

  1. Using the gpasswd Command
    Another way to add a user to the sudo group is by using the gpasswd command:
sudo gpasswd -a username sudo

Editing the sudoers File

Another method of granting sudo privileges is by directly editing the sudoers file.
This file defines which users are allowed to execute sudo commands and can be used to configure special permissions.

  1. Why Use the visudo Command?
    When editing the sudoers file, it is recommended to use the visudo command to prevent syntax errors. Open the sudoers file with the following command:
sudo visudo
  1. Example of Editing the sudoers File
    To grant sudo privileges to a specific user, add the following line:
username ALL=(ALL:ALL) ALL
  1. Restricting Sudo Access to Specific Commands
    The sudoers file can also be configured to allow users to execute only specific commands with sudo privileges.
testuser ALL=(ALL) /usr/bin/apt

Verifying Sudo Privileges

After granting sudo privileges, verify the user’s permissions by running the following command:

sudo -l

4. Verifying Sudo Settings and Security Considerations

How to Verify Sudo Permissions

After assigning sudo privileges, it is crucial to verify that the settings have been applied correctly.
Since sudo access allows critical system operations, any misconfiguration should be carefully checked.

Using the sudo -l Command

To confirm that sudo permissions are correctly assigned, run:

sudo -l

This command displays a list of sudo commands that the user is permitted to execute.
If “ALL” is displayed, it means the user has permission to execute all sudo commands.

Security Considerations

When granting sudo privileges in Ubuntu, it is essential to consider several security measures to protect the system.

Granting Only Necessary Permissions

Since sudo access allows users to perform system-wide operations, it is crucial to limit sudo privileges to only necessary users.

Managing and Backing Up the sudoers File

Using the sudo visudo command helps prevent syntax errors, but it is always a good practice to back up the sudoers file before making changes.

Checking the sudo Command Usage History

Monitoring sudo command usage can help identify security risks. The following command displays a history of sudo operations:

sudo cat /var/log/auth.log | grep sudo

Regularly Reviewing User Privileges

As more users gain sudo privileges, the security risks increase. It is recommended to periodically review sudo users and remove unnecessary access while carefully selecting new users to grant sudo privileges.

5. Frequently Asked Questions About User Management

Sudo Privileges Not Applied After Adding a User to the Sudo Group

Sometimes, sudo privileges may not take effect immediately after adding a user to the sudo group.
In this case, ask the user to log out and log back in to ensure the group changes take effect.

If the changes are still not applied, verify the user’s group membership using the id command:

id username

How to Remove Sudo Privileges

To revoke sudo privileges from a user, remove them from the sudo group using the following command:

sudo gpasswd -d username sudo

How to Add a User to Multiple Groups

Users can be assigned to multiple groups to allow different levels of access to system resources.

sudo usermod -aG group1,group2 username

Allowing a User to Execute Only Specific Commands with Sudo

Instead of granting full sudo access, you can allow a user to execute only specific commands with sudo privileges.

username ALL=(ALL) /usr/bin/apt

6. Conclusion

This guide covered how to manage users and assign privileges in Ubuntu.
Adding users and configuring sudo privileges properly is a crucial skill in system administration.
By following these key points, you can enhance system security and streamline user management:

  1. Understanding the Differences Between User Addition Commands
    Using the adduser command simplifies user creation, while the useradd command provides more control for advanced users and automation.
  2. Granting and Verifying Sudo Privileges
    Adding users to the sudo group allows them to perform administrative tasks, while editing the sudoers file provides more granular control over command execution.
  3. Ensuring Security
    Limit sudo privileges to essential users, regularly review permissions, and take preventive security measures to protect your system.

By mastering these basic operations, you can manage Ubuntu systems more securely and efficiently.
Apply this knowledge to maintain a stable and well-managed system.