Domain 9Users & GroupsCORE

Managing Users and Groups

Comprehensive guide to RHEL user and group administration, password aging policies, user default environments, and granular sudo privilege escalation.

Domain: Manage users and groups•Updated: 2026-08-30
#Users#Groups#Sudo#PAM#Password Aging#RHCSA#RHEL

Managing Users and Groups

Theory Questions

1. Enhance system security for the root user account.

  • Command(s):
passwd -l root
# AND/OR disable SSH root login in /etc/ssh/sshd_config.d/01-permitrootlogin.conf:
echo "PermitRootLogin no" > /etc/ssh/sshd_config.d/01-permitrootlogin.conf
systemctl reload sshd
  • Why: Locking the root account password (passwd -l) and disabling direct SSH root access forces administrators to log in using unprivileged individual accounts and escalate privileges via sudo. This creates clear audit trails in system logs.
  • Gotchas: Locking the root account before creating an administrative user with verified sudo privileges will lock you out of system administration.
  • Verify:
passwd -S root

(Returns root L indicating the account password is locked)


2. Identify the group required for sudo access on a default RHEL 9 installation.

  • Group Name / Command:
wheel
  • Why: The default /etc/sudoers file in RHEL includes the directive %wheel ALL=(ALL) ALL, automatically granting full administrative privileges to any user who is a member of the wheel supplementary group.
  • Gotchas: Creating custom administrative groups without defining corresponding rules in /etc/sudoers or drop-in files under /etc/sudoers.d/.
  • Verify:
grep -E "^%wheel" /etc/sudoers

3. Configure sudo to allow a user to change passwords for all users except root.

  • Command(s): Create a drop-in file using visudo:
visudo -f /etc/sudoers.d/password-admin

Add the following directive:

user1 ALL=(ALL) /usr/bin/passwd [a-zA-Z0-9]*, !/usr/bin/passwd root, !/usr/bin/passwd ""

Set file permissions:

chmod 0440 /etc/sudoers.d/password-admin
  • Why: Sudo evaluates rules in order, but explicit negations (!) override permissive matches. Excluding /usr/bin/passwd root and /usr/bin/passwd "" prevents the user from changing root’s password or running passwd without arguments (which defaults to changing the calling shell’s password context).
  • Gotchas: Omitting !/usr/bin/passwd "" or !/usr/bin/passwd root. If !/usr/bin/passwd "" is omitted, running sudo passwd with no arguments escalates to target user root and modifies root’s password.
  • Verify:
sudo -l -U user1

4. Use a command with a pipe in a sudo environment.

  • Command(s):
sudo bash -c 'cat /var/log/secure | grep "Failed password"'
  • Why: Standard shell redirection and pipes (|, >) are evaluated by the calling user’s unprivileged shell before sudo executes. Wrapping the pipeline in sh -c or bash -c passes the entire command pipeline to the elevated shell execution context.
  • Gotchas: Writing sudo cat /var/log/secure | grep "Failed password" works because cat is elevated, but running sudo echo "data" > /etc/protected_file fails because the redirect > is executed by the unprivileged shell.
  • Verify:
sudo bash -c 'whoami | tr [a-z] [A-Z]'

5. Set the default location for all new user home directories.

  • Command(s):
# Option A: Modify default configuration file
sed -i 's|^HOME=.*|HOME=/custom_home|' /etc/default/useradd

# Option B: Update defaults via useradd CLI
useradd -D -b /custom_home
  • Why: /etc/default/useradd stores baseline default variables used by useradd when creating new user accounts.
  • Gotchas: Modifying /etc/login.defs expecting home path defaults; /etc/login.defs manages password aging and UID/GID ranges, whereas /etc/default/useradd defines the default base path for home directories.
  • Verify:
useradd -D | grep HOME

6. Get information about password properties (e.g., expiry).

  • Command(s):
chage -l username
  • Why: chage -l lists password aging details, including last password change date, password expiration date, inactive account date, and minimum/maximum days between changes.
  • Gotchas: Using passwd -S username, which only provides a brief status summary rather than detailed expiration dates.
  • Verify:
chage -l username

7. Identify which files are processed when a user starts a login shell.

  • Execution Order:
