Kahibaro
Discord Login Register

User and Group Administration

Overview

User and group administration is about controlling who can log in or run processes on a system and what they are allowed to do. At the intermediate level, this moves beyond just running sudo and starts to include:

This chapter focuses on local user and group management using standard tools. Authentication policies and deeper security controls are handled in separate chapters.

User and Group Concepts (Intermediate View)

At this level, you should already know what a “user” and a “group” is conceptually. What matters now is:

You’ll also see a distinction between:

Key Files for User and Group Information

These files are central to understanding and troubleshooting user and group administration on a single system.

`/etc/passwd`

Stores basic user account information. Each line is one user, with fields separated by ::

username:x:UID:GID:comment:home_directory:login_shell

Example:

alice:x:1000:1000:Alice Smith:/home/alice:/bin/bash

Important details:

The second field, often x, used to contain the encrypted password historically. Modern systems store password hashes in /etc/shadow instead.

`/etc/shadow`

Contains sensitive authentication information (password hashes and related data). Only root and certain privileged processes can read it.

Typical line format:

username:password_hash:last_change:min:max:warn:inactive:expire:reserved

You typically don’t edit this file by hand. Instead, you use tools like passwd, chage, or account management commands.

Key points:

`/etc/group`

Defines groups and their members. Format:

group_name:x:GID:user1,user2,user3

Example:

developers:x:1001:alice,bob

Important points:

`/etc/gshadow` (if present)

Similar to /etc/shadow, but for groups. It can store group passwords (rarely used today) and is usually managed via group-related commands rather than manual editing.

UID and GID Management

Understanding and planning UID/GID ranges is important, especially when you manage multiple systems.

Common UID and GID Ranges

Typical conventions (exact numbers may vary by distribution):

These ranges can vary (e.g. some systems start regular users at 500), but the idea is the same: low numbers are reserved for system use.

Why Consistent UIDs and GIDs Matter

Because ownership is stored by ID, not name:

On standalone machines, you mostly rely on the distribution’s defaults. On fleets of servers, you may need to explicitly assign UIDs/GIDs when creating accounts.

Creating and Managing Users

The main administrative tools for user management are:

Exactly which options exist can vary slightly between distros, so checking man pages on your system is important.

Creating a User with `useradd`

Basic automatic user creation (common syntax):

sudo useradd -m -s /bin/bash alice
sudo passwd alice

Key options you’ll commonly use:

Example: Creating a User for a Human

sudo useradd -m -s /bin/bash -c "Alice Smith" -G wheel,developers alice
sudo passwd alice

Example: Creating a Non‑Login (Service) User

sudo useradd -r -s /usr/sbin/nologin -d /var/lib/myapp -c "MyApp Service" myapp

Typical options for service accounts:

Editing Users with `usermod`

Use usermod to change existing user attributes.

Common options:

Examples

Add alice to extra groups:

sudo usermod -aG docker,developers alice

Change alice’s login shell:

sudo usermod -s /usr/bin/zsh alice

Move home directory to another path:

sudo usermod -d /srv/users/alice -m alice

Deleting Users with `userdel`

Two main ways to remove a user:

  sudo userdel alice
  sudo userdel -r alice

Points to consider:

Creating and Managing Groups

Group management uses:

Creating Groups with `groupadd`

Basic group creation:

sudo groupadd developers

Useful options:

Coordinating GIDs matters, just like UIDs, in multi‑machine setups.

Modifying Groups with `groupmod`

You can rename or change GID:

sudo groupmod -n devteam developers
sudo groupmod -g 2000 devteam

Be cautious with changing GIDs on a live system because existing files may still have the old GID set; you’ll need to adjust filesystem ownerships accordingly.

Adding and Removing Users from Groups

Two common approaches:

  1. Using usermod:
    • Add alice to developers:
     sudo usermod -aG developers alice
     sudo usermod -G developers,sysadmins alice
  1. Using gpasswd:
    • Add alice to developers:
     sudo gpasswd -a alice developers
     sudo gpasswd -d alice developers

Choose whichever interface fits your habits; under the hood they adjust /etc/group (and /etc/gshadow when present).

Deleting Groups with `groupdel`

To delete a group:

sudo groupdel developers

Things to consider:

Default Settings for New Users

When you create a new user, the system uses several defaults:

These are controlled by configuration and templates.

