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.
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.
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 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.
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.
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
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:
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 -
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
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.
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.
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:
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.
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.
You can save your edits in two ways, depending on the text editor you’re using:
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:
The command below will open a custom sudoers file:
sudo visudo -f /etc/sudoers.d/username
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
If you no longer want a regular user to have superuser access, you can revoke it in three ways:
To remove a user from a group with root access (sudo), you can run:
sudo deluser username sudo
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
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
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.
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:
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.