๐Ÿ‘ค Users & Groups Basics

Users & Groups Series: Part 1 โ€” Basics  |  Part 2 โ€” Advanced Management  |  Part 3 โ€” sudo & Privileges

Linux User and Group Model

Every process and file on a Linux system is owned by a user and a group. Access control decisions โ€” who can read, write, or execute โ€” are made by comparing ownership and permissions. Understanding users and groups is fundamental to Linux security and administration.

This page covers the core files, concepts, and everyday commands for managing users and groups. Part 2 covers advanced topics like password aging and bulk operations. Part 3 covers sudo and privilege management.

The Key Files

/etc/passwd

One line per user. Contains username, UID, GID, home directory, shell. World-readable.

/etc/shadow

Hashed passwords and password aging info. Root-readable only.

/etc/group

One line per group. Contains group name, GID, member list. World-readable.

/etc/gshadow

Group passwords and administrators. Root-readable only.

/etc/login.defs

Defaults for UID/GID ranges, password aging, home directory creation.

/etc/skel/

Template files copied to new user home directories on creation.

Reading /etc/passwd

username:password:UID:GID:GECOS:home:shell craig:x:1001:1001:Craig Henderson:/home/craig:/bin/bash nginx:x:992:992:Nginx web server:/var/lib/nginx:/sbin/nologin

Fields: username ยท x (password in shadow) ยท UID ยท primary GID ยท GECOS/comment ยท home directory ยท login shell

UID Ranges

RangePurpose
0root โ€” superuser
1โ€“199System accounts (distro-assigned)
200โ€“999System accounts (dynamically assigned by dnf/apt)
1000+Regular user accounts

Examples

1
Query User and Group Information
# Show current user
whoami
id                        # UID, GID, and all group memberships
id craig                  # info for another user

# Show all groups current user belongs to
groups
groups craig

# Query the user database (works with LDAP/NIS too)
getent passwd craig
getent passwd | sort -t: -k3 -n    # all users sorted by UID

# Query group database
getent group craig
getent group | sort -t: -k3 -n    # all groups sorted by GID

# Who is logged in right now
who
w                         # more detail โ€” what they are running
last | head -20           # login history
lastlog                   # last login for every account
id output:
uid=1001(craig) gid=1001(craig) groups=1001(craig),10(wheel),1005(developers)
๐Ÿ’ก getent over cat: Always use getent passwd instead of cat /etc/passwd โ€” getent queries all configured name service sources including LDAP, NIS, and SSSD. On systems with central directory services, cat /etc/passwd only shows local accounts.
2
Create User Accounts โ€” useradd
# Create a basic user (home directory created automatically on RHEL)
sudo useradd alice

# Create with home directory explicitly (required on some Debian systems)
sudo useradd -m alice

# Create with full options
sudo useradd \
    -m \                      # create home directory
    -d /home/alice \          # home directory path
    -s /bin/bash \            # login shell
    -c "Alice Smith" \        # GECOS comment/full name
    -u 1500 \                 # specific UID
    -g staff \                # primary group
    -G developers,sysadmin \  # supplementary groups
    alice

# Create a system account (no home, no login)
sudo useradd -r -s /sbin/nologin appservice

# Set password immediately after creation
sudo passwd alice

# Create user with password in one command (scripting)
sudo useradd -m -p $(openssl passwd -6 'TempPass123!') alice

# Verify creation
getent passwd alice
ls -la /home/alice
RHEL vs Debian defaults differ: On RHEL/Rocky/AlmaLinux, useradd creates the home directory by default (CREATE_HOME yes in /etc/login.defs). On Debian/Ubuntu, you must explicitly pass -m. Always use -m to be safe and portable.
3
Modify User Accounts โ€” usermod
# Change login shell
sudo usermod -s /bin/bash alice
sudo usermod -s /sbin/nologin alice   # disable login

# Change home directory (and move contents)
sudo usermod -d /data/alice -m alice

# Change full name / comment
sudo usermod -c "Alice B. Smith" alice

# Change username
sudo usermod -l alicesmith alice

# Lock an account (prepends ! to password hash)
sudo usermod -L alice
sudo passwd -l alice      # equivalent

# Unlock an account
sudo usermod -U alice
sudo passwd -u alice      # equivalent

# Add user to supplementary groups (APPEND โ€” critical)
sudo usermod -aG wheel alice
sudo usermod -aG developers,sysadmin alice

# Change primary group
sudo usermod -g newgroup alice
โš ๏ธ Always use -aG not just -G. usermod -G groupname user without -a replaces ALL supplementary groups with just the one specified โ€” removing the user from every other group they were in. -aG appends. This is one of the most common accidental privilege removal mistakes in Linux administration.
4
Delete User Accounts โ€” userdel
# Delete user account (keeps home directory)
sudo userdel alice

# Delete user AND home directory AND mail spool
sudo userdel -r alice

# Find files owned by a user before deletion
sudo find / -user alice -ls 2>/dev/null

# Find files by UID (useful after account is deleted)
sudo find / -uid 1500 -ls 2>/dev/null

# Change ownership of orphaned files before deletion
sudo find / -user alice -exec chown newowner {} \;
Best practice before deleting an account:
  1. Lock the account first: sudo usermod -L alice
  2. Find all owned files: sudo find / -user alice -ls
  3. Archive the home directory: sudo tar -czf /archive/alice.tar.gz /home/alice
  4. Transfer ownership of needed files
  5. Delete: sudo userdel -r alice
