Delinea Blog > Securely add a user to sudoers & sudo group in Ubuntu

Securely add a user to sudoers & sudo group in Ubuntu

Published February 2025
Read time 7 minutes
What you will learn
Need to give general users on Ubuntu root privileges? Our guide provides the top three methods you can follow while maintaining security best practices.

Security best practices recommend managing privileged access for Linux distributions like Ubuntu, just as with any other operating system. That’s why most Linux systems have the root user, or superuser, and regular users. At some point, you may need to elevate a regular user’s privileges so they can execute root-level tasks, such as modifying system configurations and settings. sudo is the tool for leveraging that. Here are three ways to use it, starting with the decision you should make before you touch any of them.

Understanding sudo privileges in Ubuntu

sudo is a command-line tool that allows regular users to run commands as the root user or another user. While sudo is commonly interpreted as "superuser do," it originally stood for "substitute user do." To grant root or superuser privileges to a regular user, you must add them to the sudo group or sudoers file. 

The sudo group grants broad sudo access to any user in the group, while the sudoers file provides granular controls over which users have sudo privileges and the level of privileges they have.

Decide what to grant before you grant it

The decision that matters is the easiest option (and the broadest one available): adding someone to the sudo group. Group membership allows a user to run anything as root. In most cases, that is more than the task requires, which is why sudo misconfigurations show up so often in incident reviews. For a wider view of why this problem persists on Unix and Linux estates, think differently about Unix and Linux privilege management. 

Grant specific commands, not blanket sudo

Grant sudo access only when the task needs it and restrict what the user can do with it. Rather than the blanket entry, name the commands:

username ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart apache2, /user/bin/apt update

That user can restart Apache and update packages. They cannot run arbitrary commands as root. Writing the line this way takes a minute longer than adding someone to the sudo group and is the single biggest difference between a tidy sudoers file and an audit finding.

Think hard before NOPASSWD

NOPASSWD removes the password prompt on sudo commands. It is genuinely useful for automation, where no person is present to type anything, and it is a real risk everywhere else: anyone who gains access to that user's session inherits their sudo rights, with nothing standing in the way. The rule to follow is that NOPASSWD is acceptable when scoped to named commands, as in the example above, but not for an unrestricted sudo entry.

Checking if a user has sudo privileges

The user you want to give sudo privileges may already have them. To check for precise privileges, run the command below:

sudo -l -U username 

Three ways to add a user to sudo

Method 1: Adding a user to the sudo group

Members of a sudo group have root access and can perform commands reserved for superusers. Follow the steps below to add a regular user to the sudo group: 

Step 1: Log in as a root or sudo user

You can grant a user access only if you have administrative privileges. If you aren’t already logged in as one, you can switch by using the command below:

su - 

Step 2: Add the user to the sudo group

Once logged in as a sudo user, you can employ either the usermod or adduser command. Here’s how to use each of them: 

usermod

Run the following command by replacing “username” with the name of the user account you want to give root privileges:

usermod -aG sudo username

This command can also take the format shown below:

usermod -a -G sudo username

adduser

While this command is typically used to create new users, you can also use it to add a user to the sudo group, as shown below:

adduser username sudo 

Step 3: Verify the change

It’s always best to confirm that you’ve added the user to the sudo group. To do this, run the command below:

groups username 

If you see sudo in the output list, that’s your confirmation that you added the user successfully.

Step 4: Test sudo access

You can also check if the user can run sudo tasks by switching accounts using the su - command and then running:

sudo whoami

Note: You may need to explicitly provide the shell in certain configurations, for instance:

su - username -s /bin/bash  

If you get root as the output, this only verifies the user has some sudo access. If sudo is restricted to certain commands, this test may still return "root" while not allowing full administrative access.

Method 2: Manually adding a user to the sudoers file

You can also grant a regular user sudo access by adding them to the /etc/sudoers file, which defines users’ and groups’ sudo permissions. Here’s how to go about it:

Step 1: Open the sudoers file securely

To open /etc/sudoers, run the visudo command as shown below:

sudo visudo

Note: Directly editing this file without visudo can lead to syntax errors that lock all users (including root) out of sudo access.

Step 2: Add the user with sudo privileges

Once it is open, scroll down to the end of the sudoers file and type in:

username ALL=(ALL:ALL) ALL

Note: This grants a user full privileges and can create security risks. It’s best to restrict sudo access to specific commands where possible. 

Step 3: Save and exit

You can save your edits in two ways, depending on the text editor you’re using:

  • For Nano, Press CTRL + X, then Y, and hit Enter.
  • For Vim, Press the ESC key, type :wq, then hit Enter.

Method 3: Creating a custom sudoers configuration file

Instead of editing the /etc/sudoers file and possibly losing sudo privileges due to syntax issues, you can create a new configuration file under /etc/sudoers.d/. To do so, follow the steps outlined below: 

Step 1: Create a custom sudoers file

The command below will open a custom sudoers file:

sudo visudo -f /etc/sudoers.d/username

Step 2: Define user privileges

Next, define the user’s permissions. Since you want to give the user sudo access in this case, run the command below at the end of the configuration file:

username ALL=(ALL:ALL) ALL

Removing a user from sudo access

If you no longer want a regular user to have superuser access, you can revoke it in three ways:

Method 1: Remove from sudo group

To remove a user from a group with root access (sudo), you can run:

sudo deluser username sudo

Method 2: Remove from sudoers file

This method lets you manually remove the user by editing the sudoers file, assuming you added the user directly there. 

After opening the sudoers file safely using the sudo visudo command, find the line that grants the user sudo access. It should look like this (of course, with  replaced with the actual name of the user account):

username ALL=(ALL:ALL) ALL

Method 3: Delete custom sudoers file

If you granted the user access via a /etc/sudoers.d/ configuration file, all you have to do is remove it. To do so, you can run the command shown below:

sudo rm /etc/sudoers.d/username

Monitor and audit sudo usage

Due to the risk of insider threats, you must make sure that users with sudo privileges perform only tasks they’re allowed to. You can track what they do by using the command as shown below:

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

Note: This syntax works on Debian-based systems such as Ubuntu. Other Linux distributions use different log paths like /var/log/secure in RHEL-based systems. 

When sudoers stops scaling

Everything above works on one host. Across a fleet, sudoers files drift: an entry scoped on a dozen servers becomes blanket access, and no one notices until an audit. Privilege Control for Servers shifts the decision from the host to policy. What changes: 

  • Policy instead of sudoers files. Entitlements are defined centrally and applied across Windows and Linux servers, so the same rule reaches every host rather than being re-entered on each one. 
  • Elevation that expires.  Privilege is granted just-in-time for the task rather than held standing, which removes the permanent sudo entry that no one remembers to revoke.
  • Commands attributed to people.  Elevated commands are recorded against the individual who ran them rather than against root, which is the gap auth.log leaves open. 
  • Login as yourself. Users authenticate with enterprise directory identities rather than local accounts, so joiners and leavers are handled where identities already live. 
  • Sessions you can replay. Privileged sessions are recorded end to end, which covers the sudo su case where a single log line hides and entire root shell. 

None of this replaces knowing the sudo syntax, and on a handful of servers the methods above are the right answer. The case for moving to policy is a question of how many hosts you are maintaining and whether you could prove today that sudo holds on all of them. Take an interactive tour of the Delinea Platform to see what that looks like.