How To Add User To A Group In Linux – POFTUT

How To Add User To A Group In Linux


Linux uses groups in order to share resources in a easy way. In general a resource will have a user, group and other access rights. In this tutorial we will learn how to add user to a Linux group and its variations like creating group, deleting group, listing group members etc.

Linux Groups

Linux groups are organizational units used to organize and administrator user account and rights. Especially for privilege and access management like reading, writing, executing permissions for a given resource or login into a system can be managed with groups by allowing some group and denying some group.

There are two type of groups in Linux operating system like below.

Primary Group

Primary Group is the user first chose group which means if a user creates a file the file group will be assigned to the user primary group. If a user try to use some resources first the primary group will be checked. Primary group information can be viewed in the /etc/passwd file like below.

$ cat /etc/passwd
Primary Group
Primary Group

The 5th column provides the primary group information. As we can see some users do not have primary group because they are nor operational and used by operating system so they do not needs primary group.

Secondary or Supplementary Group

Secondary group is used to provide access rights for other resources which is not the user primary group. For example a file created by other user with the different primary group can be shared by using secondary group. A user may have multiple secondary groups.

Change or Add Existing User To A Primary Group

We will start by adding an existing user to ta primary group. Caution because this will change the primary group of the user which can create problems because of the file and folder ownership and access restrictions. We will use usermod command with -g option with a primary group name and -a to append .

LEARN MORE  How To Give Su Rigth To User In FreeBSD?

In this example we will add user ismail to the group named ali as primary group.

$ sudo usermod -a -g ali ismail

Add Existing User To A Secondary Group

We will start by adding existing user to a existing group. We will use usermod command. usermod is a command used to manage users and groups. We will also provide -a which means append and -G where we will specify the group we want to add the user.

In this exmaple we will add user ismail to the group syslog as secondary group.

$ sudo usermod -G syslog ismail

Add Existing User To Multiple Groups

In some cases we may need to add a user into multiple groups with a single command. Actually we will use the same command and parameters used for single group. We will delimit the group names with , .

In this example we will add user ismail into multiple secondary groups root, sudo , syslog

$ sudo usermod -a -G root,sudo,syslog ismail

Remove User From Group

We can remote a user from a group. This group can be primary or secondary group. We will use gpasswd with the -d option. In this example we will remote user ismail from group syslog.

$ sudo gpasswd -d ismail syslog

List Existing Groups

Linux group information is stored in the file /etc/group . We can list this file like below which will provide the groups.

$ cat /etc/group
List Existing Groups
List Existing Groups

List Existing Groups with getent Command

Another good command to list existing groups is getent command by providing the group option.

$ getent group
List Existing Groups with getent Command
List Existing Groups with getent Command

Create Group

If we need to add a new group before adding a user to this group we need to create it. We will create new group with the groupadd . In this example we will create a group named test.

$ sudo groupadd test

Delete Group

We can delete existing user group with the groupdel command. In this example we will delete the group named test.

$ sudo groupdel test

Create New User and Assign A Group with Single Command

There may be some scenarios where we need to create a user and assign groups to this user multiple groups. We will use useradd we will specify the primary user group with -g parameter and secondary groups with -G parameter.

$ sudo useradd -g ismail -G wheel,developers ismail

Display User Group with id Command

We can display existing user groups with different commands. id command is a basic command which will list user name, user primary group, user secondary group and related id.

$ id
Display User Group with id Command
Display User Group with id Command

Display User Group with groups Command

Another command which can list current user groups is groups command.

$ group
Display User Group with groups Command
Display User Group with groups Command

Leave a Comment