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
rootaccount password (passwd -l) and disabling direct SSH root access forces administrators to log in using unprivileged individual accounts and escalate privileges viasudo. This creates clear audit trails in system logs. - Gotchas: Locking the root account before creating an administrative user with verified
sudoprivileges 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/sudoersfile in RHEL includes the directive%wheel ALL=(ALL) ALL, automatically granting full administrative privileges to any user who is a member of thewheelsupplementary group. - Gotchas: Creating custom administrative groups without defining corresponding rules in
/etc/sudoersor 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 rootand/usr/bin/passwd ""prevents the user from changing root’s password or runningpasswdwithout 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, runningsudo passwdwith 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 beforesudoexecutes. Wrapping the pipeline insh -corbash -cpasses the entire command pipeline to the elevated shell execution context. - Gotchas: Writing
sudo cat /var/log/secure | grep "Failed password"works becausecatis elevated, but runningsudo echo "data" > /etc/protected_filefails 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/useraddstores baseline default variables used byuseraddwhen creating new user accounts. - Gotchas: Modifying
/etc/login.defsexpecting home path defaults;/etc/login.defsmanages password aging and UID/GID ranges, whereas/etc/default/useradddefines 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 -llists 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/profileand~/.bash_profilefirst to set global and user-specific environment variables. - Gotchas: Confusing login shells (
su - username) with non-login subshells (su username), which skip/etc/profileand~/.bash_profileand execute only~/.bashrcand/etc/bashrc. - Verify: Insert a temporary
echocommand into~/.bash_profileand 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-Gadds new supplementary groups without removing the user from existing groups. - Gotchas: Running
usermod -G sales joanawithout the-aflag. Omitting-awipes out all secondary groups assigned to the user, replacing them exclusively withsales. - Verify:
id joana
9. List all groups a user is a member of.
- Command(s):
id username
# OR
groups username
- Why:
iddisplays the user’s UID, primary group GID, and all supplementary GIDs.groupsprints a space-separated list of group names. - Gotchas: Checking only
/etc/passwd; field 4 of/etc/passwdlists 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.somodule 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_timeoutspecifies the duration (in minutes) thatsudoauthentications persist before prompting the user for a password again. - Gotchas: Setting
timestamp_timeout=0forces 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/sudoersis the primary rule file for privilege escalation. Modular drop-in configurations are stored in/etc/sudoers.d/. - Gotchas: Editing
/etc/sudoersdirectly using standard text editors likevimornanoinstead of usingvisudo. - 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:
visudolocks the target file against concurrent edits and performs strict syntax validation before saving, preventing syntax errors that could breaksudoaccess 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.breakhalts 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 /.autorelabelafter modifying configuration files in/etc/during an emergency rescue session; SELinux context mismatches can prevent services from starting. - Verify: Reboot normally and verify
sudoexecution.
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/passwdstructure 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
wheelgroup grants full administrative privileges natively under RHEL defaults. - Gotchas: Creating custom files in
/etc/sudoers.d/with file permissions other than0440or0400.sudoignores 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:
vigrlocks/etc/groupduring edits and performs syntax verification upon saving, preventing race conditions with administrative CLI commands (groupadd,usermod). - Gotchas: Editing
/etc/groupusing standardvimwhile 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:
passwdupdates the encrypted password hash, whilechagesets expiration parameters (e.g.,-Mmaximum days,-mminimum days,-Wwarning 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/shadowstores encrypted user password hashes, change timestamps, and aging parameters. It is restricted to read permissions forrootonly (0000or0400). - Gotchas: Expecting password hashes inside
/etc/passwd;/etc/passwddisplays anxplaceholder 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/groupdefines group names, GIDs, and supplementary group member lists./etc/gshadowstores encrypted group passwords and group administrators. - Gotchas: Looking in
/etc/passwdto 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., Userjoanagets primary groupjoana). -
usermod -aG: Appends supplementary groups without overwriting existing memberships. -
chage -M 90: Enforces 90-day password expiration for existing users. -
/etc/login.defs: UpdatesPASS_MAX_DAYSto ensure future users inherit the 90-day password policy automatically. -
Gotchas:
-
Forgetting the
-aflag inusermod -aG, which would wipe out other group memberships. -
Setting
PASS_MAX_DAYSin/etc/login.defsand assuming it retroactively updates existing users. Existing users must be updated explicitly usingchage. -
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
billaccess to/usr/sbin/useradd,/usr/sbin/usermod, and/usr/sbin/userdel. -
Negations (
!):!/usr/bin/passwd rootblocks explicitly targeted attempts against root, and!/usr/bin/passwd ""blockspasswdcalls with empty arguments (which execute in root’s context if elevated). -
chmod 0440: Enforces mandatory read-only file permissions required bysudo. -
Gotchas:
-
Forgetting
!/usr/bin/passwd ""(allowingbillto runsudo passwdwith 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)