How to Add a User to Sudoers in Ubuntu
Learn how to add a user to sudoers in ubuntu using usermod, safe visudo editing, granular /etc/sudoers.d/ rules, and passwordless execution.

5 min read
Learn how to add a user to sudoers in ubuntu using usermod, safe visudo editing, granular /etc/sudoers.d/ rules, and passwordless execution.
- Prerequisites and Supported Releases
- Method 1: Adding a User to the Sudo Group (Recommended)
- Method 2: Creating Granular Rules in /etc/sudoers.d/ with visudo
In the Linux security paradigm, the root superuser possesses unrestricted power to modify system files, install software, and reconfigure kernel parameters.
Logging directly into a server as root introduces significant security risks, from catastrophic typing accidents to credential theft via automated brute-force attacks.
Ubuntu eliminates direct root logins by default, instead requiring administrators to execute privileged commands using the sudo (superuser do) utility.
When you add a user to sudoers, the system consults security policies defined in /etc/sudoers to determine which commands the user can execute with administrative rights.
In this guide, we walk through how to add a user to sudoers in ubuntu using both the standard sudo group approach and granular /etc/sudoers.d/ configuration files.
Comprehensive Linux administrator guide on granting administrative privileges on Ubuntu 24.04 LTS and 22.04 LTS. Explains the difference between adding users to the secondary sudo group via usermod versus configuring granular privilege drop-in files in /etc/sudoers.d/ using visudo, avoiding syntax errors, and configuring NOPASSWD exemptions securely.
Prerequisites and Supported Releases
This guide is thoroughly verified on Ubuntu 24.04 LTS (Noble Numbat) & Ubuntu 22.04 LTS. The instructions apply cleanly to cloud instances (AWS EC2, Google Compute Engine, DigitalOcean, Hetzner), bare-metal servers, and local virtual machines.
Before proceeding, ensure you have the following requirements satisfied:
- A server or workstation running Ubuntu 24.04 LTS or Ubuntu 22.04 LTS.
- An existing user account with
sudoprivileges or direct console root access. - Basic familiarity with the Linux terminal shell and user management commands.
- A target user account created on the system.
Method 1: Adding a User to the Sudo Group (Recommended)
The fastest, most standard way to grant administrative privileges on Ubuntu is adding the user to the predefined sudo group.
In Ubuntu's default configuration, members of the sudo group are granted all administrative rights upon entering their personal account password.
Use the usermod utility with the -aG (append to supplementary group) flags:
Replace username with the actual login name of the user:
# Add user to the sudo supplementary group:
sudo usermod -aG sudo username
# Verify user group membership:
groups username
# Output confirms membership in both personal and sudo groups:
# username : username sudo
Alternative Command: adduser username sudo
Ubuntu also provides the high-level Perl wrapper adduser.
Executing sudo adduser username sudo achieves the identical result with human-friendly terminal feedback:
# Alternative syntax using adduser:
sudo adduser username sudo
# Output: Adding user `username' to group `sudo' ...
# Done.
Method 2: Creating Granular Rules in /etc/sudoers.d/ with visudo
Directly editing the main /etc/sudoers file is discouraged because package upgrades can overwrite customizations.
Instead, Ubuntu parses drop-in configuration snippets placed inside the /etc/sudoers.d/ directory in alphabetical order.
Always author drop-in files using visudo -f, which locks the file against concurrent edits and checks syntax before saving:
Create a dedicated configuration file for the user:
# Open or create a drop-in file using visudo:
sudo visudo -f /etc/sudoers.d/username
# Add the standard sudo privilege specification line:
# username ALL=(ALL:ALL) ALL
# Save and exit (Ctrl+O, Enter, Ctrl+X in nano).
# Enforce strict 0440 read-only file permissions:
sudo chmod 0440 /etc/sudoers.d/username
Understanding Sudoers Rule Syntax Anatomy
The standard rule statement username ALL=(ALL:ALL) ALL contains four distinct components:
Review this breakdown to understand how sudo determines privilege scope:
Step 3: Configuring Passwordless Sudo (NOPASSWD)
In automated CI/CD environments, deployment scripts, or backup automation jobs, requiring an interactive password prompt breaks unattended workflows.
You can configure the NOPASSWD directive for specific commands or all commands:
Open your drop-in file and specify the passwordless exemption:
# Edit drop-in file safely:
sudo visudo -f /etc/sudoers.d/username
# Example 1: Full passwordless sudo for automation user:
# username ALL=(ALL:ALL) NOPASSWD: ALL
# Example 2: Restrict passwordless sudo strictly to service restarts:
# username ALL=(ALL:ALL) NOPASSWD: /usr/bin/systemctl restart nginx, /usr/bin/systemctl reload nginx
Step 4: Testing Sudo Access from the Target User Account
Group changes do not take effect in existing terminal sessions; the user must log out and establish a fresh login shell.
Switch to the target user account and test administrative privilege escalation:
Run sudo whoami to verify that the command executes as the root superuser:
# Switch to target user session:
su - username
# Verify active groups in new shell:
id
# Output confirms: groups=1001(username),27(sudo)
# Test privileged command execution:
sudo whoami
# Enter the password for 'username' when prompted.
# Expected output: root
# List all allowed and forbidden sudo commands for the current user:
sudo -l
Step 5: Removing a User from Sudoers
When an employee departs or an account no longer requires administrative elevation, revoke sudo access promptly.
If the user was added via the sudo group, use gpasswd -d or deluser to remove them:
If privilege was granted through a drop-in file in /etc/sudoers.d/, delete the file:
# Remove user from the sudo group:
sudo gpasswd -d username sudo
# Or using deluser:
sudo deluser username sudo
# Remove drop-in configuration file if created:
sudo rm -f /etc/sudoers.d/username
# Verify revocation:
sudo -l -U username
# Output: User username is not allowed to run sudo on this host.
Troubleshooting and Common Errors
When configuring Ubuntu servers, unexpected permission errors, network timeouts, or lock contention can occur. Review these common issues and verified resolutions:
username is not in the sudoers file. This incident will be reported.
Cause: The user has not been added to the sudo group or granted privileges in /etc/sudoers.d/.
Resolution: Log in with an existing administrative user and execute sudo usermod -aG sudo username.
sudo usermod -aG sudo username
visudo: >>> /etc/sudoers.d/custom: syntax error near line 1 <<<
Cause: Typographical error or invalid keyword entered in the sudoers drop-in file.
Resolution: Press 'e' when prompted by visudo to re-edit and correct the syntax, or 'x' to exit without saving corrupt changes.
sudo visudo -c -f /etc/sudoers.d/custom
User still prompted for password after configuring NOPASSWD
Cause: A subsequent rule in /etc/sudoers or a higher-numbered drop-in file in /etc/sudoers.d/ overrides the NOPASSWD directive.
Resolution: Rename the drop-in file with a higher alphabetical priority (e.g. /etc/sudoers.d/99-custom) so it evaluates last.
sudo mv /etc/sudoers.d/custom /etc/sudoers.d/99-custom
Security Best Practices and Hardening
Never edit /etc/sudoers with standard text editors like nano or vim directly; always use sudo visudo to prevent saving syntax errors that lock all administrators out.
Drop-in files in /etc/sudoers.d/ must have file permissions set to 0440 or 0400; sudo ignores files with write permissions or invalid names containing dots or tildes.
Audit active sudo executions regularly by inspecting /var/log/auth.log for suspicious command invocations.
Related Ubuntu Tutorials and Documentation
Deepen your Linux system administration skills with our related production-tested guides:
- How to Create a Sudo User on Ubuntu — Step-by-step tutorial on creating new user accounts with sudo privileges.
- Initial Server Setup on Ubuntu 24.04 LTS — Foundational server hardening and SSH key configuration.
- How to Enable and Disable Root User Account in Ubuntu — Understand Ubuntu root account locking mechanisms and security best practices.
Authoritative References and Official Documentation

