Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 37 ยท Users, Groups and Permissions

Managing Users and Groups: useradd, usermod, passwd

Create, modify, lock and delete Linux users and groups with useradd, usermod, userdel, groupadd, passwd and chage, and set password aging policy with confidence.

Account management is among the most frequent administrator tasks: a new colleague arrives, someone changes teams, someone leaves. The low-level tools useradd, usermod, userdel, groupadd and passwd work on every distribution and edit the files described in the previous lesson safely.

All of these commands need root, so they are shown with sudo.

Creating users

Terminal
sudo useradd -m -s /bin/bash -c "Dana Lee" dana
sudo passwd dana
  • -m creates the home directory and copies files from /etc/skel.
  • -s sets the login shell.
  • -c sets the comment (full name).
  • -G sudo,developers adds supplementary groups at creation.
  • -u 1500 picks a UID; -g sets the primary group.
  • -r creates a system account (low UID, no home by default).

Debian-family systems also provide adduser, a friendlier interactive wrapper. Defaults come from /etc/default/useradd and /etc/login.defs.

A service account example:

Terminal
sudo useradd -r -s /usr/sbin/nologin -d /var/lib/myapp myapp

Changing users

Terminal
sudo usermod -aG docker dana          # append to a group (note the -a!)
sudo usermod -s /bin/zsh dana         # change shell
sudo usermod -l danalee dana          # rename login
sudo usermod -L dana                  # lock the password
sudo usermod -U dana                  # unlock
sudo usermod -e 2026-12-31 dana       # account expiry date

usermod -G group user replaces all supplementary groups. Always use -aG (append) unless you really mean to replace them.

A user must log in again before new group membership applies to their sessions.

Passwords and aging

Terminal
sudo passwd dana              # set password
sudo passwd -l dana           # lock
sudo passwd -e dana           # expire now: must change at next login
sudo chage -l dana            # show aging info
sudo chage -M 90 -m 1 -W 7 dana   # max 90 days, min 1, warn 7

chage edits the fields in /etc/shadow. For policy that applies to all new users, set PASS_MAX_DAYS and friends in /etc/login.defs. Password quality rules belong to PAM (later chapter).

Groups

Terminal
sudo groupadd developers
sudo groupadd -g 2000 ops
sudo gpasswd -a dana developers      # add member
sudo gpasswd -d dana developers      # remove member
sudo groupmod -n devs developers     # rename
sudo groupdel ops
groups dana
id dana

Groups are the main way to share access to files: put a directory's group ownership on developers, give the group write permission and add people to the group (Permissions).

Deleting users

Terminal
sudo userdel dana           # keep home directory
sudo userdel -r dana        # also remove home and mail spool

Before deleting, find files the user owns elsewhere, and think about processes and cron jobs:

Terminal
sudo find / -xdev -user dana 2>/dev/null
sudo pkill -u dana

Often it is better to lock and expire an account first, keep the data for a period, then delete. Files owned by a deleted UID show a bare number in listings; never reuse that UID.

Switching and checking

Terminal
su - dana                   # login shell as dana (needs dana's or root's rights)
sudo -iu dana               # same via sudo
sudo -l -U dana             # what may dana run with sudo?

A typical onboarding flow

flowchart LR
    A["useradd -m"] --> B["passwd or SSH key"]
    B --> C["Add to groups"]
    C --> D["chage: force change"]
    D --> E["Record the change"]

Create, authenticate, authorize, require a password change, document.

A stand-in: building a useradd command

The flags are easy to forget. This model assembles a safe command from a dictionary of choices. It only prints text; nothing is created.

Python Stand-in: build a useradd command

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A stand-in: group membership with -a versus without

This model shows why usermod -G without -a is dangerous.

Python Stand-in: usermod -G versus -aG

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Using -G without -a.
  • Deleting a user without finding their files, cron jobs and processes.
  • Sharing one account between people; you lose accountability.
  • Leaving temporary accounts without an expiry date.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which option creates the home directory with useradd?

    -m creates the home and copies /etc/skel into it.
  2. 2. What does usermod -aG docker dana do?

    -a with -G appends instead of replacing.
  3. 3. Which command forces a user to change the password at next login?

    passwd -e expires the password immediately.
  4. 4. What is a good first step before deleting a leaving user's account?

    Locking preserves data while you check what they own.
  5. 5. Which shell suits a service account?

    Service accounts should not allow interactive logins.

Summary

  • useradd -m, passwd, usermod -aG, userdel -r cover the lifecycle.
  • chage and /etc/login.defs control password aging.
  • Lock first, delete later; hunt for leftover files and processes.
  • Use groups, not shared accounts, to share access.

Next: File Permissions, Ownership and umask