Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Part 2 · System administration · Lesson 06 · 40 minutes

Add and control accounts and groups

After this lesson you can
  • Add, change, lock and remove accounts and groups, and read the files that hold them
  • Put a user into a group without removing the other groups
  • Set password ageing and force a password change
Lab files of this lesson (10)

The lab folder as it stands after this lesson. Unzip it in ~/krlab.

More

The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.

Scenario

Two people join the finance team of a small company, and one contractor leaves. Your lead gives you the rules: each person gets their own account, the team shares one group, and the contractor loses access today while the files stay for the audit. You do all of this on your practice system with accounts whose names start with kr_, so you can remove them at the end.

Recall

You see a "Z" in the STAT column of ps. What is the process, and can kill -9 remove it?

It is a zombie: finished, but its parent has not read the exit code. It cannot be killed, because it is already dead. Fix or end the parent, and the system removes it.

You run "nice -n 5 job" as a normal user. May you later lower the value to 2 with renice?

No. A normal user can make a process nicer (a higher number) but cannot raise its priority again. Root can do both.

The idea

Linux knows a user by a number, the UID, and a group by its GID. Three text files hold the facts.

Where an account lives
/etc/passwdname, uid, gid, home, shell/etc/shadowpassword hash and ageing dates/etc/groupgroup names, numbers and members/etc/passwdname, uid, gid, home, shell/etc/shadowpassword hash and ageing dates/etc/groupgroup names, numbers and members

Everyone may read /etc/passwd and /etc/group. Only root and the group shadow may read /etc/shadow, which holds the password hashes. A line of /etc/passwd has seven fields, split by colons.

A line of /etc/passwd
kr_anathe user namexthe hash is in shadow2001the UID2100the GID of the main groupAna Diazthe comment/home/kr_anathe home folder/bin/bashthe login shellkr_anathe user namexthe hash is in shadow2001the UID2100the GID of the main groupAna Diazthe comment/home/kr_anathe home folder/bin/bashthe login shell

A line of /etc/shadow holds the name, the hash, the day of the last change, the minimum and maximum age in days, and the warning days. The day is a number of days since 1 January 1970, and 99999 as maximum age means that the password never expires. A hash that starts with ! means that the password is locked, and * means that no password can work.

CommandWhat it does
useradd, usermod, userdelAdd, change and remove an account
groupadd, groupdelAdd and remove a group
passwdSet or lock a password, and show its status
chageShow and set password ageing
id, groupsShow the UID and the groups of a user

UIDs: 0 is root, below 1000 are system accounts, and normal users start at 1000. The lab accounts use fixed numbers from 2001.

Worked example

The lines show kr_ana, the new employee. The user at the prompt is you, with the UID 1001 here (yours can be 1000).

1. Read the three files

getent passwd NAME (get entries) prints the line of an account from the account database, wherever the system keeps it. sudo head -n 3 /etc/shadow reads the first lines of the shadow file, and cut -c1-60 keeps 60 characters of each.

getent passwd "$USER"
getent group "$USER"
sudo head -n 3 /etc/shadow | cut -c1-60
you:x:1001:1001::/home/you:/bin/bash
you:x:1001:
root:*:20775:0:99999:7:::
daemon:*:20775:0:99999:7:::
bin:*:20775:0:99999:7:::

The group line has four fields: name, x, GID and the list of members. The list is empty, because a primary group does not appear in it.

2. Add a group and an account

groupadd -g 2100 kr_dev makes a group with the GID 2100. useradd makes an account: -m creates the home folder with the files of /etc/skel, -u sets the UID, -g the primary group, -s the shell and -c the comment. id prints the numbers and groups. ls -A lists hidden names too, but not . and ...

sudo groupadd -g 2100 kr_dev
sudo useradd -m -u 2001 -g kr_dev -s /bin/bash -c "Ana Diaz" kr_ana
getent passwd kr_ana
id kr_ana
sudo ls -A /home/kr_ana
sudo grep '^kr_ana' /etc/shadow
kr_ana:x:2001:2100:Ana Diaz:/home/kr_ana:/bin/bash
uid=2001(kr_ana) gid=2100(kr_dev) groups=2100(kr_dev)
.bash_logout
.bashrc
.profile
kr_ana:!:20778:0:99999:7:::

The home folder is closed to other users, so ls needs sudo. It lists the start files from /etc/skel. The hash field holds !: no password yet.

3. Set a password

passwd NAME sets the password of another user (only root may). It asks twice and does not show what you type. passwd -S (status) prints the status: P usable password, L locked, NP no password.

sudo passwd kr_ana
sudo passwd -S kr_ana
New password:
Retype new password:
passwd: password updated successfully
kr_ana P 2026-11-21 0 99999 7 -1

