Lab 10: Users and Groups Management

Objective

Create and manage Linux users and groups: useradd, usermod, userdel, groupadd, /etc/passwd, /etc/shadow, /etc/group file formats, and the principle of least privilege via service accounts.

Time: 30 minutes | Level: Foundations | Docker: docker run -it --rm ubuntu:22.04 bash


Step 1: Understanding /etc/passwd

cat /etc/passwd | head -5

📸 Verified Output:

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync

Each line: username:password:UID:GID:GECOS:home:shell

  • x in password field = password stored in /etc/shadow

  • UID 0 = root (superuser)

  • /usr/sbin/nologin = account that cannot log in interactively


Step 2: Creating Users

📸 Verified Output:

📸 Verified Output:

📸 Verified Output:

💡 useradd -m creates the home directory. Without -m, no home directory is created. -s /bin/bash sets the login shell. Always specify both on real systems.


Step 3: The /etc/shadow File (Password Storage)

📸 Verified Output:

Format: user:hash:last_change:min_age:max_age:warn:inactive:expire

  • * = account locked (root can't log in with password in this container)

  • ! = no password set (new account before passwd is run)

  • The hash would look like: $6$salt$hashedpassword... (SHA-512)

💡 /etc/shadow is readable only by root (640). If an attacker gets read access to shadow, they can crack the hashes offline with hashcat or john. This is why it has restricted permissions.


Step 4: Creating and Managing Groups

📸 Verified Output:

📸 Verified Output:

📸 Verified Output:

💡 -aG = append to Groups. Without -a, usermod -G devteam alice would replace all of alice's supplementary groups with just devteam. Always use -aG to add groups.


Step 5: Listing Group Members

📸 Verified Output:

📸 Verified Output:


Step 6: Service Accounts (Least Privilege)

📸 Verified Output:

💡 Service accounts should have:

  • -r (system account, UID < 1000 by default)

  • -s /bin/false or /usr/sbin/nologin (no interactive login)

  • No password (locked account)

  • Minimal group memberships

This is why nginx runs as www-data, postgres as postgres, etc. — a compromised service process can only do what that service account is allowed to do.


Step 7: Modifying and Deleting Users

📸 Verified Output:

📸 Verified Output:


Step 8: Capstone — Enterprise User Provisioning Script

📸 Verified Output:


Summary

Command
Purpose

useradd -m -s /bin/bash user

Create user with home + bash shell

useradd -r -s /bin/false svc

Create service account (no login)

usermod -aG group user

Add user to group (append)

usermod -L user

Lock account

usermod -U user

Unlock account

groupadd groupname

Create a group

id user

Show UID, GID, groups

groups user

List user's groups

getent group groupname

Show group members

Last updated