Linux users and groups: create, lock, remove
Commands and files to create, lock, age, audit and remove Linux user accounts and groups.
This page lists the commands used to create, inspect, lock, age and remove Linux user accounts and groups, and the files that store them. The commands work on most distributions; adduser and deluser are the Debian variants.
Account files
| File | Content |
|---|---|
/etc/passwd |
Login name, UID, primary GID, home directory, login shell |
/etc/shadow |
Encrypted password and password-ageing fields (readable by root only) |
/etc/group |
Group name, GID and member list |
Query the account databases with getent, which also consults the other name services configured in NSS:
getent passwd <username>
getent group
The hash prefix in /etc/shadow gives the algorithm:
| Prefix | Algorithm |
|---|---|
$1$ |
MD5 |
$2a$, $2y$ |
Blowfish |
$5$ |
SHA-256 |
$6$ |
SHA-512 |
The ageing fields are, in order: date of last password change, minimum days between changes, maximum days of validity, warning days before expiry, inactive days after expiry, and account expiry date. Dates are counted in days since 1 January 1970.
Create an account
sudo useradd -m -g developers -G print,sshd <username>
sudo passwd <username>
-m creates the home directory, -g sets the primary group and -G the supplementary groups.
To create a service account that cannot log in:
sudo useradd -s /usr/sbin/nologin -r -M -d /dev/null <username>
-r creates a system account and -M does not create a home directory. /bin/false can be used instead of /usr/sbin/nologin. On Debian: sudo adduser --system --no-create-home --group <username>.
Lock and unlock
sudo passwd -l <username> # lock: prefixes the password hash with !
sudo usermod -U <username> # unlock
sudo chage -E -1 <username> # remove the account expiry date (-1 = never expires)
Password ageing
sudo chage -l <username> # show ageing information
| Option | Meaning |
|---|---|
-d, --lastday |
Set the date of the last password change |
-E, --expiredate |
Set the account expiration date |
-I, --inactive |
Days after expiry before the account is disabled |
-l, --list |
Show account ageing information |
-m, --mindays |
Minimum days before a password change |
-M, --maxdays |
Maximum days before a password change |
-W, --warndays |
Days of expiry warning |
-R, --root |
Directory to chroot into |
Check the integrity of the account files without modifying them:
sudo pwck -r
Failed logins (faillock)
On RHEL-like systems, pam_faillock locks an account after repeated failed logins. The PAM configuration is in /etc/pam.d/system-auth and /etc/pam.d/password-auth.
faillock --user <username> # show the failed attempts
faillock --user <username> --reset # clear them
The counters are stored in /run/faillock/<username>. Deleting that file also clears the lock.
Groups
getent group
id -nG <username> # groups of a user
groups <username>
sudo groupadd <group>
sudo usermod -aG <group> <username> # add to a group; -a is required with -G
sudo usermod -aG <group1>,<group2> <username>
sudo usermod -g <group> <username> # change the primary group
sudo gpasswd -d <username> <group> # remove from a group (Debian: deluser <username> <group>)
Without -a, usermod -G replaces the supplementary group list instead of appending to it.
Remove an account
-
Check for sessions and processes, then stop them:
who sudo pkill -KILL -u <username> -
Archive the home directory:
sudo tar -cjvf <username>-backup.tar.bz2 /home/<username> -
Review and remove the cron jobs.
crontab -rdeletes the whole crontab, so save it first:sudo ls -lh /var/spool/cron/crontabs/<username> sudo crontab -r -u <username> -
Remove pending print jobs:
lprm -U <username> -
Delete the account and its home directory:
sudo userdel -r <username> # Debian: sudo deluser --remove-home <username> # Force removal even if the user is still logged in: sudo userdel -f -r <username> -
Check that no group still refers to the account:
grep <username> /etc/group ls /home
Notes
- Deleting an account and its home directory cannot be undone: archive first.
- Granting a regular account root rights (UID 0, membership of the root group, or NOPASSWD sudo rules) is security-sensitive and is not covered here. Use sudo group or sudoers rules reviewed by the security owner.