The fields of passwd -S are: the name, the status, the day of the last change, the minimum age, the maximum age, the warning days and the inactive days (-1 means not set).

Never send a password by mail: set a temporary one and force a change (step 6).

4. Put the user in a group

usermod changes an account. -aG kr_ops adds the user to the group kr_ops as a secondary group. Without -a, -G replaces all the secondary groups of the user, which removes them from the others. groups NAME lists the groups.

sudo groupadd -g 2101 kr_ops
sudo usermod -aG kr_ops kr_ana
groups kr_ana
getent group kr_ops
kr_ana : kr_dev kr_ops
kr_ops:x:2101:kr_ana

A running session does not see a new group until the user logs in again.

5. Lock an account

passwd -l (lock) puts ! in front of the hash, and passwd -u removes it. A locked password does not stop a login with an SSH key. To close an account fully, also set an expiry date in the past (chage -E 0 NAME) or give it the shell /usr/sbin/nologin.

sudo passwd -l kr_ana
sudo passwd -S kr_ana
sudo grep '^kr_ana' /etc/shadow | cut -c1-14
sudo passwd -u kr_ana
sudo usermod -s /usr/sbin/nologin kr_ana
getent passwd kr_ana
sudo usermod -s /bin/bash kr_ana
passwd: password changed.
kr_ana L 2026-11-21 0 99999 7 -1
kr_ana:!$y$j9T
passwd: password changed.
kr_ana:x:2001:2100:Ana Diaz:/home/kr_ana:/usr/sbin/nologin

6. Set password ageing

chage (change age) sets the rules of a password. -M 90 is the maximum age in days, -m 1 the minimum, -W 7 the warning days, and -E DATE the day when the account expires. -d 0 makes the password count as old, so the user must change it at the next login. chage -l lists the settings.

sudo chage -M 90 -m 1 -W 7 kr_ana
sudo chage -d 0 kr_ana
sudo chage -l kr_ana
Last password change					: password must be changed
Password expires					: password must be changed
Password inactive					: password must be changed
Account expires						: never
Minimum number of days between password change		: 1
Maximum number of days between password change		: 90
Number of days of warning before password expires	: 7

7. Remove the accounts

userdel NAME removes the account and keeps the home folder. -r removes the home folder too. Clean up the lab. userdel may print mail spool ... not found: that is harmless.

sudo userdel -r kr_ana
sudo groupdel kr_dev
sudo groupdel kr_ops

Files that the user owned outside the home stay, with a bare UID as owner. Find them with find / -xdev -nouser.

Your turn

There are no steps here. Do this on your practice system:

  1. Make the groups kr_ops (GID 2200) and kr_logs (GID 2201).
  2. Make the account kr_cleo (UID 2003) with the primary group kr_ops, the extra group kr_logs and the shell /bin/bash. Make kr_dan (UID 2004) with the primary group kr_ops and the shell /usr/sbin/nologin.
  3. Set the maximum password age of kr_cleo to 60 days, with a warning 10 days before. Lock the password of kr_dan.

Answer from your own system.

Last, clean up: sudo userdel -r kr_cleo, sudo userdel -r kr_dan, sudo groupdel kr_ops and sudo groupdel kr_logs.

Check yourself

You run "sudo usermod -G kr_ops kr_ana" without -a. What is the result?

The account loses all its other secondary groups and keeps only kr_ops. Always write -aG to add one group.

You lock a password with passwd -l, and the user still logs in over SSH. Why?

SSH can use a key, and a locked password does not stop a key. Close the account with an expiry date in the past (chage -E 0), or set the shell to /usr/sbin/nologin, and remove the keys in ~/.ssh/authorized_keys.

You run "sudo chage -d 0 kr_ana". What happens when kr_ana logs in next?

The password counts as expired, so the system asks for a new password before it gives a shell. Use it with a temporary password: the user then chooses a private one.

The second field of a line of /etc/shadow is "!" for kr_ana. What does that tell you?

There is no usable password: the account is new, or its password is locked. The user cannot log in with a password, but an SSH key can still work.

Field notes

  • Give rights to groups, not to people. When a person changes teams, you change a group only.
  • Check for accounts that should not exist: awk -F: '$3 == 0' /etc/passwd lists every account with UID 0, and only root should be there. Check accounts without a password: the hash field of /etc/shadow must not be empty.
  • Set the password policy in /etc/login.defs and with PAM. CIS Benchmarks and NIST SP 800-63B give the targets: long passwords and multi-factor login beat frequent changes.
  • Do not delete the account of a person who left before the audit: lock it, set the shell to nologin, and remove it later. The files keep their owner until then.