How to Add a User to a Group on a Linux Server

How to Add a User to a Group on Linux

User groups play an important role in any Linux operating system and its distributions. Groups make it easier to manage larger amounts of users, giving you the ability to give all of them the same security and access privileges.

For example, files and devices may be granted access based on a user’s ID or group ID. Groups allow you to set a file’s permission for the entire group instead of setting permissions for each individual user. Overall, this makes managing permissions for your server’s users a very simple and straightforward process. Let’s show you how to add users to groups on a Linux VPS.

There are two types of groups in Linux operating systems:

Primary Groups

When you create a new user on your system, a primary group with the same name is created. Any files created by the user are automatically added to that group.
The information about the user’s primary group is stored in the /etc/passwd file.

Secondary Groups

The secondary group specifies one or more groups that an account also belongs to. Secondary groups are very useful when you want to grant certain file permissions to specific users that are members of a specific group.

Prerequisites

  • A Linux server or Linux VPS with root access enabled or a user with sudo privileges. Our VPSes all have root access fully enabled and available for all of our users.

Getting Started: Create a User and Group

To create a new user named user1, simply run the following command:

useradd user1

You can also create specific groups with a name of your choice. create a new group named group1 with the following command:

groupadd group1

Once a user has been created, you can then view the groups that the specific user account is in with the following command:

groups user1

Running that command should give you the following output:

user1 : user1

If you want to view a list of all groups that exist on your system, that’s possible as well. You can do that by using the following command:

getent group

Adding an Existing User to a Group

You can add an existing user to a secondary group – just use the following syntax:

usermod -a -G groupname username

For example, to add user1 to the sudo group, run the following command:

usermod -a -G sudo user1

You can now verify this change with the following command:

groups user1

That should give you the following output:

user1 : user1 sudo

You can also remove a user from the specified group by using the following syntax:

gpasswd -d username groupname

And to give you another example, let’s remove user1 from the sudo group. Run the following command:

gpasswd -d user1 sudo

Here’s how the output should look:

Removing user user1 from group sudo

Add an Existing User to Multiple Groups

Adding an existing user to multiple groups is very similar to adding them to just one – we can do so in a single command by using the following syntax:

usermod -a -G group1,group2 user

For example, add a user named user1 to the group sudo and www-data using the following command:

usermod -a -G sudo,www-data user1

Add Multiple Users to a Single Group

If you want to add multiple users to a single group, use the following syntax:

gpasswd -M user1,user2,user3 groupname

For example, we can add multiple users named user1, user2, and user3 to the users group with the following command:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
gpasswd -M user1,user2,user3 users

Create a New User and Assign a Group at Once

In some cases, you will need to create a new account and assign it to a specific group. We can accomplish this by using the -G option combined with the useradd command. In that case, you can use the following syntax:

useradd -G groupname newuser

For example, to assign the group sudo for the new user while creating a new user named user4, run the following command:

useradd -G sudo user4

Change a User’s Primary Group

You can also change the primary group of any user using the -g option combined with the usermod command.

For example, to change the primary group of the user user4 to www-data, run the following command:

usermod -g www-data user4

In the above guide, you learned how to add users to a group in Linux with a few practical examples. We also covered how to change what group an account is in, assigning one while creating a new user account, and so on. I hope this will help you on the Linux operating system that you use, such as Debian, Ubuntu, CentOS or Fedora.


Managing several groups and configuring your server’s permissions in detail can be a frustrating and tedious task. If you use our Managed Linux VPS hosting services, you can simply ask our expert support team to set up groups and assign specific permissions for each group, and even configure your files to have these permissions set up. They’re always available and can help you with almost any aspect of managing and maintaining your server.

If this post helped you configure your groups, we would appreciate you sharing it on social media with our share shortcuts. You can also leave any questions or additional tips in our comment section below. Thanks!

1 thought on “How to Add a User to a Group on a Linux Server”

Leave a Comment