1. /etc/profile
2. /etc/profile.d/*.sh
3. ~/.bash_profile
4. ~/.bashrc
5. /etc/bashrc
  • Why: Interactive login shells (e.g., SSH logins or su - username) execute /etc/profile and ~/.bash_profile first to set global and user-specific environment variables.
  • Gotchas: Confusing login shells (su - username) with non-login subshells (su username), which skip /etc/profile and ~/.bash_profile and execute only ~/.bashrc and /etc/bashrc.
  • Verify: Insert a temporary echo command into ~/.bash_profile and open a new SSH session.

8. Modify user group membership.

  • Command(s):
# Append a secondary (supplementary) group:
usermod -aG sales joana

# Change primary group:
usermod -g developers joana
  • Why: The -a (append) flag combined with -G adds new supplementary groups without removing the user from existing groups.
  • Gotchas: Running usermod -G sales joana without the -a flag. Omitting -a wipes out all secondary groups assigned to the user, replacing them exclusively with sales.
  • Verify:
id joana

9. List all groups a user is a member of.

  • Command(s):
id username
# OR
groups username
  • Why: id displays the user’s UID, primary group GID, and all supplementary GIDs. groups prints a space-separated list of group names.
  • Gotchas: Checking only /etc/passwd; field 4 of /etc/passwd lists only the user’s primary GID.
  • Verify:
id username

10. Temporarily prevent all non-root users from logging in.

  • Command(s):
echo "System undergoing scheduled maintenance." > /etc/nologin
  • Why: The pam_nologin.so module checks for the existence of /etc/nologin. If present, non-root login attempts are denied and the contents of the file are displayed to the user.
  • Gotchas: Forgetting to remove /etc/nologin (rm -f /etc/nologin) after maintenance is complete, leaving standard users permanently blocked.
  • Verify:
ls -l /etc/nologin

11. Expand the lifetime of the sudo token.

  • Command(s): Edit sudoers configuration via visudo:
visudo -f /etc/sudoers.d/timeout

Add the following directive (e.g., to set timeout to 30 minutes):

Defaults timestamp_timeout=30
  • Why: timestamp_timeout specifies the duration (in minutes) that sudo authentications persist before prompting the user for a password again.
  • Gotchas: Setting timestamp_timeout=0 forces a password prompt for every single command; setting a negative value (-1) keeps the token valid indefinitely for the current terminal session.
  • Verify:
sudo -V | grep "Authentication timestamp timeout"

12. Identify the sudo configuration file.

  • Path / Command:
/etc/sudoers
/etc/sudoers.d/
  • Why: /etc/sudoers is the primary rule file for privilege escalation. Modular drop-in configurations are stored in /etc/sudoers.d/.
  • Gotchas: Editing /etc/sudoers directly using standard text editors like vim or nano instead of using visudo.
  • Verify:
ls -la /etc/sudoers /etc/sudoers.d/

13. Modify a sudo configuration safely.

  • Command(s):
visudo
# OR for drop-in files:
visudo -f /etc/sudoers.d/custom_rule
  • Why: visudo locks the target file against concurrent edits and performs strict syntax validation before saving, preventing syntax errors that could break sudo access across the system.
  • Gotchas: Manually creating files in /etc/sudoers.d/ without running syntax checks (visudo -c).
  • Verify:
visudo -c

14. Recover from a broken sudo configuration when root has no password.

  • Command(s):
# 1. Reboot system and interrupt GRUB menu by pressing 'e'.
# 2. Append 'rd.break' to the end of the line starting with 'linux'.
# 3. Press Ctrl+X to boot into initramfs emergency shell.
# 4. Remount /sysroot as read-write and chroot:
mount -o remount,rw /sysroot
chroot /sysroot

# 5. Repair or remove the invalid file in /etc/sudoers.d/:
rm -f /etc/sudoers.d/broken_file
# OR edit with visudo:
visudo

# 6. Flag SELinux relabeling and exit:
touch /.autorelabel
exit
exit
  • Why: Booting with rd.break halts early initialization before root file system mounts and PAM modules load, dropping you into a root emergency shell to repair configuration files.
  • Gotchas: Omitting touch /.autorelabel after modifying configuration files in /etc/ during an emergency rescue session; SELinux context mismatches can prevent services from starting.
  • Verify: Reboot normally and verify sudo execution.

15. Determine how many groups can be assigned to a user in /etc/passwd.

  • Command / Answer: Exactly 1 (Primary Group GID in field 4).
  • Why: The /etc/passwd structure strictly defines one primary GID per user line (username:x:UID:GID:GECOS:home:shell). All secondary/supplementary group memberships are stored in /etc/group.
  • Gotchas: Attempting to manually add comma-separated GIDs into field 4 of /etc/passwd.
  • Verify:
cat /etc/passwd | grep "^root:"

16. Grant a user access to all admin commands through sudo.

  • Command(s):
usermod -aG wheel username
# OR create a dedicated drop-in file:
echo "username ALL=(ALL) ALL" > /etc/sudoers.d/username
chmod 0440 /etc/sudoers.d/username
  • Why: Adding the account to the wheel group grants full administrative privileges natively under RHEL defaults.
  • Gotchas: Creating custom files in /etc/sudoers.d/ with file permissions other than 0440 or 0400. sudo ignores drop-in files that are world-readable or writable.
  • Verify:
sudo -l -U username

17. Modify /etc/group manually and safely.

  • Command(s):
vigr
# OR for shadow group file:
vigr -s
  • Why: vigr locks /etc/group during edits and performs syntax verification upon saving, preventing race conditions with administrative CLI commands (groupadd, usermod).
  • Gotchas: Editing /etc/group using standard vim while user management commands are actively executing.
  • Verify:
grpck

18. Change user password information.

  • Command(s):
# Change password string:
passwd username

# Change password aging parameters:
chage -M 90 -m 7 -W 14 username
  • Why: passwd updates the encrypted password hash, while chage sets expiration parameters (e.g., -M maximum days, -m minimum days, -W warning days).
  • Gotchas: Confusing password resetting (passwd) with password aging policy enforcement (chage).
  • Verify:
chage -l username

19. Identify the file where user passwords are stored.

  • Path / Command:
/etc/shadow
  • Why: /etc/shadow stores encrypted user password hashes, change timestamps, and aging parameters. It is restricted to read permissions for root only (0000 or 0400).
  • Gotchas: Expecting password hashes inside /etc/passwd; /etc/passwd displays an x placeholder in field 2.
  • Verify:
ls -l /etc/shadow

20. Identify the file where group accounts are stored.

  • Path / Command:
/etc/group
/etc/gshadow
  • Why: /etc/group defines group names, GIDs, and supplementary group member lists. /etc/gshadow stores encrypted group passwords and group administrators.
  • Gotchas: Looking in /etc/passwd to find secondary group lists.
  • Verify:
cat /etc/group

Lab Tasks

Lab 6.1: Shared Group Environment

Task: Create groups sales and account. Create users joana, john, laura, and beatrix with primary private groups matching their usernames. Add joana and john to sales, and laura and beatrix to account. Set a password policy requiring all users to change passwords every 90 days.

  • Command(s):
# Step 1: Create supplementary groups
groupadd sales
groupadd account

# Step 2: Create user accounts (default behavior creates matching primary group)
useradd joana
useradd john
useradd laura
useradd beatrix

# Step 3: Assign supplementary groups
usermod -aG sales joana
usermod -aG sales john
usermod -aG account laura
usermod -aG account beatrix

# Step 4: Configure password aging policy for existing accounts
for user in joana john laura beatrix; do
  chage -M 90 "$user"
done

# Step 5: Configure password policy default for newly created future users
sed -i 's/^PASS_MAX_DAYS.*/PASS_MAX_DAYS   90/' /etc/login.defs
  • Why:

  • groupadd: Creates target supplementary security groups.

  • useradd: Creates standard accounts alongside matching primary private groups (e.g., User joana gets primary group joana).

  • usermod -aG: Appends supplementary groups without overwriting existing memberships.

  • chage -M 90: Enforces 90-day password expiration for existing users.

  • /etc/login.defs: Updates PASS_MAX_DAYS to ensure future users inherit the 90-day password policy automatically.

  • Gotchas:

  • Forgetting the -a flag in usermod -aG, which would wipe out other group memberships.

  • Setting PASS_MAX_DAYS in /etc/login.defs and assuming it retroactively updates existing users. Existing users must be updated explicitly using chage.

  • Verify:

