Website Vidya
Linux & ServersStep-by-Step Guide

How to Add and Remove Users on Ubuntu: Complete Guide

Step-by-step guide to add and remove users on Ubuntu using adduser, deluser, useradd, and userdel, with sudo privileges and home directory tips.

RR
Rajesh RayLead Systems Educator
Published:
6 min read
PDF Cheat Sheet
How to Add and Remove Users on Ubuntu: Complete Guide
AI Executive SummaryWebsiteVidya AI

6 min read

Step-by-step guide to add and remove users on Ubuntu using adduser, deluser, useradd, and userdel, with sudo privileges and home directory tips.

Key Highlights
  • Prerequisites and Supported Releases
  • Step 1: Understanding adduser vs useradd on Ubuntu
  • Step 2: Adding a New User Interactively with adduser
Grounded in full engineering runbookWebsiteVidya AI Engine

Operating system security, multi-tenant collaboration, and least-privilege access control all depend on properly configured user accounts. When you manage server infrastructure, knowing how to add and remove users on ubuntu is essential for onboarding team members, setting up isolated service accounts, and revoking credentials when access is no longer required.

Ubuntu provides two distinct toolchains for account administration: friendly, interactive Perl wrappers (adduser and deluser) and low-level POSIX standard utilities (useradd and userdel). Understanding when and how to deploy each tool ensures you can create fully configured home environments interactively or automate user provisioning within bash scripts and Ansible playbooks.

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.

Advertisement

Before proceeding, ensure you have the following requirements satisfied:

  • An active installation of Ubuntu 24.04 LTS (Server or Desktop).
  • A user account with active sudo administrative privileges.
  • Access to a local terminal or remote SSH session.

Step 1: Understanding adduser vs useradd on Ubuntu

Before executing commands, it is crucial to recognize the architectural difference between Ubuntu's primary user creation commands:

adduser is an interactive Perl script customized for Debian and Ubuntu systems. It automatically creates a home directory, copies skeleton configuration files from /etc/skel, prompts for a password, and sets standard file permissions.

useradd is a low-level compiled system binary available on all Linux distributions. By default, it does not prompt for a password or create a home directory unless explicitly instructed via command-line flags.

Advertisement
Feature / Behavioradduser (High-Level)useradd (Low-Level)
Default Home DirectoryCreated automatically (/home/username)Not created unless -m flag passed
Skeleton Files (/etc/skel)Copied automatically (.bashrc, .profile)Not copied unless -m flag passed
Password AssignmentInteractive prompt during creationAccount created locked without password
UID / GID AllocationSelects next available Debian/Ubuntu IDSelects system defaults
Best Use CaseManual sysadmin and desktop onboardingAutomated shell scripts and CI/CD pipelines

Step 2: Adding a New User Interactively with adduser

The optional user information fields (GECOS metadata) can be left blank by simply pressing Enter. Once confirmed, the user account is active immediately.

# Add a new user named 'alex' interactively
sudo adduser alex

# Sample terminal interaction:
# Adding user 'alex' ...
# Adding new group 'alex' (1001) ...
# Adding new user 'alex' (1001) with group 'alex' ...
# Creating home directory '/home/alex' ...
# Copying files from '/etc/skel' ...
# New password: [Enter strong password]
# Retype new password: [Retype strong password]
# passwd: password updated successfully
# Changing the user information for alex
# Enter the new value, or press ENTER for the default
# 	Full Name []: Alex Morgan
# 	Room Number []: 
# 	Work Phone []: 
# 	Home Phone []: 
# 	Other []: 
# Is the information correct? [Y/n] Y

Step 3: Scripted User Creation Using Low-Level useradd

When automating deployments in CI/CD pipelines, container entrypoints, or shell scripts, interactive prompts are disruptive. The useradd command allows non-interactive user creation.

Use the -m flag to create the home directory and -s to specify /bin/bash as the default login shell.

# Create a user non-interactively with a home directory and bash shell
sudo useradd -m -s /bin/bash devops

# Assign an initial password non-interactively or via standard input
echo "devops:SecureTemporaryPassw0rd!" | sudo chpasswd

# Force the user to change their password upon first login
sudo chage -d 0 devops