5
Manage Groups
# Create a group
sudo groupadd developers
sudo groupadd -g 5000 datateam    # specific GID
sudo groupadd -r appservice       # system group

# Add user to a group
sudo usermod -aG developers alice
sudo gpasswd -a alice developers  # alternative

# Remove user from a group
sudo gpasswd -d alice developers

# List group members
getent group developers
grep developers /etc/group

# Change group name
sudo groupmod -n devteam developers

# Change group GID
sudo groupmod -g 5001 developers

# Delete a group
sudo groupdel developers

# Set group administrators (can add/remove members)
sudo gpasswd -A alice developers
๐Ÿ’ก newgrp โ€” switch primary group for current session:
newgrp developers
This starts a new shell with developers as the primary group. Files created in this shell will be owned by the developers group. Exit the shell to return to your original primary group.
6
Password Management โ€” passwd and chage
# Set/change a user's password
sudo passwd alice

# Force password change on next login
sudo passwd -e alice
sudo chage -d 0 alice    # equivalent

# Lock / unlock account
sudo passwd -l alice
sudo passwd -u alice

# Show password aging info
sudo chage -l alice

# Set password expiry (90 days)
sudo chage -M 90 alice

# Set account expiry date
sudo chage -E 2026-12-31 alice

# Set warning before expiry (7 days)
sudo chage -W 7 alice
chage -l alice output:
Last password change : Apr 25, 2026 Password expires : Jul 24, 2026 Password inactive : never Account expires : never Minimum number of days between change : 0 Maximum number of days between change : 90 Number of days of warning before expiry : 7
7
File Ownership and Permissions
# Show file ownership
ls -la /home/alice/
stat /home/alice/myfile.txt

# Change file owner
sudo chown alice myfile.txt
sudo chown alice:developers myfile.txt   # owner and group
sudo chown :developers myfile.txt        # group only

# Change recursively
sudo chown -R alice:developers /var/www/myapp/

# Change group only
sudo chgrp developers myfile.txt
sudo chgrp -R developers /var/www/myapp/

# Set permissions
chmod 644 myfile.txt     # rw-r--r--
chmod 755 myscript.sh    # rwxr-xr-x
chmod 700 ~/.ssh         # rwx------
chmod 600 ~/.ssh/id_rsa  # rw-------

# Set group sticky bit on directory
# (new files inherit the directory's group)
sudo chmod g+s /var/www/myapp/
sudo chmod 2775 /var/www/myapp/
The setgid bit on directories (chmod g+s) is essential for shared project directories. Without it, files created in /var/www/myapp/ are owned by the creating user's primary group โ€” which may not be developers. With setgid, all new files inherit the directory's group automatically.
8
New Employee Onboarding Script

A practical script that brings together the full user creation workflow:

#!/bin/bash
# new-employee.sh โ€” provision a new user account

USERNAME=$1
FULLNAME=$2
DEPT_GROUP=$3    # e.g., developers, sysadmin, finance

if [[ -z "$USERNAME" || -z "$FULLNAME" || -z "$DEPT_GROUP" ]]; then
    echo "Usage: $0 username 'Full Name' department_group"
    exit 1
fi

# Create user account
sudo useradd \
    -m \
    -s /bin/bash \
    -c "$FULLNAME" \
    -G "$DEPT_GROUP",users \
    "$USERNAME"

# Force password change on first login
sudo chage -d 0 "$USERNAME"

# Set a temporary password
TEMP_PASS="Welcome$(date +%Y)!"
echo "$USERNAME:$TEMP_PASS" | sudo chpasswd

# Set up SSH directory
sudo mkdir -p /home/"$USERNAME"/.ssh
sudo chmod 700 /home/"$USERNAME"/.ssh
sudo chown "$USERNAME":"$USERNAME" /home/"$USERNAME"/.ssh

# Add to wheel/sudo if sysadmin
if [[ "$DEPT_GROUP" == "sysadmin" ]]; then
    sudo usermod -aG wheel "$USERNAME"
    echo "Note: $USERNAME added to wheel group"
fi

echo "Account created: $USERNAME ($FULLNAME)"
echo "Temp password:   $TEMP_PASS"
echo "Must change password on first login."

Quick Reference

CommandWhat it does
id [user]Show UID, GID, and group memberships
groups [user]List group memberships
whoamiCurrent username
getent passwd userQuery user from all sources (local + LDAP)
getent group groupQuery group from all sources
useradd -m -s /bin/bash userCreate user with home and bash shell
useradd -r -s /sbin/nologin svcCreate system/service account
passwd userSet or change password
usermod -aG group userAdd user to supplementary group
usermod -L userLock account
usermod -U userUnlock account
usermod -s /sbin/nologin userDisable login shell
userdel -r userDelete user and home directory
groupadd groupCreate a group
gpasswd -a user groupAdd user to group
gpasswd -d user groupRemove user from group
groupdel groupDelete a group
chage -l userShow password aging info
chage -M 90 userSet 90-day password expiry
chown user:group fileChange file owner and group
chmod g+s dirSet setgid on directory

← Back to Users-Groups Index ↑ Back to EXPANDED