The Linux Command Line, 2nd Edition: A Complete Introduction
Frequently Asked Questions
Quick answers to common questions and configurations
usermod -aG sudo grants standard all-or-nothing administrative privileges by making the user a member of the system group. Editing drop-in files in /etc/sudoers.d/ allows fine-grained access control, such as restricting users to specific binaries or configuring NOPASSWD.file.bak or file~), sudo strictly ignores any filename containing a period (.) or ending in a tilde (~).sudo -k.sudo gpasswd -d username sudo and delete any matching files in /etc/sudoers.d/. The user retains their normal login access without administrative powers.Editorial Disclosure: Website Vidya is supported by our readers. When you purchase hosting, tools, or software through our verified partner links, we may earn an affiliate commission at no extra cost to you. We only recommend solutions we trust and rigorously verify.
Rajesh Ray
I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.
Most Read Guides
TrendingUbuntu Server Not Booting? 10 Simple Steps to Fix It Fast
How to Create a 3D Button in HTML and CSS: Easy Steps
How to Install Python and Pip on Ubuntu 24.04 LTS
How to Install Nginx on Ubuntu 24.04
How to Install Python on Ubuntu 24.04 LTS
Related Technical Guides
Tested runbooks, server administration walkthroughs, and troubleshooting guides

Join the Conversation & Perspective
Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.
No comments on this guide yet. Be the first to share your thoughts or ask a question!