Step 4: Granting Administrative Privileges via the sudo Group

Advertisement

On Ubuntu systems, members of the secondary group named sudo are automatically granted administrative rights to execute commands with elevated privileges.

Add the newly created user to the sudo group using the usermod utility with the -aG (append to group) flags.

# Append user 'alex' to the sudo group
sudo usermod -aG sudo alex

# Verify the user's group memberships
id alex

# Expected output:
# uid=1001(alex) gid=1001(alex) groups=1001(alex),27(sudo)

Step 5: Inspecting User Accounts and Identity Databases

Ubuntu tracks local user accounts, encrypted password hashes, and group memberships across standard system configuration files under /etc/.

You can verify account parameters without opening sensitive files directly by using the getent utility.

# Query account details from /etc/passwd
getent passwd alex

# Output structure: username:password_placeholder:UID:GID:GECOS:home_dir:shell
# alex:x:1001:1001:Alex Morgan,,,:/home/alex:/bin/bash

# Inspect password expiration and aging policy with chage
sudo chage -l alex

Step 6: Temporarily Locking and Unlocking User Accounts

When an employee departs or an account exhibits suspicious behavior, you may want to disable access immediately without deleting their files or home directory.

Locking the account places an exclamation mark (!) in front of the encrypted password hash in /etc/shadow, preventing password logins.

# Lock the user account
sudo passwd -l alex

# Expire the account immediately to prevent SSH key access as well
sudo usermod --expiredate 1 alex

# Unlock the user account and restore access
sudo usermod --expiredate "" alex
sudo passwd -u alex

Step 7: Removing Users Safely with deluser

When you are ready to permanently decommission a user account, use deluser. It provides granular controls for cleaning up user files and group associations.

Depending on your retention policy, you can delete only the user account or purge their home directory and mail spool as well.

Option A: Delete User Account While Preserving Files

This removes the user from /etc/passwd and /etc/group, but leaves /home/alex intact on the disk for auditing or backup.

# Remove the user account only
sudo deluser alex

Option B: Delete User Account and Remove Home Directory

This removes the user and permanently erases their home directory and associated mail spool.

# Remove user, home directory, and mail spool
sudo deluser --remove-home alex

Option C: Remove User and All Files Owned Across the Filesystem

To ensure no orphaned files remain anywhere in /tmp, /var, or shared directories, instruct deluser to scan and remove all matching files:

# Purge user and all files owned across the entire system
sudo deluser --remove-all-files alex

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:

userdel: user <username> is currently used by process <pid>.

Cause: The user has active background processes, an open SSH session, or a lingering tmux/screen daemon.

Resolution: Terminate all running processes owned by the user using pkill before re-running deluser.

sudo pkill -u alex && sudo deluser --remove-home alex

useradd: warning: the home directory already exists.

Cause: A previous user with the same name was deleted without removing their home folder.

Resolution: Inspect the existing folder, back up necessary data, and either chown the folder to the new user or delete the legacy directory.

sudo rm -rf /home/alex

User cannot execute sudo commands: '<username> is not in the sudoers file'.

Cause: The user was not appended to the sudo group, or the user has not logged out and back in to refresh group tokens.

Resolution: Append the user to sudo: sudo usermod -aG sudo alex. Have the user log out and log back in.

sudo usermod -aG sudo alex

Security Best Practices and Hardening

Regularly audit /etc/passwd and /etc/shadow for dormant accounts.

Always back up user home directories to a secure archival location before using --remove-all-files to avoid unintended data loss in shared team repositories.

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
Yes. Simply run sudo deluser <username> without the --remove-home flag. The user record is removed from /etc/passwd, but /home/<username> remains on disk intact.
Execute sudo deluser <username> sudo. This strips their administrative sudo privileges while leaving their user account and personal files intact.
If you delete a user without --remove-home or --remove-all-files, their files remain on disk with numerical UID ownership (e.g., owned by UID 1001). You can find orphaned files using find / -nouser.
Inspect /etc/passwd filtering for standard interactive UIDs (typically between 1000 and 60000): awk -F: '($3 >= 1000 && $3 < 60000) {print $1}' /etc/passwd.
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