Kahibaro
Discord Login Register

Creating and removing users

Understanding the Scope of User Creation and Removal

This chapter focuses on how to create and remove user accounts on a Linux system using common administrative tools. Concepts like what a “user” is, how /etc/passwd works, and basic UID/GID meanings are assumed to be known from the parent chapter; here we focus on day‑to‑day commands and practical workflows.

We’ll cover:

All commands dealing with system accounts normally require root privileges or sudo.

Tools for Managing Users

Most Linux systems provide two main command-line tools:

Red Hat–based systems typically use useradd / userdel only. On Debian/Ubuntu, adduser is preferred for interactive usage, though useradd is also available.

When reading documentation, always check man useradd and man userdel for your exact system.

Creating Users with `useradd`

useradd is a non-interactive tool: you specify most details via options. If you run it with defaults, your system configuration files (e.g. /etc/login.defs, /etc/default/useradd) decide defaults like UID ranges, home directory base, and shell.

Basic pattern:

sudo useradd [options] username

A Simple New User

To create a user with default settings (which may or may not create a home directory, depending on distro):

sudo useradd alice

On many systems, this:

To ensure a home directory is created:

sudo useradd -m alice

The -m (--create-home) option:

Important `useradd` Options You Actually Use

Home Directory: `-m` and `-d`

Example: create a user with a custom home:

sudo useradd -m -d /srv/webadmin webadmin

Default Shell: `-s`

Set the login shell directly:

sudo useradd -m -s /bin/bash alice
sudo useradd -m -s /usr/sbin/nologin backupuser

Common patterns:

Primary Group: `-g` and Additional Groups: `-G`

You can create a user in a pre-existing group:

sudo useradd -m -g developers -G docker,git alice

If the primary group does not exist, useradd will fail unless your system is configured to auto-create user private groups (as a separate step).

User ID (UID): `-u`

Typically you don’t specify the UID manually unless you’re:

To specify a UID:

sudo useradd -m -u 1500 alice

The UID must be unique unless you use -o to allow duplicates (generally a bad idea for regular users).

Comment / Full Name: `-c`

The “comment” field (also called GECOS) often stores a user’s full name:

sudo useradd -m -c "Alice Example" alice

This information appears in /etc/passwd and tools like finger.

System Accounts: `-r`

System accounts are typically:

Example:

sudo useradd -r -s /usr/sbin/nologin -M backup

Common flags for system users:

Expiration Dates: `-e` and Inactive Period: `-f`

You can create accounts that expire automatically:

Example: temporary intern account:

sudo useradd -m -c "Intern - Bob" -e 2025-02-01 internbob

Examples of Common User Creation Scenarios

Standard Human User

sudo useradd -m -s /bin/bash -c "Alice Example" alice
sudo passwd alice

User with Specific Groups

sudo useradd -m -s /bin/bash -c "Dev Alice" \
  -g developers -G docker,git alice
sudo passwd alice

Service Account (No Login)

sudo useradd -r -s /usr/sbin/nologin -M webapp

This account exists only for ownership and separation of privileges.

Creating Users with `adduser` (Debian/Ubuntu)

On Debian and derivatives, adduser is the recommended interactive tool. It is a Perl script that wraps useradd (or similar) and guides you through the process.

Basic call:

sudo adduser alice

It will:

For most interactive usage on Debian/Ubuntu systems, adduser is simpler and safer than memorizing many useradd options.

You can still pass extra options:

sudo adduser --shell /bin/bash --ingroup developers alice

Setting and Managing User Passwords

Creating the account does not always configure a usable password; some distributions leave accounts in a locked state until you set one.

Setting a Password for a New User

Use passwd:

sudo passwd alice

You’ll be prompted to enter the new password twice.

Creating a User Without a Password

You may want a user that authenticates some other way (e.g. SSH keys, service account). Two common patterns:

  sudo useradd -m -s /bin/bash alice
  sudo passwd -l alice   # lock password

A locked password means you cannot log in with a password, but other mechanisms (like SSH key auth) can still work if configured.

Removing Users with `userdel`

userdel removes user accounts at a low level.

Basic usage:

sudo userdel username

By default, this:

Removing a User and Their Home Directory: `-r`

To also remove the user’s home directory and mail spool:

sudo userdel -r alice

This will:

Use this carefully: data in the user’s home will be permanently removed.

Handling Running Processes

userdel fails if the user has running processes. Before deletion:

  ps -u alice
  sudo pkill -u alice

After ensuring no processes remain, re-run userdel.

Files Owned by the User Outside Their Home

userdel -r only removes the user’s home and mail spool, not other files they might own in places like /srv, /var, or shared directories.

To find and optionally reassign or remove such files:

  1. Identify the user’s UID before deletion (e.g. id alice).
  2. After deletion, search for files with that UID:
   sudo find / -uid 1501 -ls
  1. Decide whether to:
    • Reassign ownership to another user:
     sudo chown -R newowner:newgroup /path/to/files

Removing Users with `deluser` (Debian/Ubuntu)

On Debian/Ubuntu, deluser is a higher-level tool with helpful options.

Basic removal:

sudo deluser alice

To remove the user and their home directory:

sudo deluser --remove-home alice

To also remove their owned files in other locations according to configuration:

sudo deluser --remove-all-files alice

deluser behavior is configurable via /etc/deluser.conf, allowing you to define policies like:

Locking and Disabling Accounts vs Removing Them

Sometimes you don’t want to delete the account (which affects file ownership and logs) but simply prevent login.

Locking an Account

Locking the password:

sudo passwd -l alice

Unlocking:

sudo passwd -u alice

You can also expire the account:

sudo usermod -e 2025-01-01 alice

From that date on, the user cannot log in (depending on your authentication configuration).

When to Lock vs Delete

Typical patterns:

Your organization’s policies should define the correct approach.

Batch and Scripted User Creation

On larger systems, you rarely add users one-by-one manually; instead you:

Creating Multiple Users from a File

Example: You have a file new_users.txt:

alice:Alice Example:/bin/bash
bob:Bob Person:/bin/bash

Simple shell script:

#!/bin/sh
while IFS=: read -r username fullname shell; do
  sudo useradd -m -c "$fullname" -s "$shell" "$username"
  # Optionally set an initial password or lock account
  # echo "$username:TempPass123" | sudo chpasswd
done < new_users.txt

This is a minimal example; in practice, you’d add error checking, logging, and integration with your organization’s policies.

Safe Practices for Creating and Removing Users

These practices help keep your system manageable, consistent, and compliant with any audit or security requirements.

Views: 25

Comments

Please login to add a comment.

Don't have an account? Register now!