RHCSA Commands: User and Groups
1. User Login Activity & Auditing
| Command | Description | Example |
|---|---|---|
who |
Lists currently logged-in users. Reads /run/utmp. |
who |
w |
Lists currently logged-in users with idle time, CPU usage, and current activity. Reads /run/utmp. |
w |
last |
Reports history of successful user login attempts and system reboots. Reads /var/log/wtmp. |
last |
last reboot |
Lists system reboot details only. | last reboot |
lastb |
Reports history of failed (unsuccessful) login attempts. Reads /var/log/btmp. Requires root. |
lastb |
lastlog |
Reports the most recent login time for every user account on the system. Reads /var/log/lastlog. |
lastlog |
uptime |
Shows current system time, uptime, number of logged-in users, and CPU load averages. | uptime |
2. User Identity & Group Information
| Command | Description | Example |
|---|---|---|
id |
Displays UID, GID, group name, all secondary groups, and SELinux context for the current user. | id |
id <username> |
Displays identification information for a specific user. | id linda |
groups |
Lists all groups the current user is a member of (primary first, then secondary). | groups |
groups <username> |
Lists all groups a specific user belongs to. | groups user1 |
whoami |
Shows the effective (current) username after switching users with su. |
whoami |
logname |
Shows the original login username (ignores su switches). |
logname |
lid -g <group> |
Lists which users are members of a specific group. | lid -g sales |
groupmems -g <group> -l |
Lists all members (primary + secondary) of a specific group. | groupmems -g sales -l |
3. User Account Creation (useradd)
| Command | Description | Example |
|---|---|---|
useradd <username> |
Creates a new user with all default values from /etc/default/useradd and /etc/login.defs. |
useradd user2 |
useradd -u <UID> <username> |
Creates a user with a specific UID. | useradd -u 1010 user3 |
useradd -d <home-dir> <username> |
Creates a user with a custom home directory path. | useradd -d /usr/user3a user3 |
useradd -s <shell> <username> |
Creates a user with a specific login shell. | useradd -s /bin/sh user3 |
useradd -s /sbin/nologin <username> |
Creates a non-interactive (no-login) user account. | useradd -s /sbin/nologin user4 |
useradd -c <comment> <username> |
Adds a GECOS/comment field for the user. | useradd -c "John Doe" john |
useradd -g <GID> <username> |
Sets the primary group GID for the new user. | useradd -g 6000 user5000 |
useradd -G <groups> <username> |
Adds the user to one or more secondary groups (comma-separated). | useradd -G sales,ops linda |
useradd -m <username> |
Forces creation of a home directory. | useradd -m john |
useradd -e <YYYY-MM-DD> <username> |
Sets an account expiry date. | useradd -e 2025-12-31 tempuser |
useradd -f <days> <username> |
Sets inactivity days before account lockout after password expiry. | useradd -f -1 user3 |
useradd -k <skel-dir> <username> |
Specifies a custom skeleton directory. | useradd -k /etc/myskel user3 |
useradd -b <base-dir> <username> |
Sets the base directory for the user’s home directory. | useradd -b /usr user3 |
useradd -r <username> |
Creates a system account (UID below 1000, no password expiry). | useradd -r apache |
useradd -o <username> |
Allows creating a user with a non-unique (duplicate) UID. | useradd -o -u 1000 shareduser |
useradd -m -u 1201 -G sales,ops linda |
Creates user with home dir, custom UID, and secondary groups. | useradd -m -u 1201 -G sales,ops linda |
useradd -D |
Displays the default settings used at the time of user creation. | useradd -D |
useradd -D -s /bin/zsh |
Modifies the default shell for all future users. | useradd -D -s /bin/zsh |
for i in lucy lori bob; do useradd $i; done |
Creates multiple users in a loop. | for i in lucy lori bob; do useradd $i; done |
4. User Account Modification (usermod)
| Command | Description | Example |
|---|---|---|
usermod -l <newname> <oldname> |
Changes the login name of a user. | usermod -l user2new user2 |
usermod -u <UID> <username> |
Changes the UID of an existing user. | usermod -u 2000 user2new |
usermod -d <new-home> <username> |
Changes the home directory path. | usermod -d /home/user2new user2new |
usermod -md <new-home> <username> |
Changes the home directory and moves contents from the old location. | usermod -md /home/user2new user2new |
usermod -s <shell> <username> |
Changes the login shell. | usermod -s /sbin/nologin user2new |
usermod -aG <group> <username> |
Appends the user to a secondary group (preserves existing groups). Always use -a with -G. |
usermod -aG wheel user |
usermod -aG <group> <username> |
Adds user as a secondary member to a group. | usermod -aG dba user1 |
usermod -g <GID> <username> |
Changes the primary group of a user. | usermod -g sysadm user200 |
usermod -L <username> |
Locks a user account (prepends ! to the password hash in /etc/shadow). |
usermod -L user200 |
usermod -U <username> |
Unlocks a locked user account (removes ! from the password hash). |
usermod -U user200 |
usermod -e <YYYY-MM-DD> <username> |
Sets account expiry date. | usermod -e 2025-12-31 tempuser |
usermod -G <groups> <username> |
Replaces all secondary groups (without -a, removes user from all other secondary groups!). |
usermod -G sales,ops linda |
5. User Account Deletion (userdel)
| Command | Description | Example |
|---|---|---|
userdel <username> |
Removes the user account from authentication files. Home directory is retained. | userdel user2new |
userdel -r <username> |
Removes the user account along with home directory and mail spool. | userdel -r user2new |
userdel -f <username> |
Forces removal even if the user is currently logged in. | userdel -f user2new |
6. Password Management (passwd)
| Command | Description | Example |
|---|---|---|
passwd <username> |
Sets or changes a user’s password interactively. | passwd user2 |
passwd |
Changes the current user’s own password. | passwd |
echo <password> | passwd --stdin <username> |
Sets a password non-interactively (pipe method). | echo password123 | passwd --stdin user2 |
passwd -n <days> <username> |
Sets minimum days before password can be changed. | passwd -n 7 user200 |
passwd -x <days> <username> |
Sets maximum password validity days. | passwd -x 90 user200 |
passwd -w <days> <username> |
Sets warning days before password expiry. | passwd -w 14 user200 |
passwd -i <days> <username> |
Sets inactive days after password expiry before account lockout. | passwd -i 5 user200 |
passwd -n 7 -x 90 -w 5 <username> |
Sets multiple aging attributes at once. | passwd -n 7 -x 90 -w 5 alice |
passwd -l <username> |
Locks a user account. | passwd -l user10 |
passwd -u <username> |
Unlocks a locked user account. | passwd -u user10 |
passwd -e <username> |
Forces the user to change password at next login (expires current password). | passwd -e user60 |
passwd -d <username> |
Deletes (removes) a user’s password without expiring the account. | passwd -d user5 |
passwd -S <username> |
Displays password status information for a user. | passwd -S user5 |
7. Password Aging (chage)
| Command | Description | Example |
|---|---|---|
chage -m <days> <username> |
Sets minimum days between password changes. | chage -m 7 user100 |
chage -M <days> <username> |
Sets maximum password validity days. | chage -M 28 user100 |
chage -W <days> <username> |
Sets warning days before password expiry. | chage -W 5 user100 |
chage -I <days> <username> |
Sets inactive days after password expiry before lockout. Use -I -1 to disable. |
chage -I 7 user100 |
chage -E <YYYY-MM-DD> <username> |
Sets account expiry date. Use -E -1 to disable. |
chage -E 2020-01-31 user100 |
chage -d 0 <username> |
Forces user to change password at next login (sets last change to epoch). | chage -d 0 user60 |
chage -l <username> |
Lists current password aging attributes for a user. | chage -l linda |
chage <username> |
Interactive mode: prompts for all password aging properties one by one. | chage anna |
chage -m 7 -M 28 -W 5 <username> |
Sets multiple aging attributes at once. | chage -m 7 -M 28 -W 5 user100 |
8. Group Account Management
Creating Groups
| Command | Description | Example |
|---|---|---|
groupadd <groupname> |
Creates a new group with default settings. | groupadd sales |
groupadd -g <GID> <groupname> |
Creates a group with a specific GID. | groupadd -g 5000 linuxadm |
groupadd -o -g <GID> <groupname> |
Creates a group sharing a GID with an existing group. | groupadd -o -g 5000 dba |
groupadd -r <groupname> |
Creates a system group (GID below 1000). | groupadd -r ntp |
Modifying Groups
| Command | Description | Example |
|---|---|---|
groupmod -n <newname> <oldname> |
Renames an existing group. | groupmod -n sysadm linuxadm |
groupmod -g <new-GID> <groupname> |
Changes the GID of an existing group. | groupmod -g 6000 sysadm |
Deleting Groups
| Command | Description | Example |
|---|---|---|
groupdel <groupname> |
Deletes a group and its entries from /etc/group and /etc/gshadow. |
groupdel sysadm |
Group Membership & Administration
| Command | Description | Example |
|---|---|---|
gpasswd <groupname> |
Sets a group-level password (allows non-members to use newgrp). |
gpasswd sales |
gpasswd -a <user> <group> |
Adds a user as a member of a group. | gpasswd -a user1 sales |
gpasswd -d <user> <group> |
Removes a user from a group. | gpasswd -d user1 sales |
gpasswd -A <admin1,admin2> <group> |
Assigns group administrators who can manage members. | gpasswd -A user1 sales |
newgrp <groupname> |
Temporarily changes the user’s primary group to the specified group. | newgrp sales |
9. Switching Users & Privileged Access
su (Substitute User)
| Command | Description | Example |
|---|---|---|
su |
Switches to root (subshell, no environment loaded). Prompts for root password. | su |
su - |
Switches to root with a full login shell (loads target user’s environment). Recommended. | su - |
su - <username> |
Switches to a specific user with a full login shell. | su - bob |
su <username> |
Switches to a specific user (subshell, no environment loaded). | su bob |
su -c "<command>" |
Runs a single command as root without switching shells. | su -c "firewall-cmd --list-services" |
su - <username> -c "<command>" |
Runs a single command as a specific user. | su - user100 -c "ls -la" |
sudo (Superuser Do)
| Command | Description | Example |
|---|---|---|
sudo <command> |
Runs a command with root privileges. Prompts for the user’s own password. | sudo systemctl restart sshd |
sudo -i |
Opens a root shell via sudo (full login environment). | sudo -i |
sudo -u <user> <command> |
Runs a command as a specific (non-root) user. | sudo -u apache cat /etc/httpd/conf/httpd.conf |
sudo sh -c "<command1> | <command2>" |
Runs piped commands with sudo (pipe runs as root, not as normal user). | sudo sh -c "rpm -qa | grep ssh" |
sudo ls -l /root |
Runs a specific command with elevated privileges. | sudo ls -l /root |
sudo passwd <username> |
Changes another user’s password with sudo. | sudo passwd betty |
pkexec (PolicyKit)
| Command | Description | Example |
|---|---|---|
pkexec <command> |
Runs a command with administrative privileges via PolicyKit (alternative to sudo). | pkexec visudo |
pkexec visudo |
Repairs sudo configuration when sudo access is completely lost. | pkexec visudo |
10. Sudo Configuration (visudo)
| Command | Description | Example |
|---|---|---|
visudo |
Safely edits the /etc/sudoers file (creates temp copy, checks syntax). |
visudo |
visudo -f <file> |
Safely edits a specific sudoers file (e.g., a drop-in file). | visudo -f /etc/sudoers.d/custom |
echo "<rule>" > /etc/sudoers.d/<file> |
Creates a drop-in sudoers file (best practice for custom rules). | echo "betty ALL=(ALL) ALL" > /etc/sudoers.d/betty |
Sudoers File Syntax (configuration entries, not commands)
| Entry | Description | Example |
|---|---|---|
username ALL=(ALL) ALL |
Grants full sudo access to a user (password required). | alice ALL=(ALL) ALL |
username ALL=(ALL) NOPASSWD: ALL |
Grants full sudo access without password prompt. | alice ALL=(ALL) NOPASSWD: ALL |
%groupname ALL=(ALL) ALL |
Grants full sudo access to all members of a group. | %wheel ALL=(ALL) ALL |
username ALL=<commands> |
Grants access to specific commands only. | bob ALL=/usr/bin/systemctl, /usr/bin/journalctl |
username ALL=<cmd>, !<cmd> |
Grants access with an exception (negation). | amy ALL=/usr/bin/passwd, !/usr/bin/passwd root |
Defaults timestamp_timeout=<mins> |
Extends the sudo token lifetime (default: 5 minutes). | Defaults timestamp_timeout=240 |
User_Alias NAME = user1, user2 |
Defines a user alias for grouping users. | User_Alias PKGADM = user1, user100, user200 |
Cmnd_Alias NAME = /path/cmd1, /path/cmd2 |
Defines a command alias for grouping commands. | Cmnd_Alias PKGCMD = /usr/bin/yum, /usr/bin/rpm |
11. Safe File Editing for Authentication Files
| Command | Description | Example |
|---|---|---|
vipw |
Safely edits /etc/passwd with file locking to prevent corruption. |
vipw |
vipw -s |
Safely edits /etc/shadow with file locking. |
vipw -s |
vigr |
Safely edits /etc/group with file locking. |
vigr |
12. File Ownership Management
| Command | Description | Example |
|---|---|---|
chown <user> <file> |
Changes the owner of a file. | chown user100 file10 |
chown <user>:<group> <file> |
Changes both the owner and group of a file. | chown user200:user100 file10 |
chown -R <user>:<group> <dir> |
Recursively changes owner and group of a directory and its contents. | chown -R user200:dba dir10 |
chgrp <group> <file> |
Changes only the group ownership of a file. | chgrp dba file10 |
ls -ln |
Lists files showing numeric UID/GID instead of names. | ls -ln |
13. Viewing & Inspecting Configuration Files
| Command | Description | Example |
|---|---|---|
cat /etc/passwd |
Displays the passwd file (user account info). | cat /etc/passwd |
cat /etc/shadow |
Displays the shadow file (hashed passwords, aging). Root only. | cat /etc/shadow |
cat /etc/group |
Displays the group file (group info and members). | cat /etc/group |
cat /etc/gshadow |
Displays the gshadow file (group passwords, admins). Root only. | cat /etc/gshadow |
cat /etc/default/useradd |
Displays default settings for the useradd command. |
cat /etc/default/useradd |
cat /etc/login.defs |
Displays system-wide login defaults (UID ranges, password aging). | cat /etc/login.defs |
tail -n 10 /etc/shadow |
Shows the last 10 lines of the shadow file. | tail -n 10 /etc/shadow |
grep <username> /etc/passwd |
Searches for a specific user in the passwd file. | grep lori /etc/passwd |
grep <username> /etc/passwd /etc/shadow /etc/group |
Searches for a user across multiple authentication files. | grep lori /etc/passwd /etc/shadow /etc/group |
grep <username> /etc/shadow |
Checks the shadow entry for a user (e.g., to verify lock status). | grep user200 /etc/shadow |
grep <username> /etc/group |
Checks group membership for a user. | grep user1 /etc/group |
grep '/sbin/nologin' /etc/passwd |
Lists all non-interactive (nologin) user accounts. | grep '/sbin/nologin' /etc/passwd |
14. Miscellaneous & Utility Commands
| Command | Description | Example |
|---|---|---|
exit |
Exits the current shell (returns to the parent shell or logs out). | exit |
mkdir <dirname> |
Creates a directory (used in skeleton directory setup). | mkdir /etc/skel/fotos |
cd <directory> |
Changes the current working directory. | cd /etc/skel |
vim /etc/login.defs |
Opens the login.defs file for editing (to change defaults like PASS_MAX_DAYS). | vim /etc/login.defs |
15. Key Configuration Files Reference
| File | Purpose | Permissions |
|---|---|---|
/etc/passwd |
User account info (username, UID, GID, home, shell). World-readable. | 644 |
/etc/shadow |
Hashed passwords and password aging. Root-only. | 000 |
/etc/group |
Group info (name, GID, members). World-readable. | 644 |
/etc/gshadow |
Group passwords and administrators. Root-only. | 000 |
/etc/default/useradd |
Default values for useradd (home base, shell, skel). |
644 |
/etc/login.defs |
System-wide defaults (UID/GID ranges, password aging, UMASK). | 644 |
/etc/skel/ |
Skeleton directory; contents copied to new user home directories. | 755 |
/etc/sudoers |
Main sudo configuration file. Edit with visudo only. |
440 |
/etc/sudoers.d/ |
Drop-in directory for custom sudo rules (best practice). | – |
/etc/nologin.txt |
Custom message displayed when non-root users are denied login. | – |
/run/utmp |
Currently logged-in users (read by who, w). |
– |
/var/log/wtmp |
History of successful logins and reboots (read by last). |
– |
/var/log/btmp |
History of failed login attempts (read by lastb). |
– |
/var/log/lastlog |
Most recent login for each user (read by lastlog). |
– |
/var/log/secure |
Sudo authentication and command logs. | – |
16. Important Defaults & Directives
/etc/default/useradd Directives
| Directive | Purpose |
|---|---|
GROUP |
Default GID (if USERGROUPS_ENAB is no). |
HOME |
Base directory for user home directories (default: /home). |
INACTIVE |
Inactivity days before account disablement after password expiry. |
EXPIRE |
Account expiry date. |
SHELL |
Default login shell (default: /bin/bash). |
SKEL |
Skeleton directory path (default: /etc/skel). |
CREATE_MAIL_SPOOL |
Whether to create a mail spool directory. |
/etc/login.defs Key Directives
| Directive | Purpose |
|---|---|
UID_MIN / UID_MAX |
UID range for regular users (default: 1000+). |
GID_MIN / GID_MAX |
GID range for regular groups. |
SYS_UID_MIN / SYS_UID_MAX |
UID range for system accounts. |
SYS_GID_MIN / SYS_GID_MAX |
GID range for system groups. |
PASS_MAX_DAYS |
Default maximum password validity days. |
PASS_MIN_DAYS |
Default minimum days between password changes. |
PASS_WARN_AGE |
Default warning days before password expiry. |
PASS_MIN_LEN |
Minimum password length. |
CREATE_HOME |
Whether to create a home directory for new users. |
UMASK |
Default permissions mask for new user home directories. |
USERGROUPS_ENAB |
Whether to delete a user’s private group on user deletion. |
ENCRYPT_METHOD |
Password encryption algorithm (default: SHA-512). |
MAIL_DIR |
Mail directory location. |
MOTD_FILE |
Message of the day file path. |
ENV_PATH |
Default $PATH variable for new users. |