One of the most important components in any operating system, regardless of the purpose of this, are the users, and they may have certain types of privileges within the system and the applications that are managed there..
Now, one of the elements that are linked to the users are the groups, which can be of various types based on the requirements of the organization and possibly some user at a given time must belong to one of these groups to lead to perform support, management or control tasks on specific functions.
Today in TechnoWikis we will learn to add a user to a new group in Linux.
1. How to create group in Linux
By default, Linux has some groups already established, but, as we said before, for management or support reasons, it is necessary to create specific groups within the organization.
Step 1
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
2. 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 this:
sudo usermod -a -G user group
3. Change a user's primary group in Linux
Another of the functions that we can carry out in Linux is to edit the main groups for a user, this is because Linux uses primary and secondary groups.
The user's login process and the files and folders created by the user will be assigned in the primary group..
In this case we must use the -g parameter to define a user's primary group:
sudo usermod -g user group
4. How to view a user's groups in Linux
Step 1
To see the general list of groups available in Linux, we will execute the following line:
Groups
Step 2
If we want to see the ID number associated with each group we will execute:
id
Step 3
To see the groups in which a specific user is, we will execute the following:
user groups
Step 4
Or, we can see the ID of the groups of that user:
user id
5. How to create a new user and assign it to a group in Linux
Step 1
We have the possibility to create a new user and immediately assign it to one of the available groups, for this, we must use the following syntax:
useradd -G new_user group
Step 2
Then, it is necessary to assign a password to this user with the following syntax:
user passwd
6. How to add a user to multiple groups in Linux
We may have to add a user to multiple groups at the same time, for this the following syntax will be useful:
sudo usermod -a -G group1, group2, group3 user
7. See all system groups in Linux
Finally, if it is necessary to know in detail all the groups available in Linux, just run the following command:
getent group
It's that simple to manage users and groups on Linux.