# Manage user and group accounts and related system files

> Create, change and remove users and groups, and read the files where Linux keeps them.

Source: https://aminbytes.dev/bytes/administrative-tasks/users-and-groups/


Every process on Linux runs as some user, and every file belongs to a user and a group. So accounts are the base of
all permissions: who can read your files, who may restart a service, who can log in at all. Adding a colleague,
locking out someone who left, or creating a quiet account for a web server are everyday admin jobs.

The good news: it all lives in a few plain text files, and a handful of commands edit them safely for you.

## Where accounts live

Four files in `/etc` hold the local account database:

| File | What it holds | Who can read it |
| --- | --- | --- |
| `/etc/passwd` | One line per user: name, UID, home, shell | Everyone (mode `644`) |
| `/etc/shadow` | Password hashes and password aging | Only root (and group `shadow` on Debian) |
| `/etc/group` | One line per group: name, GID, members | Everyone |
| `/etc/gshadow` | Group passwords and group admins | Only root |

### /etc/passwd

```console
$ grep alice /etc/passwd
alice:x:1001:1001:Alice Smith,,,:/home/alice:/bin/bash
```

Seven fields, split by colons:

| # | Field | Example | Meaning |
| --- | --- | --- | --- |
| 1 | Login name | `alice` | The name you type at login |
| 2 | Password | `x` | `x` means "the hash is in `/etc/shadow`" |
| 3 | UID | `1001` | The user ID. The kernel only cares about this number |
| 4 | GID | `1001` | The user's **primary group** |
| 5 | GECOS | `Alice Smith,,,` | Comment: full name, room, phone (free text) |
| 6 | Home | `/home/alice` | Home directory |
| 7 | Shell | `/bin/bash` | Program started at login |

UIDs follow a convention, set in `/etc/login.defs`:

- `0` is root. Any account with UID 0 has full power, whatever its name.
- `1` to `999` are **system accounts** for services (`www-data`, `sshd`, `chrony`). `SYS_UID_MIN` and `SYS_UID_MAX` set the range.
- `1000` and up are regular people (`UID_MIN`, `UID_MAX`).
- `65534` is `nobody`, an account that owns nothing on purpose.

### /etc/shadow

`/etc/passwd` must be readable by everyone (so `ls -l` can show names), which is why the password hashes moved to
`/etc/shadow`, readable only by root:

```console
$ sudo grep alice /etc/shadow
alice:$y$j9T$Lr2...truncated...:20788:0:99999:7:::
```

Nine fields:

| # | Field | Example | Meaning |
| --- | --- | --- | --- |
| 1 | Login name | `alice` | Same as in `/etc/passwd` |
| 2 | Hash | `$y$j9T$...` | The hashed password. `!` or `*` in front (or alone) means no password login |
| 3 | Last change | `20788` | Day of the last password change, counted in days since 1 January 1970 |
| 4 | Min | `0` | Days before the password may be changed again |
| 5 | Max | `99999` | Days a password stays valid (`99999` is "forever") |
| 6 | Warn | `7` | Days of warning before the password expires |
| 7 | Inactive | (empty) | Days after expiry that the old password still works to set a new one |
| 8 | Expire | (empty) | Day the whole **account** stops working |
| 9 | Reserved | (empty) | Unused |

The start of the hash tells you the algorithm: `$1$` MD5 (old, weak), `$5$` SHA-256, `$6$` SHA-512 (RHEL 9
default), `$y$` yescrypt (Debian 12, Ubuntu 24.04 and Fedora default). On RHEL, a fresh account without a
password shows `!!`.

Permissions differ a little: Debian makes `/etc/shadow` mode `640` with group `shadow`, RHEL makes it mode `000`
(only root gets in, because root ignores file permissions).

### /etc/group

```console
$ grep developers /etc/group
developers:x:1002:alice,bob
```

Four fields: group name, password (`x`, the real one is in `/etc/gshadow`), GID, and a comma separated member
list. That list only shows **supplementary** groups. A user's primary group comes from field 4 of `/etc/passwd`, so
`alice` is in group `alice` even though `/etc/group` doesn't list her there.

> [!WARNING]
> Never edit these files with a normal editor while the system is busy. Use the commands below, or `vipw` and
> `vigr`, which lock the file and check it before saving.

## Asking the database: getent and id

Accounts don't have to live in `/etc/passwd`. They can also come from LDAP, Active Directory or systemd, depending
on `/etc/nsswitch.conf`. `getent` asks the same way programs do, so it sees all of them:

```console
$ getent passwd alice
alice:x:1001:1001:Alice Smith,,,:/home/alice:/bin/bash
$ getent group developers
developers:x:1002:alice,bob
$ sudo getent shadow alice
alice:$y$j9T$Lr2...:20788:0:99999:7:::
```

Without a key, `getent passwd` lists every account. To see one user's groups, use `id` or `groups`:

```console
$ id alice
uid=1001(alice) gid=1001(alice) groups=1001(alice),1002(developers)
$ groups alice
alice : alice developers
```

## Creating users: useradd

`useradd` is the low-level tool on every distribution:

```bash
sudo useradd -m -s /bin/bash -c "Alice Smith" -G developers alice
sudo passwd alice
```

| Option | Meaning |
| --- | --- |
| `-m` | Create the home directory and copy `/etc/skel` into it |
| `-M` | Don't create a home directory |
| `-d DIR` | Use another home directory |
| `-s SHELL` | Login shell |
| `-c TEXT` | GECOS comment, usually the full name |
| `-u UID` | Pick the UID yourself |
| `-g GROUP` | Primary group (must already exist) |
| `-G G1,G2` | Supplementary groups |
| `-r` | System account: UID below 1000, no aging, no home unless you add `-m` |
| `-e YYYY-MM-DD` | Account expiry date |
| `-k DIR` | Use another skeleton directory than `/etc/skel` |
| `-D` | Show (or with other options, change) the defaults |

By default `useradd` also creates a group with the same name as the user and makes it the primary group. This is
called a **user private group** (`USERGROUPS_ENAB yes` in `/etc/login.defs`).

The defaults come from two files: `/etc/login.defs` (UID ranges, password aging defaults, hash method, whether to
create a home) and `/etc/default/useradd` (shell, skeleton, home base). `useradd -D` shows the second:

```console
$ useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
```

That output is from RHEL 9. Debian and Ubuntu differ in two ways that catch people out:

- Without `-m`, Debian's `useradd` creates **no** home directory. RHEL creates one (`CREATE_HOME yes` in `/etc/login.defs`).
- Debian's default shell is `/bin/sh`, RHEL's is `/bin/bash`.

 (Side note: Debian and Ubuntu also ship `adduser`, a friendly script that asks for the password and full name and always creates a home. On RHEL, `adduser` is just a link to `useradd`. The exam focuses on `useradd`.)

### The skeleton: /etc/skel

Whatever is in `/etc/skel/` gets copied into each new home directory made with `-m`. Put a default `.bashrc`, a
company `README` or a `.vimrc` there and every *new* user gets it. Existing users are not touched.

```console
$ ls -A /etc/skel
.bash_logout  .bashrc  .profile
```

