Skip to content

Beginner · Permissions, users and sudo · topic 8 of 57 · 30 min

Creating and managing users and passwords

Explanation

useradd creates a user, passwd sets the password, usermod changes an account (usermod -aG adds a group), and userdel removes it. groupadd makes groups. chage controls password ageing: when a password expires and when an account stops working.

Creating users

bash
# useradd asha
# passwd asha
Changing password for user asha.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
# useradd -u 1500 -c "Ravi Kumar" -s /bin/bash -G wheel ravi
# id ravi
uid=1500(ravi) gid=1500(ravi) groups=1500(ravi),10(wheel)
On RHEL, useradd creates the home directory and a private group with the same name by default.

Useful useradd options: -u UID, -g primary group, -G extra groups (comma-separated), -c comment, -d home directory, -s shell, -m create the home (already the default on RHEL), -M do not create a home. The new home is filled with copies of the files in /etc/skel, so put a standard .bashrc there for every new user.

Defaults come from /etc/login.defs (UID ranges, password ageing defaults, whether to create the home) and /etc/default/useradd (default shell, skel directory). useradd -D shows the second one.

Changing users: usermod

bash
# usermod -aG developers asha    # ADD asha to developers, keep her other groups
# usermod -s /sbin/nologin backup  # change the shell
# usermod -L ravi                  # lock the password (puts ! in /etc/shadow)
# usermod -U ravi                  # unlock
# usermod -c "Asha Rao, DBA" asha  # change the comment
Careful: usermod -G developers asha (without -a) REPLACES all of asha's extra groups with only developers. If she was in wheel, she just lost her admin rights. Always use -aG to add.

Removing users and groups

bash
# userdel ravi        # remove the account, keep /home/ravi
# userdel -r ravi     # remove the account AND its home directory and mail spool
# groupadd -g 2000 finance
# groupmod -n accounts finance   # rename the group
# groupdel accounts

If you delete a user but keep the files, the files still belong to the old UID number. ls -l will show a number instead of a name. If a new user later gets that same UID, they silently own the old files. Find such files with find / -nouser.

Password ageing with chage

bash
# chage -l asha
Last password change                    : Oct 19, 2026
Password expires                        : never
Password inactive                       : never
Account expires                         : never
Minimum number of days between password change  : 0
Maximum number of days between password change  : 99999
Number of days of warning before password expires: 7
# chage -M 90 -W 7 asha          # must change every 90 days, warn 7 days before
# chage -d 0 asha                # force a password change at next login
# chage -E 2026-12-31 intern1    # account stops working after 31 Dec 2026
chage optionMeaning/etc/shadow field
-dDate of last change (0 = must change now)3
-mMinimum days between changes4
-MMaximum days a password is valid5
-WDays of warning before expiry6
-IInactive days allowed after expiry7
-EAccount expiry date (YYYY-MM-DD, or -1 to remove)8

To change the defaults for future users, edit PASS_MAX_DAYS, PASS_MIN_DAYS and PASS_WARN_AGE in /etc/login.defs. These only affect users created afterwards; existing users need chage.

Common mistakes

  • Forgetting -a with usermod -G (see the warning above).
  • Setting PASS_MAX_DAYS in /etc/login.defs and expecting existing users to change. Use chage for them.
  • Putting a password on the command line in scripts. It ends up in the shell history. If you must script it, read it from a protected file: echo "asha:NewPass" | chpasswd from a root-only script is the usual way.
Tip: RHCSA tasks often say things like "create user X with UID Y, a supplementary group Z and a password that expires in N days". Read every part, do each with its own option, and check with id and chage -l at the end.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Creating and managing users and passwords · 1/7

useradd asha
passwd asha

useradd creates a user. On RHEL it also makes the home folder and a private group.

Prefer a video by a teacher? Find free videos on “rhcsa useradd usermod chage password aging tutorial” on YouTube ↗

In real life: Interns joining a Hyderabad IT services company

Every June, 40 interns join a support team in Hyderabad for six months. The admin creates their accounts with an expiry date at the end of the internship, puts them in a "support" group, and forces each to set their own password at first login. When an intern leaves early, usermod -L locks the account at once, and userdel -r runs after the data is checked.

bash
# groupadd support
# useradd -G support -c "Intern 2026" -e 2026-12-31 intern07
# echo 'intern07:Welcome@2026' | chpasswd
# chage -d 0 intern07
# chage -l intern07 | head -4
Last password change                    : password must be changed
Password expires                        : password must be changed
Password inactive                       : password must be changed
Account expires                         : Dec 31, 2026

Practice (0/3 done)

Task 1 · Question

Which command adds asha to the group devops without removing her other groups?

Task 2 · Question

What does chage -d 0 ravi do?

Task 3 · Practice

In your lab, create the group "sysadmins" with GID 3000, the user "harry" with UID 3001, comment "Harry Admin", member of sysadmins, whose password must be changed every 60 days and who must set a new password at first login.

Quick quiz

Question 1

1.Where do new home directories get their starting files from?

Question 2

2.Which removes the user and the home directory?

Question 3

3.Which chage option sets the date an account stops working?

Question 4

4.You change PASS_MAX_DAYS in /etc/login.defs to 90. Who is affected?

Question 5

5.What does usermod -L do?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

Where do new home directories get their starting files from?

Answer: /etc/skel

useradd copies /etc/skel into the new home.

Which removes the user and the home directory?

Answer: userdel -r ravi

-r removes the home and mail spool too.

Which chage option sets the date an account stops working?

Answer: -E

-E sets the account expiry date.

You change PASS_MAX_DAYS in /etc/login.defs to 90. Who is affected?

Answer: Only users created after the change

login.defs gives defaults to new accounts; existing ones keep their values.

What does usermod -L do?

Answer: Locks the password so password login fails

It puts ! in front of the hash in /etc/shadow.

What is the 7th field of an /etc/passwd line?Related

Answer: Login shell

The last field is the login shell.

From Users, groups and the files behind them

Whose password does sudo ask for by default?Related

Answer: The user's own

sudo asks for the calling user's own password.

From sudo and the sudoers file

"Failed password for invalid user oracle from 203.0.113.9" means…Related

Answer: Someone tried a username that does not exist on this server

"invalid user" means the account is not on the system: typical of automated guessing.

From Linux security basics

Which command adds user ravi to the docker group without removing his other groups?Related

Answer: sudo usermod -aG docker ravi

-a appends. Without it, -G replaces all extra groups.

From Users, groups and permissions

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 4 more topics in “Permissions, users and sudo” before its exam: File permissions: rwx, chmod and chown, umask and the special bits: SUID, SGID and sticky, Access control lists (ACLs), sudo and the sudoers file.
  2. Module 2 exam: Permissions, users and sudo · 14 questions · 25 min, without help. 6 topics of this module still to finish first.
  3. Then the next module, Processes, services and logs. 57 topics left in the course before the final exam.

← Previous: Users, groups and the files behind them