How to Add User to Group or Secondary Group in Linux
One of the essential components in any operating system, regardless of the purpose of this, is the users, and these may have certain types of privileges within the system and the applications that are handled there.
Now, one of the elements that are linked to the users are the groups, which can be of different types based on the requirements of the organization and possibly some user at a particular moment must belong to one of these groups to carry out tasks of support, management or control over specific functions.
Today we will learn to add a user to a new group in Linux.
How to Create Group in Linux
By default, Linux has some already established groups, but, as we said before, for management or support reasons, it is necessary to create specific groups within the organization.
- To create a new group in Linux, we must use the “groupadd” command, and for this, we will execute the following syntax:
sudo groupadd new group
Add a User to an Existing Group in Linux
For this process, we must know the name of the user and the group in which it is to be assigned, and we will use the following syntax for it:
sudo usermod -a -G group user
3. Change Primary Group of a User in Linux
Another function that we can carry out in Linux is to edit the main groups for a user, this because Linux uses primary and secondary groups.
The user's login process and the files and folders created by the user will be assigned to the primary group.
In this case, we must use the -g parameter to define the primary group of a user:
sudo usermod -g user group
How to View a User's groups in Linux
1. To see the general list of available groups in Linux, we will execute the following line:
Groups
2. If we want to look at the ID number associated with each group we will run:
id
3. To see the groups in which a specific user is, we will execute the following:
user groups
4. Or, we can see the ID of the groups of that user:
user id
How to Create a New User and Assign it to a Group in Linux
1. We can create a new user and assign it immediately to one of the available groups, for this, we must use the following syntax:
useradd -G group new_user
2. Then, it is necessary to assign a password to this user with the following syntax:
user passwd
How to Add a User to Multiple Groups in Linux
We may need to add a user to multiple groups at once, for which the following syntax will be useful:
sudo usermod -a -G group1, group2, group3 user
See all system groups in Linux
Finally, if it is necessary to know in detail all the groups available in Linux, just execute the following command:
getent group
Conclusion: It's that simple to manage users and groups in Linux.