(On RHEL you'll see `.bash_logout  .bash_profile  .bashrc`.)

## Passwords: passwd

```bash
passwd                 # change your own password
sudo passwd alice      # root sets alice's password, no old one needed
```

`passwd` also manages the account's password state:

| Command | Effect |
| --- | --- |
| `passwd -l alice` | Lock: puts `!` in front of the hash, so the password stops working |
| `passwd -u alice` | Unlock: removes the `!` |
| `passwd -d alice` | Delete the password (empty: be careful) |
| `passwd -e alice` | Expire it now: alice must pick a new one at next login |
| `passwd -S alice` | Show the status |

```console
$ sudo passwd -S alice
alice P 2026-12-01 0 99999 7 -1
```

`P` means a usable password, `L` locked, `NP` no password. Then come last change, min, max, warn and inactive days.

> [!IMPORTANT]
> A locked password does **not** stop SSH key logins. To shut an account down completely, also expire it:
> `usermod -L -e 1 alice` (or `chage -E 0 alice`), or give it the shell `/usr/sbin/nologin`.

## Password aging: chage

`chage` ("change age") edits the aging fields of `/etc/shadow`. Start by listing them:

```console
$ sudo chage -l alice
Last password change                                    : Dec 01, 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
```

| Option | Shadow field | Example |
| --- | --- | --- |
| `-m DAYS` | 4 Min | `chage -m 1 alice`: wait at least a day between changes |
| `-M DAYS` | 5 Max | `chage -M 90 alice`: new password every 90 days |
| `-W DAYS` | 6 Warn | `chage -W 14 alice`: warn two weeks ahead |
| `-I DAYS` | 7 Inactive | `chage -I 30 alice`: 30 days of grace after expiry |
| `-E DATE` | 8 Expire | `chage -E 2027-03-31 alice`: account ends that day (`-E -1` removes it) |
| `-d DATE` | 3 Last change | `chage -d 0 alice`: force a new password at next login |

Run `chage alice` without options and it asks for each value in turn. A normal user may run `chage -l` on their
own account. `passwd` can set some of these too (`-n` min, `-x` max, `-w` warn, `-i` inactive), but `chage` is the
one to remember.

New accounts take their aging defaults from `PASS_MAX_DAYS`, `PASS_MIN_DAYS` and `PASS_WARN_AGE` in
`/etc/login.defs`. Changing those later doesn't touch existing users.

## Changing users: usermod

`usermod` takes mostly the same options as `useradd`, plus a few of its own:

```bash
sudo usermod -aG docker alice                     # add a supplementary group
sudo usermod -s /bin/zsh alice                    # new login shell
sudo usermod -c "Alice Smith-Jones" alice         # new comment
sudo usermod -l alicej alice                      # rename the login to alicej
sudo usermod -d /home/alicej -m alicej            # new home, and move the files there
sudo usermod -L alice                             # lock the password (like passwd -l)
sudo usermod -U alice                             # unlock it
sudo usermod -e 2027-03-31 alice                  # account expiry date
```

> [!CAUTION]
> `-G` on its own **replaces** all supplementary groups. Always pair it with `-a` (append) when you want to add
> one: `usermod -aG wheel alice`. Forgetting the `-a` is a classic way to remove someone from `sudo`.

Group changes apply at the user's next login. A running shell keeps its old groups (`newgrp developers` starts a
shell with the new group straight away).

Renaming with `-l` does not rename the home directory or the user's private group. Change those yourself with
`-d -m` and `groupmod -n`.

## Removing users: userdel

```bash
sudo userdel bob        # remove the account, keep /home/bob
sudo userdel -r bob     # also remove the home directory and mail spool
```

`userdel` refuses while the user still has processes running (`user bob is currently used by process 2314`).
`-f` forces it, but it's cleaner to stop those processes first.

Files bob owned outside his home stay on disk, now owned by a bare number. Find them with:

```bash
sudo find / -nouser -o -nogroup 2>/dev/null
```

If you later create a user who gets the same UID, that user silently owns those files. That's why some admins
lock and expire old accounts instead of deleting them.

## Groups: groupadd, groupmod, groupdel

```bash
sudo groupadd developers            # next free GID
sudo groupadd -g 2000 projects      # pick the GID
sudo groupadd -r backup-svc         # system group (GID below 1000)
sudo groupmod -n devs developers    # rename developers to devs
sudo groupmod -g 2001 devs          # change the GID
sudo groupdel devs                  # delete the group
```

