Skip to main content
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.

Before you start

  • Connect with SSH as root or a sudo user. Root can leave out sudo.
  • A group is a named set of users. Files have an owner and a group, and permissions 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

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

Check the result

Expected output:
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.
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

How to remove a user from a group

Expected output:

How to create users

adduser asks for a password and creates /home/alex.
Useful useradd options on any distribution:

How to list users and groups

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

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.
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:
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.

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.

VPS and dedicated server guides

Every server guide in one place.

Create a sudo user

Give a user admin rights.

File permissions explained

chmod and chown in plain words.

Who is logged in

Check sessions and login history.

Checked against the usermod, useradd, userdel, gpasswd and passwd man pages from the shadow-utils project on 4 November 2026; commands tested on Ubuntu 24.04 LTS. Commands can differ on other distributions or releases.

Need Extra Help?

If you encounter any issues, our support team is ready to assist:

Save on Your Hosting

Ready to upgrade your hosting? Use code KB20 at checkout for 20% off your first order!

Last Updated: November 2026