Configure Linux user and group management with useradd, usermod and advanced account security

Beginner 25 min Apr 03, 2026 29 views
Ubuntu 24.04 Ubuntu 22.04 Debian 12 AlmaLinux 9 Rocky Linux 9 Fedora 41

Learn to manage Linux users and groups with useradd, usermod, and groupadd commands. This tutorial covers creating accounts, setting permissions, configuring password policies, and implementing security best practices for production systems.

Prerequisites

  • Root or sudo access
  • Basic Linux command line knowledge

What this solves

Linux user and group management is essential for system security and access control in production environments. This tutorial teaches you to create, modify, and secure user accounts using native Linux commands like useradd, usermod, and groupadd, while implementing proper permission structures and password policies.

Understanding Linux users and groups

Linux uses a multi-user system where each user has a unique identifier (UID) and belongs to one or more groups (GID). The system distinguishes between system users (UID 1-999) for services and regular users (UID 1000+) for human access.

Key files that control user management include:

  • /etc/passwd - User account information
  • /etc/shadow - Encrypted passwords and password policies
  • /etc/group - Group definitions and memberships
  • /etc/gshadow - Group passwords and administrators
Note: Always use the command-line tools instead of editing these files directly to prevent corruption and maintain system consistency.

Step-by-step user management setup

Install user management utilities

Install additional user management tools that provide enhanced functionality beyond the basic commands.

sudo apt update
sudo apt install -y passwd adduser libpam-pwquality
sudo dnf update -y
sudo dnf install -y passwd shadow-utils libpwquality

Create a new user with useradd

Create a basic user account with useradd. This command provides fine-grained control over user creation parameters.

sudo useradd -m -s /bin/bash -c "Application User" appuser
sudo passwd appuser

The -m flag creates a home directory, -s sets the shell, and -c adds a comment describing the user.

Create a user with specific UID and group

For system consistency, you may need to specify exact UID values and primary groups.

sudo groupadd -g 2001 developers
sudo useradd -m -u 2001 -g developers -s /bin/bash -c "Developer Account" devuser
sudo passwd devuser

Create a system user for services

System users run services and applications without login capabilities. They use lower UID ranges and typically have no home directory or shell access.

sudo useradd -r -s /usr/sbin/nologin -c "Web Service User" -d /var/lib/webservice webservice
sudo mkdir -p /var/lib/webservice
sudo chown webservice:webservice /var/lib/webservice
sudo chmod 755 /var/lib/webservice

Configure user account expiration

Set account expiration dates for temporary users or security compliance.

sudo useradd -m -s /bin/bash -e 2024-12-31 -c "Temporary User" tempuser
sudo passwd tempuser
sudo chage -l tempuser

Modify existing users with usermod

Use usermod to change user properties after creation, such as adding to groups or changing the home directory.

sudo usermod -aG sudo,developers appuser
sudo usermod -s /bin/zsh appuser
sudo usermod -l newname appuser
sudo usermod -d /home/newname -m newname

Create and manage groups

Groups organize users and simplify permission management. Create groups for different roles and responsibilities.

sudo groupadd webadmins
sudo groupadd -g 3001 dbusers
sudo gpasswd -a appuser webadmins
sudo gpasswd -a devuser dbusers
sudo groups appuser

Configure password policies

Set strong password requirements and aging policies for enhanced security.

minlen = 12
minclass = 3
maxrepeat = 2
maxsequence = 3
dcredit = -1
ucredit = -1
lcredit = -1
ocredit = -1
dictcheck = 1

Configure password aging policies

Set password expiration and change requirements using chage command.

sudo chage -M 90 -m 7 -W 14 appuser
sudo chage -I 30 appuser
sudo chage -l appuser

This sets maximum age to 90 days, minimum age to 7 days, warning period to 14 days, and inactive period to 30 days.

Set default user creation policies

Configure system-wide defaults for new user creation.