`groupdel` refuses to remove a group that is still some user's **primary** group:

```console
$ sudo groupdel alice
groupdel: cannot remove the primary group of user 'alice'
```

Members are managed from the user side (`usermod -aG`), or with `gpasswd -a bob devs` to add and `gpasswd -d bob devs`
to remove a single member.

## Special purpose and limited accounts

Not every account is a person. Services like a web server or a database run as their own **system account**, so a
bug in that service can only touch that service's files. These accounts have a low UID, often no home and a shell
that refuses logins:

```bash
sudo useradd -r -M -s /usr/sbin/nologin -c "Backup service" backupsvc
```

```console
$ getent passwd www-data
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
$ sudo su - www-data
This account is currently not available.
```

`/usr/sbin/nologin` (also reachable as `/sbin/nologin`) prints that polite message and exits. `/bin/false` does the
same without a message. Either way, nobody gets a shell, but the service can still run processes as that user.

For people who need *some* access, limit the account:

- **Temporary accounts:** set an end date, `useradd -e 2027-01-31 intern` or `chage -E 2027-01-31 intern`. After
  that day the account can't log in, but it still exists for your records.
- **Forced password hygiene:** `chage -M 90 -W 14 -I 7 alice`.
- **First login:** `chage -d 0 alice` so the password you handed over only works once.
- **Restricted shell:** `rbash` (bash started as `rbash`) blocks `cd`, changing `PATH` and running commands with a
  `/` in the name. It's a weak fence, not a security boundary.
- **Locked accounts:** `usermod -L -e 1 olduser` keeps the files and UID but blocks every login.

## Exam checklist

- `/etc/passwd` has 7 fields (`name:x:UID:GID:GECOS:home:shell`), `/etc/group` has 4 (`name:x:GID:members`), `/etc/shadow` has 9.
- Shadow dates are days since 1 January 1970. `!` or `*` in the hash field means no password login.
- UID 0 is root, system accounts sit below 1000, people start at 1000 (`/etc/login.defs`).
- `useradd -m` copies `/etc/skel/` into the new home. Defaults: `/etc/default/useradd` (`useradd -D`) and `/etc/login.defs`.
- `usermod -aG group user` adds a group. `-G` without `-a` replaces the list.
- `passwd -l`/`-u` and `usermod -L`/`-U` lock and unlock the password. Expire the account too to block key logins.
- `chage -l` lists aging, `-M` max days, `-m` min, `-W` warn, `-I` inactive, `-E` account expiry, `-d 0` forces a change.
- `userdel -r` removes the home directory too. `groupdel` can't remove a user's primary group.
- `getent passwd|group|shadow name` queries every source in `/etc/nsswitch.conf`, not only the local files.
- Service accounts: `useradd -r -s /usr/sbin/nologin`, often with `-M` for no home.


## Byte check

1. `alice` is in the supplementary groups `sudo` and `developers`. You run `usermod -G docker alice`. What are her supplementary groups now?
   - `sudo`, `developers` and `docker`
   - **Only `docker`** (correct)
   - Nothing changes: `-G` needs `-a` or it fails
   Without `-a`, `-G` replaces the whole list of supplementary groups. Use `usermod -aG docker alice` to add one.

2. You run `chage -E 2027-03-31 bob`. What did you set?
   - The date bob's password must be changed
   - **The date bob's whole account stops working** (correct)
   - How many days of warning bob gets before his password expires
   `-E` sets the account expiry date (field 8 of `/etc/shadow`). Password aging uses `-M`, `-m`, `-W` and `-I`.

3. Which line in `/etc/group` makes `bob` a member of `developers`?
   - **`developers:x:1002:alice,bob`** (correct)
   - `bob:x:1002:developers`
   - `developers:x:1002:bob:alice`
   The format is `name:password:GID:members`, and the members are a comma separated list in the fourth field.


