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

# How to Add a User to a Group on Linux (and Manage Users)

> Add a user to a group on Linux with sudo usermod -aG group user, then log in again. Also create, list, lock and delete users and groups on your server.

To add a user to a group on Linux, run `sudo usermod -aG groupname username`, for example `sudo usermod -aG www-data alex`. The `-a` (append) flag keeps the user's other groups. The change applies the next time the user logs in; check it with `id alex`. The same command works on Ubuntu, Debian, AlmaLinux and Rocky Linux.

| Difficulty | Time | Works on | Needs |
| - | - | - | - |
| Beginner | 5 minutes | Ubuntu 22.04 / 24.04, Debian 12 / 13, AlmaLinux / Rocky 9 / 10 | sudo or root access |

## Before you start

* [Connect with SSH](https://help.freakhosting.com/portals/other/how-to-connect-to-a-linux-server-with-ssh.md) as root or a [sudo user](https://help.freakhosting.com/portals/other/how-to-create-a-sudo-user-on-linux.md). Root can leave out `sudo`.
* A **group** is a named set of users. Files have an owner and a group, and [permissions](https://help.freakhosting.com/portals/other/linux-file-permissions-explained-chmod-and-chown.md) decide what each can do. Putting users in a group is how you let several people share files.

## How to add a user to a group

**1. Add the user**

```bash theme={null}
sudo usermod -aG developers alex
```

To add several groups at once, separate them with commas: `sudo usermod -aG developers,www-data alex`.

**2. Check the result**

```bash theme={null}
id alex
```

Expected output:

```text theme={null}
uid=1001(alex) gid=1001(alex) groups=1001(alex),27(sudo),1002(developers)
```

**3. Log in again**

The user must log out and back in (or start a new SSH session) before the new group applies. Running processes keep their old groups.

> [!WARNING]
>
> Never leave out `-a`. `sudo usermod -G developers alex` **replaces** all of alex's extra groups with just `developers`, which can remove sudo access. Also don't add normal users to `sudo`, `wheel`, `root`, `docker` or `disk`: each of these gives root-level control.

## How to create and delete groups

```bash theme={null}
sudo groupadd developers     # create a group
getent group developers      # show the group and its members
sudo groupdel developers     # delete a group (it must not be anyone's primary group)
```

## How to remove a user from a group

```bash theme={null}
sudo gpasswd -d alex developers
```

Expected output:

```text theme={null}
Removing user alex from group developers
```

## How to create users

> [tab] Ubuntu / Debian
>
> ```bash theme={null}
> sudo adduser alex
> ```
>
> `adduser` asks for a password and creates `/home/alex`.

> [tab] AlmaLinux / Rocky
>
> ```bash theme={null}
> sudo useradd -m alex
> sudo passwd alex
> ```

Useful `useradd` options on any distribution:

| Option | Effect | Example |
| - | - | - |
| `-m` | Create the home directory | `useradd -m alex` |
| `-s` | Login shell | `useradd -m -s /bin/bash alex` |
| `-G` | Extra groups | `useradd -m -G developers alex` |
| `-r` | System account for a service (no login, low UID) | `useradd -r -s /usr/sbin/nologin myapp` |

## How to list users and groups

```bash theme={null}
getent passwd            # every account, one per line
getent passwd | cut -d: -f1   # just the user names
groups alex              # groups of one user
getent group sudo        # members of one group
```

Accounts with a UID of 1000 or higher are usually people. Lower numbers are system accounts used by services such as `www-data` or `mysql`.

## How to change, lock and delete users

| Task | Command |
| - | - |
| Change a password | `sudo passwd alex` |
| Change the login shell | `sudo usermod -s /bin/bash alex` |
| Rename a user | `sudo usermod -l alexandra alex` (the home folder keeps its name unless you also use `-d /home/alexandra -m`) |
| Lock an account (password login stops) | `sudo passwd -l alex` |
| Unlock it | `sudo passwd -u alex` |
| Expire an account fully (also blocks SSH keys) | `sudo usermod --expiredate 1 alex` |
| Delete a user, keep files | `sudo userdel alex` |
| Delete a user and home directory | `sudo userdel -r alex` |

> [!WARNING]
>
> `userdel -r` deletes the user's home directory and mail spool permanently. Copy anything you need first. Make sure the user has no running processes (`pgrep -u alex`) before you delete the account.

> [!NOTE]
>
> `passwd -l` only blocks password logins. A user with an SSH key can still log in. To stop all logins, expire the account with `usermod --expiredate 1` or remove the key from `~/.ssh/authorized_keys`.

## How to check it worked

Start a new SSH session as the user and run `id`. The new group must appear in the list. To test file access, create a shared folder:

```bash theme={null}
sudo mkdir -p /srv/shared
sudo chown root:developers /srv/shared
sudo chmod 2775 /srv/shared
```

Any member of `developers` can now create files in `/srv/shared`. The `2` (setgid) makes new files inherit the `developers` group.

## Frequently asked questions

### Why doesn't my new group work yet?

Group membership is read at login. Log out and back in, or run `newgrp developers` to start a shell with the new group straight away.

### What is the difference between a primary and a secondary group?

The primary group (usually named like the user) is the group new files get. Secondary groups, added with `usermod -aG`, give extra access. `id` shows the primary group as `gid=` and all groups under `groups=`.

### How do I give a user admin (sudo) rights?

Add them to `sudo` on Ubuntu/Debian or `wheel` on AlmaLinux/Rocky. See [how to create a sudo user](https://help.freakhosting.com/portals/other/how-to-create-a-sudo-user-on-linux.md).

### Can I edit /etc/passwd or /etc/group directly?

You can, but a typo can lock everyone out. Use `usermod`, `gpasswd` and `groupadd`, or `sudo vigr` and `sudo vipw`, which lock the file and check it.

## Related guides

> [card] [VPS and dedicated server guides](https://help.freakhosting.com/portals/other/vps-and-dedicated-server-guides.md)
>
> Every server guide in one place.

> [card] [Create a sudo user](https://help.freakhosting.com/portals/other/how-to-create-a-sudo-user-on-linux.md)
>
> Give a user admin rights.

> [card] [File permissions explained](https://help.freakhosting.com/portals/other/linux-file-permissions-explained-chmod-and-chown.md)
>
> chmod and chown in plain words.

> [card] [Who is logged in](https://help.freakhosting.com/portals/other/how-to-check-who-is-logged-in-and-login-history.md)
>
> Check sessions and login history.

***

> [!NOTE]
>
> **Checked against the `usermod`, `useradd`, `userdel`, `gpasswd` and `passwd` man pages from the [shadow-utils project](https://github.com/shadow-maint/shadow)** on 4 November 2026; commands tested on Ubuntu 24.04 LTS. Commands can differ on other distributions or releases.

> [!NOTE]
>
> ### Need Extra Help?
>
> If you encounter any issues, our support team is ready to assist:
>
> * **Live Chat:** Quick assistance via our website.
> * **Support Ticket:** [Open a Ticket](https://freakhosting.com/clientarea/submitticket.php)
> * **Discord:** [Join our Community](https://discord.gg/freakhosting)
> * **Email:** [support@freakhosting.com](mailto:support@freakhosting.com)

> [!TIP]
>
> ### Save on Your Hosting
>
> Ready to upgrade your hosting? Use code **KB20** at checkout for **20% off** your first order!

***

**Last Updated:** November 2026


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