GROUP=100
HOME=/home
INACTIVE=30
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

Configure login definitions

Set system-wide login policies including UID ranges and password aging.

PASS_MAX_DAYS 90
PASS_MIN_DAYS 7
PASS_WARN_AGE 14
UID_MIN 1000
UID_MAX 60000
GID_MIN 1000
GID_MAX 60000
CREATE_HOME yes
UMASK 022

Advanced user account security

Lock and unlock user accounts

Temporarily disable accounts without deleting them for security incidents or maintenance.

sudo usermod -L appuser
sudo passwd -l appuser
sudo usermod -U appuser
sudo passwd -u appuser

Monitor user login activity

Track user login attempts and system access for security monitoring.

sudo lastlog
sudo last -10
sudo faillog -u appuser
sudo faillog -r -u appuser

Set user resource limits

Configure system resource limits to prevent resource exhaustion attacks. This integrates with our user session limits tutorial for comprehensive resource management.

appuser soft nproc 100
appuser hard nproc 150
appuser soft nofile 1024
appuser hard nofile 2048
@developers soft nproc 200
@developers hard nproc 300

Configure sudo access

Grant specific users administrative privileges with controlled access.

sudo visudo
appuser ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart nginx
devuser ALL=(ALL) PASSWD: /usr/sbin/service, /usr/bin/systemctl
%webadmins ALL=(ALL) /var/log/, /usr/sbin/logrotate

Verify your user management setup

Test your user configuration and verify security policies are working correctly.

id appuser
groups appuser
sudo chage -l appuser
sudo passwd -S appuser
sudo faillog -u appuser
getent passwd appuser
getent group developers
sudo -l -U appuser

Check that proper file permissions are set for user directories:

ls -la /home/appuser
sudo ls -la /var/lib/webservice
stat -c "%n %a %U:%G" /home/appuser

File permissions and ownership best practices

Understanding Linux file permissions is crucial for secure user management. The permission system uses three categories: owner, group, and others, with read (4), write (2), and execute (1) permissions.

Never use chmod 777. It gives every user on the system full access to your files. Instead, fix ownership with chown and use minimal permissions like 644 for files and 755 for directories.

Common permission patterns for user directories:

  • User home directories: 755 (drwxr-xr-x) - Owner full access, others can read and execute
  • Private user files: 644 (-rw-r--r--) - Owner read/write, others read-only
  • Executable scripts: 755 (-rwxr-xr-x) - Owner full access, others read and execute
  • SSH private keys: 600 (-rw-------) - Owner read/write only
  • Shared group directories: 2775 (drwxrwsr-x) - Group sticky bit for shared access

For more detailed information on file permissions, see our file permissions tutorial.

Common issues

SymptomCauseFix
useradd: user already existsUsername conflicts with existing userCheck with getent passwd username and choose different name
Permission denied accessing user filesIncorrect ownership or permissionsFix with sudo chown user:group file and proper chmod
User cannot loginAccount locked or expiredCheck with sudo chage -l username and unlock with sudo usermod -U username
Group membership not workingUser not properly added to groupAdd with sudo gpasswd -a username groupname and user must re-login
Password policy not enforcedPAM modules not configuredInstall libpam-pwquality and configure /etc/security/pwquality.conf
SSH key authentication failsWrong permissions on .ssh directorySet chmod 700 ~/.ssh and chmod 600 ~/.ssh/id_rsa
Sudo access deniedUser not in sudoers configurationAdd user to sudo group or configure in /etc/sudoers.d/
Home directory not createdMissing -m flag in useraddCreate manually with sudo mkdir /home/user && sudo chown user:user /home/user

Next steps

Automated install script

Run this to automate the entire setup

#linux user management #useradd #usermod #linux groups #user security

Need help?

Don't want to manage this yourself?

We handle infrastructure for businesses that depend on uptime. From initial setup to ongoing operations.

Talk to an engineer