`/etc/default/useradd` (or Distribution Equivalent)

On many systems, useradd uses a config file (often /etc/default/useradd or /etc/login.defs) to get defaults such as:

Use useradd -D to view current defaults on many distributions.

`/etc/skel` — Skeleton Directory

When a user is created with -m, the contents of /etc/skel are copied into their new home directory.

Example:

sudo cp /path/to/custom/.bash_aliases /etc/skel/

Future users created with useradd -m will get this file copied into their home.

Managing Passwords and Account States

Beyond simply setting a password, you often need to lock/unlock accounts and manage password aging.

Setting and Changing Passwords with `passwd`

  sudo passwd alice
  sudo passwd -e alice
  sudo passwd -l alice
  sudo passwd -u alice

Locking with passwd -l modifies the password hash (prefixing it with ! in /etc/shadow).

Account Locking vs Password Locking

There are multiple ways to prevent a user from logging in:

You can combine approaches depending on security requirements.

Password Aging with `chage`

chage interfaces with the password aging fields in /etc/shadow.

Common operations:

  sudo chage -l alice
  sudo chage -M 90 alice
  sudo chage -m 1 alice
  sudo chage -W 7 alice
  sudo chage -E 2025-12-31 alice

Password and account policies are discussed in more depth in the dedicated Password Policies chapter; here you only need enough to manage individual accounts.

System vs Human Users

On a running system, you’ll see many accounts that never log in interactively.

Characteristics of System Accounts

Typically:

Examples might include www-data (web server), postgres (database), or sshd.

When you install packages, they often create these accounts automatically. You should:

Creating Custom Service Accounts

For your own services, it’s best practice to use a dedicated account instead of running as root.

Example:

sudo useradd -r -s /usr/sbin/nologin -d /var/lib/myapp -c "MyApp Service" myapp
sudo mkdir -p /var/lib/myapp
sudo chown myapp:myapp /var/lib/myapp

Then configure your service (e.g. in a systemd unit) to run as User=myapp and Group=myapp.

Inspecting and Querying Users and Groups

You’ll often need to check account details or find which groups a user belongs to.

Listing Users and Groups

  getent passwd
  getent group

getent is preferred over directly reading /etc/passwd or /etc/group because it works with centralized identity services too (LDAP, NIS, etc.), not just local files.

Checking a Single User

  getent passwd alice
  groups alice
  id alice

id shows:

Checking File Ownership vs Users

Sometimes you need to correlate file ownership with accounts:

  sudo find / -uid 1005 -ls
  # or by name (resolved via current user database)
  sudo find / -user alice -ls

This is helpful when:

Handling Common Administrative Tasks

Here are some typical real-world tasks that tie concepts together.

Onboarding a New User

Example workflow for adding a new developer:

  1. Create user with home directory and shell:
   sudo useradd -m -s /bin/bash -c "Jane Doe" jane
  1. Set initial password and force change on first login:
   sudo passwd jane
   sudo passwd -e jane
  1. Add to required groups:
   sudo usermod -aG developers,docker jane
  1. Verify:
   id jane

Offboarding a User

Example workflow when a user leaves:

  1. Lock the account immediately:
   sudo passwd -l jane
   # or
   sudo usermod -L jane
  1. Optionally set an expire date in the near future:
   sudo chage -E 2025-01-01 jane
  1. Archive home directory:
   sudo tar czf /var/archives/jane-home.tar.gz /home/jane
  1. Reassign ownership of important files (if needed) to another user.
  2. Remove the account and home directory once safe:
   sudo userdel -r jane

Converting a Regular Account into a Service Account

If a user account is no longer used interactively but needs to run background services:

  1. Remove interactive shell:
   sudo usermod -s /usr/sbin/nologin serviceuser
  1. Lock the password:
   sudo passwd -l serviceuser
  1. Ensure the user owns only the directories needed for the service.

Integration with Centralized Authentication (High-Level)

While this chapter focuses on local user and group administration, it’s important to understand how it fits into larger setups:

Best practices in mixed environments:

Details of configuring LDAP/AD and PAM are covered in other chapters, but everything you learned here about interpreting and managing local accounts still applies as a foundation.

Summary

In this chapter, you learned to:

These skills form the core of day‑to‑day Linux system administration for handling identities and access control at the account level.

Views: 19

Comments

Please login to add a comment.

Don't have an account? Register now!