How to Add User to Sudoers in Ubuntu¶
Sudo is a command-line program used in Unix-like systems. It enables authorized users to run commands with the privileges of another user, often the root user. This allows users to perform administrative tasks or execute system-level commands without having to log in as the root user. Sudo helps control access to privileged commands and reduce the risks associated with running commands as the root user.
This article will demonstrate two methods for granting a user sudo privileges.
The first method involves adding the user to the sudoers file, which contains information governing which users and groups have access to sudo privileges and the extent of those privileges.
The second method involves adding the user to the sudo group identified in the sudoers file. On Debian-based distributions like Ubuntu and Linux Mint, the "sudo" group members have default access to sudo.
Adding User to the sudo Group¶
To grant a user sudo privileges on Ubuntu, the simplest method is to add the user to the "sudo" group. This group enables its members to run any command as root using sudo, and requires password authentication for sudo usage. It is assumed that the user already exists, but if you need to create a new user, refer to this guide.
To add a user to the group, run the following command as a root or another user with sudo privileges.
Ensure that you replace "username" with the name of the user to whom you want to grant permissions.
Granting sudo access via this method is usually adequate for most use cases.
usermod -aG sudo username
To ensure that the user has sudo privileges,
sudo whoami
Adding User to the sudoers File¶
The /etc/sudoers file defines the sudo privileges for users and groups. Adding a user to this file allows customized access to commands and configuration of security policies.
You can modify a user's sudo access by editing the sudoers file or creating a new configuration file in the /etc/sudoers.d directory. The files in this directory are included in the sudoers file.
It's important to use visudo when editing the /etc/sudoers file. This command checks for syntax errors and prevents loss of sudo access due to errors. Editing the file with a text editor may result in syntax errors and loss of sudo access.
Updated by Gareth Eaton almost 2 years ago · 2 revisions