
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.
Table of Contents
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:
You can also create specific groups with a name of your choice. create a new group named group1
with the following command:
Once a user has been created, you can then view the groups that the specific user account is in with the following command:
Running that command should give you the following output:
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:
Adding an Existing User to a Group
You can add an existing user to a secondary group – just use the following syntax:
For example, to add user1
to the sudo
group, run the following command:
You can now verify this change with the following command:
That should give you the following output:
You can also remove a user from the specified group by using the following syntax:
And to give you another example, let’s remove user1
from the sudo
group. Run the following command:
Here’s how the output should look:
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:
For example, add a user named user1
to the group sudo
and www-data
using the following command:
Add Multiple Users to a Single Group
If you want to add multiple users to a single group, use the following syntax:
For example, we can add multiple users named user1
, user2
, and user3
to the users
group with the following command:
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:
For example, to assign the group sudo
for the new user while creating a new user named user4
, run the following command:
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:
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!
thank you very the article is important keep it on supporting other