October 24

Linux user and group management commands

How to See Which Groups Your Linux User Account Belongs To?


How to add a new group? 

sudo groupadd

How to add user to a group?

usermod -a -G  

Change a User’s Primary Group

While a user account can be part of multiple groups, one of the groups is always the “primary group” and the others are “secondary groups”. The user’s login process and files and folders the user creates will be assigned to the primary group.

To change the primary group a user is assigned to, run the usermod command, replacingexamplegroup with the name of the group you want to be the primary and exampleusernamewith the name of the user account.

usermod -g  

Note the -g here. When you use a lowercase g, you assign a primary group. When you use an uppercase -G , as above, you assign a new secondary group.

View the Groups a User Account is Assigned To

To view the groups the current user account is assigned to, run the groups command. You’ll see a list of groups.


To view the numerical IDs associated with each group, run the id command instead:


Create a New User and Assign a Group in One Command

useradd -G examplegroup exampleusername

(You’ll want to assign a password for that user afterwards, of course:)

passwd jsmith

Add a User to Multiple Groups

usermod -a -G group1,group2,group3 exampleusername

View All Groups on the System

getent group

Send us a message. We will reply as soon as we can.