How to Add User to Group on Ubuntu 24.04

Managing users and groups is one of the significant tasks, but only root users (who have administrative privileges) can add a user to a group. Linux has two major types of groups:

Primary Group: This group is the same as the username. When a user creates a file, it is automatically added to this group. The details of a primary group are saved in the file located at ‘/etc/passwd.

Secondary Group: This group provides particular access to the users and manages software applications and individual files. The secondary group members don’t have the read, write, and execute permissions.

Every user in a Linux system is part of a primary group and can be included in more to have particular privileges. It is time to check out all the possible ways to add a user to a group on Ubuntu 24.04. We have divided this blog into sections explaining various methods to easily add users to the group.

Add an Existing User to a Group

You can add an existing user to any group using multiple commands such as adduser, gpasswd, and usermod.

First, list all the existing users using the following command:

cat /etc/passwd | awk -F: '{print $1}'
Add an Existing User to a Group

Run the below command in the terminal using the below command to list all groups:

cut -d: f1 /etc/group
List All groups on Ubuntu

The adduser Command

You can create a group or add a user using the adduser command. Meanwhile, you can also use this command to add the user to a group through the below command:

sudo adduser <user_name> <group_name>

For example, let’s add a user ‘sys‘ to an ‘adm‘ group by running the following command in the terminal:

sudo adduser sys adm
adduser Command

The gpasswd Command

The gpasswd command is another utility to add an existing user to a group:

sudo gpasswd -a <user_name> <group_name>

In the following example, we add a backup user to a group named voice:

sudo gpasswd -a backup voice
gpasswd Command

Rather than adding one user to a group, you can add multiple users to a single gpasswd command, which is as follows:

sudo gpasswd -M <user_1>,<user_2>,<user_3> <group_name>

Now, we add the user name news, mail, and lp to a group called “proxy” using the following command:

sudo gpasswd -M news,mail,lp proxy
Add users to group

In the above syntax -M stands for the multiple users.

The usermod Command

This command changes user details like username, group, home directory, shell, and password. So here, we use this command to add the user to the group, and the general syntax for this is as follows:

sudo usermode -a -G <group_name> <user_name>

Here, -a → appends the username in the group

  • -G → shows that the user added to the secondary group
  • -g → shows that the user added to the primary group

For example, here we add the IRC user to the tty secondary group:

sudo usermod -a -G tty irc
Add a user to secondary group in Linux

You can also add a single user to multiple groups using this command:

sudo usermod -a -G <group_1>,<group_2>,<group3> <user_name>

For example, here, we add the uucp user to the floppy and cdrom groups by running the following command:

sudo usermod -a -G floppy, cdrom uucp
Example to add users in a secondary group

Note: Don’t leave a space between the comma and the other username.

Add a New User to a Group

Linux has a “useradd” utility from which users can create a new user and add it to a group simultaneously. Users need to follow the below syntax to add a new user to a group:

sudo useradd -g <group_name> <new_username> (for primary groups)
sudo useradd -G <group_name> <new_username> (for secondary groups)

For example, here, we create a new user, “h2s,” and add it to the ‘man‘ group. So, we run the following command in the terminal:

sudo useradd -G man h2s
group adding syntax

Note: You can also verify the addition of a user to a group using the following syntax:

groups <user_name>
Linux group listing syntax

Moreover, through the ‘id‘ command, you can also verify that the user belongs to a particular group:

id <username>

Conclusion

Adding a user to a group is an essential task with many benefits related to system management, security, etc. This blog has mentioned all the possible commands to add users to groups, like usermod, gpasswd, and adduser. Each utility has specific functions, like adding single or multiple users to a group or adding various users to a single group or primary/secondary group.

Other Articles:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.