> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Managing User Accounts

> Guide to creating modifying and deleting Linux user accounts from the command line with verification commands and common useradd usermod userdel options

In this lesson you'll learn how to create, modify, and delete Linux user accounts from the command line. We'll also show how to verify the changes by inspecting system files and using commands like `id`, `getent`, and `passwd -S`. By the end you'll understand the common user-management commands (`useradd`, `usermod`, `userdel`) and the system changes they produce.

Note: these examples assume you are running as the `root` user. If you are not root, prefix the commands with `sudo`.

Resources:

* useradd, usermod, userdel: [useradd(8)](https://man7.org/linux/man-pages/man8/useradd.8.html) / [usermod(8)](https://man7.org/linux/man-pages/man8/usermod.8.html) / [userdel(8)](https://man7.org/linux/man-pages/man8/userdel.8.html)
* Check groups and passwd entries: `getent` (see `getent passwd`, `getent group`)

## Verify accounts and system files

Use these commands to inspect accounts and groups:

* Show user identity and groups:

```bash theme={null}
id <username>
```

* Lookup user entry in system databases (works with NSS like LDAP):

```bash theme={null}
getent passwd <username>
getent group <groupname>
```

* Check `/etc/passwd` and `/etc/group` directly (local files):

```bash theme={null}
grep '^<username>:' /etc/passwd
grep '^<groupname>:' /etc/group
```

## Create users

* Create a basic user `user1` (no home by default on some distros):

```bash theme={null}
useradd user1
```

Verify the account and its primary group:

```bash theme={null}
id user1
getent passwd user1
```

Expected sample `id` output (UID/GID numbers vary):

```text theme={null}
uid=1005(user1) gid=1005(user1) groups=1005(user1)
```

* Create `user2` with a custom home directory and ensure the home is created (`-m` and `-d`):

```bash theme={null}
useradd -m -d /custom_home/user2 user2
```

Verify:

```bash theme={null}
id user2
ls -ld /custom_home/user2
```

* Create `user3` with a specific UID:

```bash theme={null}
useradd -u 1050 -m user3
id user3
```

Expected:

```text theme={null}
uid=1050(user3) gid=1050(user3) groups=1050(user3)
```

* Create a non-login/system-style account `user4` by setting the shell to `/sbin/nologin`:

```bash theme={null}
useradd -m -s /sbin/nologin user4
id user4
```

* Create `user5` with an existing primary group `student` (the `student` group must already exist):

```bash theme={null}
useradd -m -g student user5
id user5
```

Expected (if `student` has GID 1000):

```text theme={null}
uid=1006(user5) gid=1000(student) groups=1000(student)
```

* Create `user6` and add it to the `wheel` group as a supplementary group:

```bash theme={null}
useradd -m -G wheel user6
id user6
getent group wheel
```

Expected output showing `wheel` membership (UID/GID numbers may differ):

```text theme={null}
uid=1007(user6) gid=1007(user6) groups=1007(user6),10(wheel)
```

## Modify users

Use `usermod` to change existing accounts.

* Rename `user1` to `newuser1`:

```bash theme={null}
usermod -l newuser1 user1
id newuser1
```

Note: renaming a user does not automatically rename the home directory. To rename the account and move its home directory, include `-d` and `-m`:

```bash theme={null}
usermod -l newuser1 -d /home/newuser1 -m user1
```

* Add `user2` to the `wheel` group without removing existing supplementary groups (append with `-aG`):

```bash theme={null}
usermod -aG wheel user2
id user2
```

* Change the login shell for `user5` to `/bin/bash`:

```bash theme={null}
usermod -s /bin/bash user5
getent passwd user5
```

* Lock `user4` to disable password-based logins:

```bash theme={null}
usermod -L user4
# On many distributions you can inspect account status:
passwd -S user4
```

Expected `passwd -S` output after locking (format varies by distro):

```text theme={null}
user4 L 01/01/1970 0 99999 7 -1 (Password locked.)
```

## Provide default files for new users

Files in `/etc/skel/` are copied into new users' home directories when you create homes with `useradd -m`.

* Create a welcome file in `/etc/skel/`:

```bash theme={null}
echo "Welcome to the system!" > /etc/skel/welcome
chmod 644 /etc/skel/welcome
```

* Create a new user `user7` to test that the `welcome` file is copied:

```bash theme={null}
useradd -m user7
ls -l /home/user7/welcome
cat /home/user7/welcome
```

You should see `/home/user7/welcome` and the message you added.

## Delete users

* Delete `user4` (removes only the user account, not the home directory):

```bash theme={null}
userdel user4
```

* Delete `user5` and remove its home directory and mail spool (`-r`):

```bash theme={null}
userdel -r user5
```

Confirm deletion by checking the passwd database:

```bash theme={null}
getent passwd user4 || echo "user4 not found"
getent passwd user5 || echo "user5 not found"
```

## Quick reference — common options

| Command | Option | Purpose | Example |
| - | - | - | - |
| `useradd` | `-m` | Create the user's home directory | `useradd -m user7` |
| `useradd` | `-d /path` | Set custom home directory | `useradd -m -d /custom_home/user2 user2` |
| `useradd` | `-u UID` | Specify numeric UID | `useradd -u 1050 -m user3` |
| `useradd` | `-s /bin/sh` | Set login shell | `useradd -m -s /sbin/nologin user4` |
| `useradd` | `-g group` | Primary group (group must exist) | `useradd -m -g student user5` |
| `useradd` | `-G group[,..]` | Supplementary groups | `useradd -m -G wheel user6` |
| `usermod` | `-l newname` | Rename account | `usermod -l newuser1 user1` |
| `usermod` | `-d /newhome -m` | Move and update home directory | `usermod -d /home/newuser1 -m user1` |
| `usermod` | `-aG group` | Append supplementary group(s) | `usermod -aG wheel user2` |
| `usermod` | `-s /bin/bash` | Change login shell | `usermod -s /bin/bash user5` |
| `usermod` | `-L` | Lock account (disable password login) | `usermod -L user4` |
| `userdel` | `-r` | Remove user and home/mail spools | `userdel -r user5` |

## Summary

* `useradd` — create user accounts. Useful options: `-m`, `-d`, `-u`, `-s`, `-g`, `-G`.
* `usermod` — modify existing accounts. Common options: `-l`, `-d -m`, `-s`, `-aG`, `-L`.
* `userdel` — remove accounts; add `-r` to delete home directories and mail spools.
* Put default files in `/etc/skel/` to have them copied into new users' homes when using `useradd -m`.
* Verify accounts with `id`, `getent passwd`, `getent group`, or by inspecting `/etc/passwd` and `/etc/group`.

<Callout icon="lightbulb" color="#1CB2FE">
  Always ensure any groups you reference (for example `student` or `wheel`) exist before assigning them as primary or supplementary groups. Use `getent group <groupname>` to check.
</Callout>

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/red-hat-certified-system-administrator-rhcsa/module/7940be7b-b644-468b-abba-fe810cb9cc9b/lesson/35c022fa-c24f-47df-9682-7dfc66f08aa0" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.