3.4.2. Creating and removing users
Table of Contents
Understanding user creation and removal
In an intermediate Linux administration context, creating and removing users is about more than just adding a login name. It involves choosing how the account behaves, where its files live, which groups it belongs to, and what happens when the account is no longer needed. This chapter focuses on the practical command line tools and the most common options you will use when managing users on a typical Linux system.
Always verify which distribution you are using and consult man pages like man useradd and man adduser for exact behavior, because defaults and helper tools can differ between systems.
Tools used for user management
Most Linux systems provide a low level tool called useradd and a companion tool userdel for removing users. On some distributions, you will also find a high level interactive tool called adduser which wraps useradd and asks questions in a friendlier way.
When you run these tools, they modify system user databases such as /etc/passwd, /etc/shadow, and group files. Another pair of tools, passwd and chage, are used to set and adjust passwords and password expiry, but the details of those belong in password policy topics.
For the purposes of this chapter, you can think of useradd as the canonical way to create accounts in scripts and automated setups, while adduser is helpful when you create accounts manually.
Creating a basic user account
On a typical system with useradd, the simplest account creation looks like this:
sudo useradd alice
This command creates an entry for alice in the system user database. However, what exactly happens depends on defaults controlled by login.defs and configuration in /etc/default/useradd or similar. On many distributions, the default behavior is to:
Create a private group for the user with the same name as the user.
Assign a default home directory, often /home/alice.
Set a default shell such as /bin/bash.
Populate the home directory with files copied from a skeleton directory, often /etc/skel.
If you run useradd without additional options, you might end up with a user that has no home directory or no password, depending on the distribution defaults. For that reason, administrators often specify their intentions explicitly.
A more explicit and safer pattern is:
sudo useradd -m -s /bin/bash alice
sudo passwd alice
Here -m instructs useradd to create the home directory if it does not exist, and -s sets the login shell. passwd then prompts you to set a password for alice so that the account can be used for interactive logins.
On systems where useradd does not create a home directory by default, forgetting the -m option results in an account that can log in but has no personal directory, which often breaks common workflows and can confuse new users.
Controlling home directories
Home directories are the main place where ordinary users store their own files and configuration. When you create a user, you can choose the location of the home directory with the -d option to useradd.
For example:
sudo useradd -m -d /srv/projects/alice -s /bin/bash alice
This command creates alice with a home directory at /srv/projects/alice. The -m flag instructs the tool to create that directory and populate it with files from the skeleton directory.
The skeleton directory is used to provide default configuration files at account creation. It is usually located at /etc/skel. You can override it with the -k option:
sudo useradd -m -k /etc/skel-developers -s /bin/bash devuser
This copies files from /etc/skel-developers into the new home directory. This pattern is useful if you maintain different template environments for different classes of users, such as developers and regular office users.
Primary and supplementary groups during creation
The group or groups a user belongs to control access to shared files and resources. When you create a user, a default primary group is assigned. On many modern systems, this is a per user group with the same name as the user, such as alice:alice.
You can specify a primary group explicitly with the -g option. The group must already exist:
sudo groupadd staff
sudo useradd -m -g staff -s /bin/bash bob
Now bob has staff as the primary group. This is often used in environments where files created by members of the staff group are meant to be shared.
To add the new user to additional groups at creation time, use the -G option with a comma separated list:
sudo useradd -m -s /bin/bash -G wheel,developers carol
Here carol has a standard primary group (typically carol) and is also a member of the wheel and developers supplementary groups. On some distributions, the wheel group or a similar group name is associated with administrative privileges through sudo.
If you want the primary group to be one of the groups from your list, you must specify it with -g. The -G option only sets supplementary groups, never the primary group.
System accounts versus regular accounts
Not all users on a system correspond to human users. Many services and daemons run under dedicated system accounts. These accounts usually:
Do not have login shells, or use a shell like /usr/sbin/nologin.
Often have no home directory, or a directory that is not used interactively.
Have user IDs in a reserved range for system use.
To create such a system account, you can use the -r option with useradd:
sudo useradd -r -s /usr/sbin/nologin -d /var/lib/myservice myserviceThis command asks the system to allocate a UID in the system range and defines a non interactive shell. The home directory points at a service related directory, which might hold configuration or data but is not meant as a login environment.
Creating clear system accounts for services helps keep privileges separated, which limits the impact of potential vulnerabilities.
Locking and disabling accounts
Sometimes you do not want to remove a user entirely, but you need to prevent logins. This is common when someone leaves a company but their files must remain available. The usual approach is to lock the account.
You can lock an account using the -L option with usermod:
sudo usermod -L aliceThis modifies the password entry in a way that disables password based logins. To unlock the account later, use:
sudo usermod -U alice
Another pattern is to set the login shell to something non interactive such as /usr/sbin/nologin while leaving the password alone:
sudo usermod -s /usr/sbin/nologin aliceThis prevents interactive logins through typical shells, but keeps files and group memberships intact. Other authentication methods or specialized services may still be able to use the account, so you should always check how your environment authenticates users.
Interactive creation with adduser
Some distributions, especially Debian based ones, provide adduser as a friendlier wrapper. Instead of specifying many flags, you run:
sudo adduser aliceThe tool will then ask you a series of questions such as full name, room number, work phone, and more. It will typically:
Create the home directory and copy in skeleton files.
Set the login shell from a default.
Assign a private group.
Prompt you for an initial password.
Since adduser primarily calls useradd under the hood, it respects the same low level configuration files. As an administrator, you should still understand useradd because it is more portable and is often the only option on non Debian derived systems.
Removing user accounts with userdel
When a user no longer needs access to the system, you remove their account with userdel. The most basic command is:
sudo userdel alice
This deletes the user entry from /etc/passwd and related databases, but on many systems it does not delete the home directory by default. The files remain on disk, but their ownership may now show the numeric user ID instead of a name, because the name is no longer recognized.
If you want to remove the account and also delete the home directory and the user's mail spool, you can use:
sudo userdel -r alice
The -r option instructs userdel to remove the home directory contents. This is a destructive action and cannot be reversed easily, so it should be used only when you are sure that no one needs the user's files.
Never run userdel -r on an account if you have not confirmed that all important data has been backed up or migrated. Deleting a home directory removes all configuration, scripts, and personal files inside it.
Handling processes and logged in users during removal
If the user is currently logged in or has running processes, removal becomes more complicated. On some systems, userdel will refuse to remove such an account without a special option. Even if it allows the operation, you can end up with processes that run under an orphan UID that no longer resolves to a user name.
Before removing an account, it is good practice to check for running processes:
ps -u alice
If you see active processes, you can ask the user to log out or terminate those processes in a controlled way. Only once there are no processes running should you proceed with userdel. This avoids inconsistent states and unexpected issues with services or sessions that still rely on the account.
In more complex environments, you might also need to check for cron jobs, systemd units, or other scheduled tasks associated with the user, and disable or remove them separately.
Strategies for preserving or migrating user data
Account removal often has implications beyond simple access control. In many organizations, user home directories contain project files, documentation, and scripts that other team members still need.
One conservative strategy is to:
Lock the account with usermod -L.
Change the shell to a non interactive one.
Leave the home directory intact.
This immediately prevents new logins while giving you time to review or reassign files. After evaluation, you can archive the home directory, for example with tar, and then proceed to remove the account and delete the original directory if desired.
You can also change ownership of important directories to a shared account or another user with chown before removal, but that action belongs to ownership administration topics and is only mentioned here to show how it connects to user removal.
Naming conventions and UID ranges
While the exact ranges are configured in system files, most Linux distributions separate user ID ranges into areas for system users and for human users. When you create system accounts using useradd -r, the tool typically chooses a UID from the system range. For regular user accounts, it chooses from a higher range.
You can override this behavior with the -u option:
sudo useradd -m -u 1050 -s /bin/bash alice
This assigns UID 1050 explicitly. You should only do this if you understand the UID allocation strategy on your system and you are avoiding collisions with existing IDs. It is often used when migrating users from another system where UIDs must remain consistent.
Consistent UIDs are particularly important when multiple machines share home directories over a network filesystem because file ownership is stored numerically, not by user name.
Summary of safe practices
When you create users, specify your intentions explicitly instead of relying completely on distribution defaults. Use -m to create home directories, choose appropriate shells, assign primary and supplementary groups, and decide whether the account is a regular or a system account.
When removing users, decide whether you want to preserve data, and handle running processes and scheduled jobs gracefully. Locking accounts before deletion gives you time to verify that important files have been backed up or reassigned. With these practices, user creation and removal become predictable operations that support both security and continuity on your Linux systems.
Views: 0
Where to Move
Move chapter:
- ☰ 1. PART I — Linux Fundamentals (Beginner)
- ☰ 1.1. What Is Linux?
- ☰ 1.1.1. What is an operating system
- ☰ 1.1.2. History of Unix and Linux
- ☰ 1.1.3. The Linux kernel
- ☰ 1.1.4. GNU and the Free Software Foundation
- ☰ 1.1.5. Open source philosophy
- ☰ 1.1.6. Where Linux is used today
- ☰ 1.1.7. Desktop vs server vs embedded Linux
-
- ☰ 1.2. Choosing a Linux Distribution
- ☰ 1.2.1. What is a distribution?
- ☰ 1.2.2. Package formats (deb, rpm, pacman)
- ☰ 1.2.3. Popular distributions
- ☰ 1.2.3.1. Ubuntu
- ☰ 1.2.3.2. Debian
- ☰ 1.2.3.3. Fedora
- ☰ 1.2.3.4. Arch Linux
- ☰ 1.2.3.5. openSUSE
-
- ☰ 1.2.4. Rolling vs fixed release
- ☰ 1.2.5. Choosing the right distribution
-
- ☰ 1.3. Installing Linux
- ☰ 1.3.1. Creating a bootable USB
- ☰ 1.3.2. Installing on real hardware
- ☰ 1.3.3. Dual booting with Windows
- ☰ 1.3.4. Installing in a virtual machine
- ☰ 1.3.5. Partitioning
- ☰ 1.3.5.1. `/` (root)
- ☰ 1.3.5.2. `/home`
- ☰ 1.3.5.3. Swap
-
- ☰ 1.3.6. First-time setup
-
- ☰ 1.4. First Steps After Installation
- ☰ 1.4.1. Desktop environment basics
- ☰ 1.4.2. Updating the system
- ☰ 1.4.3. Installing essential tools
- ☰ 1.4.4. Setting up drivers
- ☰ 1.4.5. Using the software center
-
- ☰ 1.5. Linux Filesystem Hierarchy
- ☰ 1.5.1. Overview of the root filesystem
- ☰ 1.5.2. Important directories
- ☰ 1.5.2.1. `/etc`
- ☰ 1.5.2.2. `/var`
- ☰ 1.5.2.3. `/usr`
- ☰ 1.5.2.4. `/tmp`
- ☰ 1.5.2.5. `/dev`
- ☰ 1.5.2.6. `/proc`
- ☰ 1.5.2.7. `/sys`
-
- ☰ 1.5.3. File types
- ☰ 1.5.4. Absolute vs relative paths
-
- ☰ 1.6. The Linux Graphical Environment
- ☰ 1.6.1. GNOME
- ☰ 1.6.2. KDE Plasma
- ☰ 1.6.3. XFCE
- ☰ 1.6.4. File managers
- ☰ 1.6.5. System settings
- ☰ 1.6.6. Workspaces and window management
-
-
- ☰ 2. PART II — Command Line Essentials
- ☰ 2.1. Terminal and Shell Basics
- ☰ 2.1.1. What is a shell?
- ☰ 2.1.2. Bash vs Zsh vs Fish
- ☰ 2.1.3. Command structure
- ☰ 2.1.4. Environment variables
- ☰ 2.1.5. Getting help
- ☰ 2.1.6. Command history and autocomplete
-
- ☰ 2.2. Files and Directories
- ☰ 2.2.1. Listing files
- ☰ 2.2.2. Navigating directories
- ☰ 2.2.3. File operations
- ☰ 2.2.4. Wildcards and globbing
- ☰ 2.2.5. Searching files
-
- ☰ 2.3. Viewing and Editing Text Files
- ☰ 2.3.1. Viewing tools
- ☰ 2.3.2. Editing with nano
- ☰ 2.3.3. Introduction to vim
- ☰ 2.3.4. Input/output redirection
-
- ☰ 2.4. Users, Permissions, and Ownership
- ☰ 2.4.1. Users and groups
- ☰ 2.4.2. Permission types (r, w, x)
- ☰ 2.4.3. Changing permissions
- ☰ 2.4.4. Changing ownership
- ☰ 2.4.5. Using sudo
-
- ☰ 2.5. Package Management
- ☰ 2.5.1. Software repositories
- ☰ 2.5.2. APT (Debian/Ubuntu)
- ☰ 2.5.3. DNF (Fedora/RHEL)
- ☰ 2.5.4. Pacman (Arch Linux)
- ☰ 2.5.5. Snap, Flatpak, AppImage
- ☰ 2.5.6. System upgrades
-
- ☰ 2.6. Working with Processes
- ☰ 2.6.1. What is a process?
- ☰ 2.6.2. Viewing processes
- ☰ 2.6.3. Killing processes
- ☰ 2.6.4. Foreground and background jobs
- ☰ 2.6.5. System services and daemons
-
- ☰ 2.7. Introduction to Shell Scripting
- ☰ 2.7.1. Writing your first script
- ☰ 2.7.2. Variables
- ☰ 2.7.3. Conditionals
- ☰ 2.7.4. Loops
- ☰ 2.7.5. Functions
- ☰ 2.7.6. Making scripts executable
-
-
- ☰ 3. PART III — Intermediate System Administration
- ☰ 3.1. Systemd and Service Management
- ☰ 3.1.1. What is systemd?
- ☰ 3.1.2. Managing services (systemctl)
- ☰ 3.1.3. Boot targets
- ☰ 3.1.4. System logs (journalctl)
- ☰ 3.1.5. Creating custom services
-
- ☰ 3.2. Storage and Filesystems
- ☰ 3.2.1. Devices and partitions
- ☰ 3.2.2. Filesystems (EXT4, XFS, Btrfs)
- ☰ 3.2.3. Mounting and unmounting
- ☰ 3.2.4. Disk usage tools
- ☰ 3.2.5. Archiving and compression (tar, gzip, xz)
-
- ☰ 3.3. Networking Fundamentals
- ☰ 3.3.1. IP addressing
- ☰ 3.3.2. Subnets and routing
- ☰ 3.3.3. DNS and hostnames
- ☰ 3.3.4. Networking tools (ip, ss, ping)
- ☰ 3.3.5. Editing network configuration files
-
- ☰ 3.4. User and Group Administration
- ☰ 3.4.1. User information files
- ☰ 3.4.2. Creating and removing users
- ☰ 3.4.3. Password policies
- ☰ 3.4.4. Managing groups
- ☰ 3.4.5. Shell configuration
-
- ☰ 3.5. System Monitoring
- ☰ 3.5.1. CPU and memory monitoring
- ☰ 3.5.2. Disk and I/O monitoring
- ☰ 3.5.3. Log files in /var/log
- ☰ 3.5.4. Boot performance
- ☰ 3.5.5. Monitoring running services
-
- ☰ 3.6. Backup and Restore
- ☰ 3.6.1. Backup strategies
- ☰ 3.6.2. Using rsync
- ☰ 3.6.3. Creating tar archives
- ☰ 3.6.4. Using snapshot systems
- ☰ 3.6.5. Automating backups
-
- ☰ 3.7. Basic System Security
- ☰ 3.7.1. Firewalls (UFW, firewalld)
- ☰ 3.7.2. Authentication policies
- ☰ 3.7.3. SSH security basics
- ☰ 3.7.4. SELinux and AppArmor overview
- ☰ 3.7.5. Intrusion detection basics
-
-
- ☰ 4. PART IV — Advanced Linux Skills
- ☰ 4.1. Advanced Shell Scripting
- ☰ 4.1.1. Script structure
- ☰ 4.1.2. Error handling
- ☰ 4.1.3. Debugging scripts
- ☰ 4.1.4. Scheduled tasks (cron)
-
- ☰ 4.2. The Linux Boot Process
- ☰ 4.2.1. BIOS vs UEFI
- ☰ 4.2.2. Understanding GRUB2
- ☰ 4.2.3. Kernel loading
- ☰ 4.2.4. initramfs
- ☰ 4.2.5. Troubleshooting boot issues
-
- ☰ 4.3. Kernel Management
- ☰ 4.3.1. What is the kernel?
- ☰ 4.3.2. Kernel modules
- ☰ 4.3.3. Kernel parameters
- ☰ 4.3.4. Compiling a custom kernel
- ☰ 4.3.5. Kernel versioning and updates
-
- ☰ 4.4. Advanced Filesystems and Storage
- ☰ 4.4.1. LVM concepts
- ☰ 4.4.2. Logical volumes
- ☰ 4.4.3. RAID levels
- ☰ 4.4.4. Full disk encryption (LUKS)
- ☰ 4.4.5. Snapshots and rollbacks
-
- ☰ 4.5. Network Services
- ☰ 4.5.1. SSH server configuration
- ☰ 4.5.2. NFS
- ☰ 4.5.3. Samba
- ☰ 4.5.4. FTP/SFTP
- ☰ 4.5.5. Firewalls in depth
-
- ☰ 4.6. Logging and Auditing
- ☰ 4.6.1. Systemd logging
- ☰ 4.6.2. Traditional logs in /var/log
- ☰ 4.6.3. Auditd
- ☰ 4.6.4. Log rotation
- ☰ 4.6.5. Creating custom logs
-
- ☰ 4.7. Virtualization and Containers
- ☰ 4.7.1. Virtual machines (KVM/QEMU)
- ☰ 4.7.2. Linux containers (LXC/LXD)
- ☰ 4.7.3. Docker fundamentals
- ☰ 4.7.4. Podman vs Docker
- ☰ 4.7.5. Container networking
-
-
- ☰ 5. PART V — Server Administration (Expert)
- ☰ 5.1. Web Servers
- ☰ 5.1.1. Apache basics
- ☰ 5.1.2. Nginx basics
- ☰ 5.1.3. Virtual hosts
- ☰ 5.1.4. SSL/HTTPS
- ☰ 5.1.5. Reverse proxy concepts
-
- ☰ 5.2. Database Servers
- ☰ 5.2.1. MySQL/MariaDB
- ☰ 5.2.2. PostgreSQL
- ☰ 5.2.3. Database backup strategies
- ☰ 5.2.4. Database hardening
-
- ☰ 5.3. Email Services
- ☰ 5.3.1. SMTP fundamentals
- ☰ 5.3.2. Configuring Postfix
- ☰ 5.3.3. IMAP/POP3 with Dovecot
- ☰ 5.3.4. Spam filtering systems
- ☰ 5.3.5. Email routing concepts
-
- ☰ 5.4. DNS and DHCP
- ☰ 5.4.1. Understanding DNS
- ☰ 5.4.2. Setting up BIND
- ☰ 5.4.3. DNS zones and records
- ☰ 5.4.4. DHCP server configuration
- ☰ 5.4.5. DNS troubleshooting
-
- ☰ 5.5. Load Balancing
- ☰ 5.5.1. HAProxy fundamentals
- ☰ 5.5.2. Nginx as a load balancer
- ☰ 5.5.3. Session persistence
- ☰ 5.5.4. High availability concepts
-
- ☰ 5.6. Clustering and High Availability
- ☰ 5.6.1. Failover principles
- ☰ 5.6.2. Pacemaker
- ☰ 5.6.3. Corosync
- ☰ 5.6.4. Cluster resource management
- ☰ 5.6.5. Distributed filesystems
-
-
- ☰ 6. PART VI — DevOps and Cloud
- ☰ 6.1. Linux for Developers
- ☰ 6.1.1. Compiler toolchains (gcc, make)
- ☰ 6.1.2. Git fundamentals
- ☰ 6.1.3. Debugging tools (gdb, strace)
- ☰ 6.1.4. Build automation (Makefile, CMake)
-
- ☰ 6.2. DevOps on Linux
- ☰ 6.2.1. CI/CD principles
- ☰ 6.2.2. YAML basics
- ☰ 6.2.3. GitHub Actions
- ☰ 6.2.4. GitLab CI/CD
- ☰ 6.2.5. Automated testing
-
- ☰ 6.3. Configuration Management
- ☰ 6.3.1. Ansible basics
- ☰ 6.3.2. Playbooks and inventories
- ☰ 6.3.3. Puppet overview
- ☰ 6.3.4. Chef overview
-
- ☰ 6.4. Infrastructure as Code
- ☰ 6.4.1. Terraform fundamentals
- ☰ 6.4.2. Providers and resources
- ☰ 6.4.3. Modules
- ☰ 6.4.4. Cloud provisioning with Terraform
-
- ☰ 6.5. Cloud Computing with Linux
- ☰ 6.5.1. Linux on AWS
- ☰ 6.5.2. Linux on Azure
- ☰ 6.5.3. Linux on GCP
- ☰ 6.5.4. Cloud networking basics
- ☰ 6.5.5. Scaling and monitoring
-
-
- ☰ 7. PART VII — Master Topics
- ☰ 7.1. Hardening a Linux System
- ☰ 7.1.1. Hardening principles
- ☰ 7.1.2. Kernel hardening
- ☰ 7.1.3. File integrity monitoring
- ☰ 7.1.4. Vulnerability scanning
-
- ☰ 7.2. Forensics and Incident Response
- ☰ 7.2.1. Collecting evidence
- ☰ 7.2.2. Log and file recovery
- ☰ 7.2.3. Analyzing suspicious processes
- ☰ 7.2.4. Incident response workflow
-
- ☰ 7.3. Performance Tuning
- ☰ 7.3.1. CPU tuning
- ☰ 7.3.2. Memory tuning
- ☰ 7.3.3. Disk and I/O optimization
- ☰ 7.3.4. Profiling tools
-
- ☰ 7.4. Writing Linux Tools
- ☰ 7.4.1. Bash-based tools
- ☰ 7.4.2. Python command-line tools
- ☰ 7.4.3. Writing tools in C
- ☰ 7.4.4. Using Linux system APIs
-
- ☰ 7.5. Linux Internals
- ☰ 7.5.1. Process lifecycle
- ☰ 7.5.2. Memory management
- ☰ 7.5.3. Signals and IPC
- ☰ 7.5.4. Namespaces
- ☰ 7.5.5. cgroups
-
- ☰ 7.6. Becoming a Linux Expert
- ☰ 7.6.1. Career paths
- ☰ 7.6.2. Certifications (RHCE, LFCS, LPIC)
- ☰ 7.6.3. Building a homelab
- ☰ 7.6.4. Contributing to open source
-
-
Comments
Please login to add a comment.
Don't have an account? Register now!