+3 votes
265 views
How to add user to group or secondary group in Linux

in Linux / Unix by (552k points)
reopened | 265 views

1 Answer

+4 votes
Best answer

1. How to create group in Linux
2. Add a user to an existing group in Linux
3. Change a user's primary group in Linux
4. How to view a user's groups in Linux
5. How to create a new user and assign it to a group in Linux
6. How to add a user to multiple groups in Linux
7. See all system groups in Linux

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 
image

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 
image

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 
image

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 
image
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 
image

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 
image
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 
image

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 
image

It's that simple to manage users and groups on Linux.


by (3.5m points)
edited

Related questions

+4 votes
1 answer
asked Apr 17, 2023 in Linux/Unix by backtothefuture (552k points) | 54 views
+5 votes
1 answer
+4 votes
1 answer
asked Oct 30, 2019 in Linux / Unix by backtothefuture (552k points) | 240 views
+5 votes
1 answer
asked Nov 18, 2019 in Windows 10 by backtothefuture (552k points) | 284 views
+5 votes
1 answer
asked Oct 15, 2019 in Linux / Unix by backtothefuture (552k points) | 273 views
Sponsored articles cost $40 per post. You can contact us via Feedback

Most popular questions within the last 30 days

10,659 questions
10,791 answers
510 comments
3 users