id joana
id laura
chage -l joana | grep "Maximum number of days"
grep PASS_MAX_DAYS /etc/login.defs

Lab 6.2: Sudo Configuration

Task: Create a sudo configuration that allows user bill to manage user properties and passwords, but prevents bill from changing the root user’s password.

  • Command(s):
# Step 1: Ensure user 'bill' exists
id bill &>/dev/null || useradd bill

# Step 2: Create drop-in sudoers file using visudo
visudo -f /etc/sudoers.d/bill

Add the following rules to /etc/sudoers.d/bill:

bill ALL=(ALL) /usr/sbin/useradd, /usr/sbin/usermod, /usr/sbin/userdel, /usr/bin/passwd [a-zA-Z0-9]*, !/usr/bin/passwd root, !/usr/bin/passwd ""

Set required file permissions:

chmod 0440 /etc/sudoers.d/bill
  • Why:

  • /etc/sudoers.d/bill: Isolates custom rules in a modular drop-in file.

  • Allowed binaries: Grants bill access to /usr/sbin/useradd, /usr/sbin/usermod, and /usr/sbin/userdel.

  • Negations (!): !/usr/bin/passwd root blocks explicitly targeted attempts against root, and !/usr/bin/passwd "" blocks passwd calls with empty arguments (which execute in root’s context if elevated).

  • chmod 0440: Enforces mandatory read-only file permissions required by sudo.

  • Gotchas:

  • Forgetting !/usr/bin/passwd "" (allowing bill to run sudo passwd with no arguments, which modifies root’s password).

  • Setting improper permissions on /etc/sudoers.d/bill (e.g., 0644). Sudo will log a warning and ignore files with insecure permissions.

  • Verify & Reboot Persistence Test:

# 1. Validate syntax of drop-in files:
visudo -c

# 2. Test user privilege rules:
sudo -l -U bill

# 3. Reboot system to ensure configuration persistence:
reboot

# 4. Post-reboot check:
sudo -l -U bill

(Output confirms that bill has allowed user management commands, while explicitly denying root password modifications persistently across reboots)