Website Vidya
SecurityStep-by-Step Guide

How to Create a Sudo User on Ubuntu

Learn how to create a sudo user on ubuntu with adduser, assign sudo group membership, verify privileges, and configure passwordless sudo safely.

RR
Rajesh RayLead Systems Educator
Published:
5 min read
PDF Cheat Sheet
How to Create a Sudo User on Ubuntu
AI Executive SummaryWebsiteVidya AI

5 min read

Learn how to create a sudo user on ubuntu with adduser, assign sudo group membership, verify privileges, and configure passwordless sudo safely.

Key Highlights
  • Prerequisites and Supported Releases
  • Step 1: Creating a New User with adduser
  • Step 2: Adding the User to the Sudo Group
Grounded in full engineering runbookWebsiteVidya AI Engine

Linux security principles mandate that daily administrative operations should never be performed logged in directly as the root superuser.

Instead, system administrators should operate under a regular user account and escalate privileges on demand using the sudo (superuser do) command.

The sudo mechanism creates an auditable trail of administrative actions in system logs and prevents accidental destructive commands from executing without explicit confirmation.

Ubuntu is designed around this security model out of the box, delegating administrative privileges to members of the sudo system group.

Advertisement

In this guide, we demonstrate how to create a sudo user on ubuntu step-by-step using modern commands, security best practices, and verification steps.

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:

Advertisement
  • A server or workstation running Ubuntu 24.04 LTS or Ubuntu 22.04 LTS.
  • Access to an existing account with sudo privileges or direct root console access.
  • A basic terminal connection via SSH or local console.

Step 1: Creating a New User with adduser

Ubuntu provides two utilities for user creation: useradd (a low-level binary) and adduser (a friendly interactive Perl script).

Always prefer adduser because it automatically creates the user's home directory, sets appropriate ownership permissions, copies skeleton configuration files, and prompts for a secure password.

Run the following command as root or with sudo to create a user named developer:

sudo adduser developer

# Terminal execution prompt:
Adding user `developer' ...
Adding new group `developer' (1002) ...
Adding new user `developer' (1002) with group `developer (1002)' ...
Creating home directory `/home/developer' ...
Copying files from `/etc/skel' ...
New password: 
Retype new password: 
passwd: password updated successfully
Changing the user information for developer
Enter the new value, or press ENTER for the default
	Full Name []: Deployment Engineer
	Room Number []: 
	Work Phone []: 
	Home Phone []: 
	Other []: 
Is the information correct? [Y/n] Y

Step 2: Adding the User to the Sudo Group

On Ubuntu, the /etc/sudoers file includes a predefined rule granting all members of the sudo group full administrative rights:

Advertisement

%sudo ALL=(ALL:ALL) ALL

To grant administrative rights, simply add your new user to this group using the usermod utility with the append (-a) and group (-G) flags:

# Append the developer user to the sudo group:
sudo usermod -aG sudo developer

# Verify that group membership has taken effect:
id developer

# Output showing membership in group 27 (sudo):
uid=1002(developer) gid=1002(developer) groups=1002(developer),27(sudo)

Step 3: Verifying Sudo Privileges

Always test the new account's ability to escalate privileges before logging out of your current session.

Switch to the new user account using the su (substitute user) command with the login shell flag (-):

# Switch user session to developer:
su - developer

# Execute a privileged test command using sudo:
sudo whoami

# Terminal prompt:
[sudo] password for developer: 
root

Testing Sudo Command Execution

When prompted, enter the password you assigned to the developer account.

If the command returns root, your user has successfully authenticated and elevated privileges through the sudoers subsystem.

Understanding Sudo Shell Environments and Flags

When executing commands with sudo, subtle differences exist between running one-off commands and opening interactive administrative shells.

Executing sudo -i launches an interactive login shell, resetting environment variables and setting the current working directory to /root.

Conversely, sudo -s runs your current user shell as root while preserving your current working directory and selectively forwarding variables configured in env_keep.

# Standard one-off command execution:
sudo systemctl restart nginx

# Interactive root login shell (cleans environment):
sudo -i

# Interactive shell preserving working directory:
sudo -s

# Check current effective user ID:
id -u
# 0

Auditing Sudo Execution Logs with journalctl

Security compliance frameworks require auditing which users execute privileged commands and when.

Every invocation of sudo records a syslog event containing the user identity, terminal TTY, working directory, and exact command string.

Query these audit events in real time using journalctl:

# Inspect recent sudo command invocations:
sudo journalctl _COMM=sudo -n 25

# Or query the traditional authorization log:
sudo grep sudo /var/log/auth.log | tail -n 20

User Offboarding: Revoking Privileges and Deleting Accounts

When an administrator or contractor leaves an organization, their access must be terminated immediately to maintain system integrity.

First, terminate active user processes and revoke their sudo group membership, or lock the account password entirely:

If the account is permanently decommissioned, remove the account and archive their home directory:

# Lock user password immediately:
sudo passwd -l developer

# Kill all running processes owned by the user:
sudo pkill -u developer

# Remove from sudo group:
sudo gpasswd -d developer sudo

# Delete user account and home directory permanently:
sudo userdel -r developer

Configuring Custom Sudo Rules via /etc/sudoers.d/

Never edit the main /etc/sudoers file directly using standard text editors, as any syntax error can completely lock all users out of administrative access.

Instead, place custom drop-in configuration snippets inside the /etc/sudoers.d/ directory using the syntax-validating visudo utility.

For example, to configure passwordless sudo execution for automation scripts or deployment runners:

# Safely edit a custom sudoers snippet:
sudo visudo -f /etc/sudoers.d/developer

# Add the following directive to permit passwordless execution:
developer ALL=(ALL) NOPASSWD:ALL

# Save and exit. Visudo automatically validates syntax before writing to disk.

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:

user is not in the sudoers file. This incident will be reported.

Cause: The user account has not been added to the sudo group, or group membership has not refreshed in the active login shell.

Resolution: Add the user to the sudo group using an active root session, and ensure the user logs out and logs back in.

sudo usermod -aG sudo <username>

syntax error in /etc/sudoers.d/filename

Cause: A manual edit introduced a malformed rule into a sudoers drop-in file.

Resolution: Use pkexec or boot into single-user recovery mode to remove or correct the file with visudo.

sudo visudo -c -f /etc/sudoers.d/filename

Security Best Practices and Hardening

Only grant sudo privileges to users who strictly require system administration access.

Avoid granting NOPASSWD:ALL to user accounts accessible via password authentication over public SSH.

Review the audit trail of sudo commands in /var/log/auth.log periodically to monitor administrative activities.

Deepen your Linux system administration skills with our related production-tested guides:

Authoritative References and Official Documentation

Frequently Asked Questions

Quick answers to common questions and configurations

4 FAQs
adduser is a friendly interactive Perl wrapper that automatically creates home directories, sets default shells, copies skeleton configuration files, and prompts for passwords. useradd is a low-level system binary that requires manual flags for home directories and shells.
visudo locks the sudoers file against concurrent edits and conducts strict syntax checking before saving changes. If you make a syntax error, visudo warns you and prevents saving, saving you from catastrophic administrative lockouts.
By default on Ubuntu, sudo caches your credentials for 15 minutes per terminal session. You can customize this timeout by setting Defaults env_reset, timestamp_timeout=30 using visudo.
Remove the user from the sudo group using the gpasswd utility: sudo gpasswd -d username sudo. The user will immediately lose administrative escalation privileges.
RR
Written byVerified Author

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.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout