Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

Chapter 6Name in the book: User and Group Management

Manage users, groups and admin rights

Every person and every service on Linux works as a user. This chapter shows how to create, change and delete users and groups, how to set password rules, and how to give admin rights in a safe way with sudo.

  • 16 Lessons
  • 25 Commands
  • 2 Labs

$ run as a normal user# run as root (or with sudo)

Exam objectives in this chapter

  • Create, delete and change local user accounts
  • Change passwords and password aging for local users
  • Create, delete and change local groups and group members
  • Set up admin (privileged) access

6.1

Know the kinds of users

Linux knows three kinds of accounts. The UID (user ID number) tells which one it is.

KindUIDExampleUse
root0rootFull control of the system
System accounts1–999apache, chrony, sshdRun services; nobody logs in with them
Normal users1000+studentPeople
id
uid=1000(student) gid=1000(student) groups=1000(student),10(wheel)
id apache
getent passwd root

Remember

UID 0 = root, under 1000 = services, 1000 and up = people.

6.2

Work as root only when you need it

root can do anything — also destroy the system with one typo. Work as a normal user and switch to root only for admin tasks.

  • The prompt shows it: $ for a normal user, # for root.
  • Use sudo COMMAND for one admin command, or sudo -i for a root shell when you have many tasks.
  • Leave the root shell with exit as soon as you are done.

At work

Every sudo command is logged in /var/log/secure (and the journal) with the real user name. A shared root password has no such trace.

6.3

Switch user with su

su (substitute user) starts a shell as another user. You type that user's password.

su -
exit
su - lisa
su -c 'tail /var/log/messages' -
  • su - (with the dash) gives a full login shell: the new user's environment and home folder. Always use the dash.
  • su without a user name means root.
  • root can switch to any user without a password.

Watch out

su without - keeps your old environment (PATH, variables). Programs can behave strangely or run from the wrong folder.

6.4

Give admin rights with sudo

sudo lets allowed users run commands as root (or another user) with their own password. The rules are in /etc/sudoers and in files in /etc/sudoers.d/.

On RHEL, members of the group wheel may run everything:

/etc/sudoers (part)
%wheel  ALL=(ALL)       ALL
  1. Give a user full admin rights by adding the user to wheel.
    usermod -aG wheel lisa
  2. Or give one user only some commands, in a separate file. Always edit with visudo, which checks the syntax before saving.
    visudo -f /etc/sudoers.d/webadmins
    /etc/sudoers.d/webadmins
    %webadmins  ALL=(root)  /usr/bin/systemctl restart httpd, /usr/bin/systemctl status httpd
  3. Test it as that user.
    sudo -l
    sudo systemctl restart httpd

The rule format is: WHO WHERE=(AS_WHOM) COMMANDS. A % before the name means a group. NOPASSWD: before the commands means no password is asked.

Exam tip

The objective "configure privileged access" = sudo rules. Use a file in /etc/sudoers.d/ and visudo; never break /etc/sudoers.

Watch out

A syntax error in a sudoers file can lock everybody out of sudo. visudo stops you before you save a broken file.

6.5

Know PolicyKit

PolicyKit (polkit) decides which actions of system services a user may do — for example change the network, shut down, or mount a disk — mostly from graphical tools.

  • When a user without rights tries such an action, polkit asks for an admin password.
  • Members of wheel count as administrators for polkit.
  • pkexec COMMAND runs a command as root through polkit (like sudo, but with polkit rules).
  • Rules are in /usr/share/polkit-1/ (do not change) and /etc/polkit-1/rules.d/ (your own).
nmcli general permissions
pkexec systemctl restart chronyd

6.6

Read /etc/passwd and /etc/shadow

User accounts are lines in text files. /etc/passwd has the account data (readable by all); /etc/shadow has the password hashes and password ages (only root).

/etc/passwd
lisa:x:1001:1001:Lisa Hassan:/home/lisa:/bin/bash
FieldMeaning
1 lisaUser name
2 xThe password is in /etc/shadow
3 1001UID
4 1001GID of the primary group
5 Lisa HassanComment (GECOS): full name
6 /home/lisaHome folder
7 /bin/bashLogin shell
/etc/shadow
lisa:$6$aBc…:20385:0:90:7:::

Fields of shadow: user, password hash (! or !! = locked or no password), last change (days since 1970), min days, max days, warning days, inactive days, expire date.

Watch out

Do not edit these files with a normal editor while users change passwords. Use the commands (useradd, usermod, passwd) or vipw/vigr, which lock the files.

Commands in this lesson

6.7

Create users

useradd makes the account; passwd sets the password. The new home folder gets a copy of /etc/skel.

  1. Create a user with a full name.
    useradd -c "Lisa Hassan" lisa
  2. Set the password.
    passwd lisa
  3. Create a user with more settings in one go.
    useradd -u 1500 -G wheel,webadmins -s /bin/bash -c "Omar Ali" omar
  4. Create an account for a service that nobody logs in to.
    useradd -r -s /sbin/nologin -d /var/lib/myapp myapp
OptionMeaning
-cComment (full name)
-uA chosen UID
-g / -GPrimary group / extra groups
-sLogin shell
-dHome folder
-m / -MMake / do not make the home folder
-eExpire date (YYYY-MM-DD)
-rSystem account (UID under 1000, no aging)

Check it

The user exists and has the right groups.

id omar
getent passwd omar

6.8

Set the defaults for new users

useradd takes its defaults from /etc/login.defs and /etc/default/useradd. Change them once, and every new user gets them.

useradd -D
useradd -D -s /bin/bash
grep -E '^(UID_MIN|PASS_MAX_DAYS|PASS_MIN_DAYS|PASS_WARN_AGE|CREATE_HOME|HOME_MODE)' /etc/login.defs
FileSets
/etc/login.defsUID/GID ranges, password aging defaults, create home, home permissions, umask
/etc/default/useraddDefault shell, home base folder, skel folder, group, expire
/etc/skel/Files every new home folder gets (like .bashrc)
  1. Make every new user get a file with company rules.
    echo "Read the IT policy: https://intranet.example.com/policy" > /etc/skel/README.txt
  2. Make new passwords expire after 90 days by default.
    sed -i 's/^PASS_MAX_DAYS.*/PASS_MAX_DAYS   90/' /etc/login.defs

Remember

login.defs = numbers and rules, default/useradd = shell and home, skel = starter files. They only affect new users.

6.9

Change and delete users

usermod changes an existing account; userdel removes it.

usermod -aG webadmins lisa
usermod -s /sbin/nologin olduser
usermod -L lisa
usermod -U lisa
usermod -e 2026-12-31 contractor1
usermod -l lisa.hassan lisa
userdel -r contractor1
  • -aG GROUP adds an extra group. Without -a, -G replaces all extra groups.
  • -L / -U lock / unlock the password.
  • -l NEW OLD renames the login (the home folder keeps its name unless you add -d NEWHOME -m).
  • userdel -r also removes the home folder and the mail spool.

Watch out

usermod -G wheel lisa (without -a) removes lisa from every other extra group. Always use -aG to add.

At work

When someone leaves, first lock the account (usermod -L, chage -E 0), keep the files for a while, then delete. Find their files with find / -user NAME.

6.10

Set password aging

Password aging forces users to change passwords and can expire accounts. chage sets it per user; passwd has some short forms.

chage -l lisa
chage -M 90 -m 1 -W 7 lisa
chage -E 2026-12-31 contractor1
chage -d 0 lisa
passwd -e lisa
OptionMeaning
-M 90Maximum: change the password every 90 days
-m 1Minimum: wait 1 day before the next change
-W 7Warn 7 days before it expires
-I 14Lock the account 14 days after the password expired
-E DATEThe whole account expires on this date (-E -1 = never)
-d 0Force a new password at the next login

Check it

Look at the result.

chage -l lisa

Exam tip

Tasks like "user X must change the password every 30 days" or "the account expires on …" use chage. For all new users, set PASS_MAX_DAYS in /etc/login.defs.

6.11

Give users a working environment

A new home folder starts as a copy of /etc/skel. The shell reads the profile files at login, so users get the same start settings.

  • Put files for all new users in /etc/skel (like .bashrc with company aliases).
  • Settings for all users at every login: a file in /etc/profile.d/.
  • Accounts that must not log in get the shell /sbin/nologin.
ls -la /etc/skel
usermod -s /sbin/nologin backup
su - backup
This account is currently not available.

6.12

Understand groups

A group gives the same rights to many users. Every user has one primary group and can be in many secondary (extra) groups.

  • The primary group is the group of new files a user makes. On RHEL each user gets a private group with the same name.
  • Secondary groups give access to shared files and folders.
  • Groups are in /etc/group (name, password placeholder, GID, members) and /etc/gshadow.
id lisa
uid=1001(lisa) gid=1001(lisa) groups=1001(lisa),1100(webadmins)
groups lisa
getent group webadmins
webadmins:x:1100:lisa,omar

Watch out

A new group membership is active only after the user logs in again (or runs newgrp GROUP).

6.13

Create and change groups

groupadd makes a group, groupmod changes it, groupdel removes it. Add members with usermod -aG or gpasswd -a.

groupadd webadmins
groupadd -g 2000 dbadmins
groupmod -n web-admins webadmins
gpasswd -a lisa web-admins
gpasswd -d lisa web-admins
groupdel dbadmins

To edit the files by hand safely, use vigr (for /etc/group) and vipw (for /etc/passwd). They lock the files while you edit.

Check it

The members of a group.

getent group web-admins

Remember

add member = usermod -aG GROUP USER or gpasswd -a USER GROUP. Remove member = gpasswd -d USER GROUP.

6.14

Remember the main points

The most important facts of this chapter, in short.

  • root (UID 0) can do everything; system accounts run services; normal users start at UID 1000.
  • Use sudo for admin work: members of the wheel group may run all commands; own rules go in /etc/sudoers.d/ with visudo.
  • User data is in /etc/passwd, passwords and aging in /etc/shadow, groups in /etc/group.
  • useradd, usermod, userdel manage users; defaults come from /etc/login.defs and /etc/default/useradd; new home folders get the files of /etc/skel.
  • passwd sets a password; chage sets password aging (-M max days, -m min days, -W warn, -E expire date).
  • Every user has one primary group and can have secondary groups (usermod -aG group user).
  • groupadd, groupmod, groupdel and gpasswd manage groups.

6.15

Learn the key terms

The words of this chapter in simple English.

TermMeaning
UID / GIDThe number of a user / a group.
rootThe super user with UID 0.
System accountAn account for a service, not for a person (UID below 1000).
sudoRun one command as root (or another user), with rules in /etc/sudoers.
wheelThe group whose members may use sudo on RHEL.
PolicyKit (polkit)Rules that let normal users do some admin actions in graphical tools.
/etc/shadowThe file with the password hashes and aging data.
Password agingRules for how long a password is valid.
Primary groupThe group a new file of the user gets.
Secondary groupAn extra group membership for more access.
/etc/skelFiles copied into every new home folder.

6.16

Check what you learned

Think about the answer first, then open it.

1How do you create user lisa with a home folder and the shell /bin/bash?

useradd -s /bin/bash lisa (RHEL makes the home folder by default; -m makes sure).

2How do you add user lisa to the group sales without removing her other groups?
3How do you force lisa to change her password every 90 days?
4How do you give group helpdesk the right to restart sshd as root and nothing else?

visudo -f /etc/sudoers.d/helpdesk with the line %helpdesk ALL=(root) /usr/bin/systemctl restart sshd.

5Where do you put a file that every new user must get in the home folder?

In /etc/skel.

6Which file has the password hashes?

/etc/shadow (only root can read it).

7How do you lock an account so the user cannot log in with a password?
8Which command shows the UID, GID and groups of a user?

Practice

All commands in this chapter