Add A User To A Group In Linux

In Linux users are associated with the groups. These groups are used to set permissions about files and folders. Users may have single or more groups related to them. The second group is called as secondary group . In this tutorial, we examine how to add a user into a group for different scenarios like the first group, secondary group.

Add New Group

Linux users can be assigned or added to the groups. If the group we want to add user do not exist first we should add or create a new group. The groupadd command is used to add a new group in a Linux system. In the following example, we add a new group named developers .

$ sudo groupadd developers

List All Groups

Currently existing users can be listed with the getent command providing the group parameter like below.

$ getent group
List All Groups

This output provides the group name, group terminal, group ID, and member user names. In the following example, adm is the group name, x is the terminal which is nothing, 4 is the group ID, “syslog,ismail” are member users.

adm:x:4:syslog,ismail

Add Existing User to Group

An existing user can be added to a group by using the usermod command. The -a parameter is provided to add and -G parameter is provided to specify the group name. The username is provided as the last parameter. The syntax is like below.

usermod -a -G GROUP USER
  • GROUP is the group name we want to add the USER.
  • USER is the user name we want to add to the GROUP.

In the following example, we add the user “ismail” into the group named “developers”.

$ sudo usermod -a -G developers ismail

Change User Primary Group

Every user in Linux has a primary group. The primary group is used to set user-created files group ownership etc. This primary group is generally created as the user name when a regular user is created. For example, during the creation of the user “ismail” a group called “ismail” is also created and set as the primary group for the user “ismail”.We can change the user primary group with the usermod command like below. The syntax is like below.

usermod -g PRIMARY_GROUP USER
  • PRIMARY_GROUP is the primary group we want to set for the USER.
  • USER primary group set as PRIMARY_GROUP.

In the following example, we set the primary group of the user “ismail” as the “developers”.

$ sudo usermod -g developers ismail

List User Groups

Every user has single or more groups assigned. These user-assigned groups can be listed and displayed in different ways by using different commands. The groups command displays the current user groups like below.

$ groups
ismail adm cdrom sudo dip plugdev lpadmin lxd sambashare docker

The primary group for the current user is displayed as the first group name.

Alternatively, the id command prints the user groups with their group ID information like below.

$ id
uid=1000(ismail) gid=1000(ismail) groups=1000(ismail),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),121(lpadmin),132(lxd),133(sambashare),997(docker)

The groups command can be also used to displayed specified user assigned groups. In the following example, we list all groups related to the user “ismail”.

$ groups ismail
ismail : ismail adm cdrom sudo dip plugdev lpadmin lxd sambashare docker

Add New User Assigning To Group

The useradd command is used to add a new user to the Linux system. While adding a new user a group can be assigned to this user. the -G option is used to specify the group name we want o assign. In the following example, we add a new user named “ismail” and assign the group “developers” to this user.

$ sudo useradd -G developers ismail

Add User To Multiple Groups

A user can be added into multiple groups with a single command. The usermod command is used to add a user into multiple groups. The group names are provided with the -G option and group names are separated with a comma. In the following example, we add the user “ismail” into the groups named developers, admins, testers.

$ sudo usermod -a -G developers,admins,testers ismail

Remove User From A Group

A user can be removed from a group by using the gpasswd command. The -d option is provided to specify the username and the group name is provided as the last parameter. The syntax is like below.

gpasswd -d USER GROUP

In the following example, we remove the user “ismail” from the group named “developers”.

$ sudo gpasswd -d ismail developers

Delete Existing Group

An existing group can be deleted with the groupdel command. The group name we want to delete is provided as a parameter. In the following example, we delete the group named “developers”.

$ sudo groupdel developers

Leave a Comment