How to add or remove user from a group in Linux. How to add a user to a group in Linux. How to add a user more than one group in Linux. How to remove a user from group. Delete a user from a group.

 

Add a user to a group

If a user already exists then we can use ‘usermod‘ to add the user to a group.

I already have a user ‘deepak

[[email protected] ~]# id deepak
uid=1002(deepak) gid=1000(deepak) groups=1000(deepak)

And I have a group ‘mygroup

To add a user to a group

Syntax:

# usermod -a -G GROUP1,GROUP2 USER

Explanation of the arguments

-a, --append
  Add the user to the supplementary group(s). Use only with the -G option.

-G, --groups GROUP1[,GROUP2,...[,GROUPN]]]
  A list of supplementary groups which the user is also a member of. Each group is separated from the next by a comma, with no intervening whitespace.

Example:

[[email protected] ~]# usermod -a -G mygroup deepak
NOTE:
To add a secondary group we use “-G” but if you wish to change the primary group of a user then use “-g

To verify the changes

[[email protected] ~]# id deepak
uid=1002(deepak) gid=1000(deepak) groups=1000(deepak),1001(mygroup)

[[email protected] ~]# groups deepak
deepak : deepak mygroup

To add a user to multiple groups

[[email protected] ~]# usermod -a -G mygroup,techteam deepak
[[email protected] ~]# id deepak
uid=1002(deepak) gid=1000(deepak) groups=1000(deepak),1001(mygroup),1002(techteam)

 

Remove a user from group

To remove a user from group there are two methods which I know of. The most recommended way is by using gpasswd, but you can also use ‘usermod

 

Remove user from group using gpasswd

The gpasswd command is used to administer /etc/group, and /etc/gshadow.

Syntax:

gpasswd -d

Here user ‘deepak‘ is part of multiple groups.

[[email protected] ~]# groups deepak
deepak : deepak mygroup techteam

Example:

[[email protected] ~]# gpasswd -d deepak mygroup
Removing user deepak from group mygroup

Verify the changes

[[email protected] ~]# groups deepak
deepak : deepak techteam

 

Remove user from group using usermod

The usermod command if used with “-a” will append a group to the provided user but when used without “-a” will remove all other secondary groups and will keep only the provided groups for the user.

Syntax:

usermod -G <secondary_group1,secondary_group2>

This command changes the secondary group membership to match the values given in the command. You must list all the groups required (including any groups of which the user is already a member):

For the user, determine which groups they are already a member (in this example, “deepak” is the primary group and “mygroup techteam” is the secondary group):

[[email protected] ~]# groups deepak
deepak : deepak mygroup techteam

In this example, we will remove ‘deepak‘ from the “mygroup” group. Use usermod to do this:

[[email protected] ~]# usermod -G techteam deepak

Verify the changes

[[email protected] ~]# groups deepak
deepak : deepak techteam

To remove all the secondary groups from a user

[[email protected] ~]# groups deepak
deepak : deepak mygroup techteam

Use usermod “” with “-G

[[email protected] ~]# usermod -G "" deepak

Verify the changes

[[email protected] ~]# groups deepak
deepak : deepak

 

HINT:
You can additionally use system-config-users to modify all the users and groups related changes.

 

Lastly I hope the steps from the article to add or remove user from a group on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *