Four methods to add an User to specific group in Linux

Linux groups are organization units used to manage user accounts in Linux.

It has unique numerical identification number for each users and groups in the Linux system.

It’s called a userid (UID) and a groupid (GID). The main purpose of groups is to define a set of privileges to the members of the group.They all can perform the particular operations but not others.

There are two types of default groups available in Linux. Each user should have exactly one primary group and any number of secondary groups.

  • Primary Group: Primary group has been added to the user during the user account creation. It’s typically the name of the user. The primary group is applied to the user when performing any actions such as creating new files (or directories), modifying files, or executing commands, etc,. The user primary group information is stored in the /etc/passwd file.
  • Secondary Group: Is known as Supplementary Groups. It allows the group of users to perform the particular action in the same group members files.For example, if you would like to allow few users to run Apache (httpd) service command then it will suit perfectly.

You may interested in the following articles which is related to user management.

It can be done using the following four methods.

  • usermod: The usermod command modifies the system account files to reflect the changes that are specified on the command line.
  • gpasswd: The gpasswd command is used to administer /etc/group, and /etc/gshadow. Every group can have administrators, members and a password.
  • Shell Script: Shell scripts allows administrator to automate the required tasks.
  • Manual Method: We can manually add the users into any group by editing the /etc/group file.

I assume that you already have the necessary group and users for this activity. In this example, we are going to use following users user1, user2, user3 and groups  mygroup and mygroup1.

Before making the changes, I would like to check the users and group information.I could see the below users were associate with their own group and not with others.

# id user1
uid=1008(user1) gid=1008(user1) groups=1008(user1)

# id user2
uid=1009(user2) gid=1009(user2) groups=1009(user2)

# id user3
uid=1010(user3) gid=1010(user3) groups=1010(user3)

I could see there is no users are associated in this group.

# getent group mygroup
mygroup:x:1012:

# getent group mygroup1
mygroup1:x:1013:

Method-1: Using usermod command

The usermod command modifies the system account files to reflect the changes that are specified on the command line.

How to add an existing user to Secondary or Supplementary group using usermod command?

To add an existing user to a secondary group, use the usermod command with -G option and the name of the group.
Syntax

# usermod [-G] [GroupName] [UserName]

You will be getting an error message if the given user or group doesn’t exist in your system. If you doesn’t get any error then the user has been added to the corresponding group.

# usermod -a -G mygroup user1

Let us see the output using id command. Yes, it’s added successfully.

# id user1
uid=1008(user1) gid=1008(user1) groups=1008(user1),1012(mygroup)

How to add an existing user to multiple secondary or supplementary groups using usermod command?

To add an existing user to multiple secondary groups, use the usermod command with -G option and the name of the groups with comma.

Syntax

# usermod [-G] [GroupName1,GroupName2] [UserName]

In this example, we are going to add the user2 into mygroup and mygroup1.

# usermod -a -G mygroup,mygroup1 user2

Let me see the output using id command. Yes, user2 is successfully added into mygroup and mygroup1.

# id user2
uid=1009(user2) gid=1009(user2) groups=1009(user2),1012(mygroup),1013(mygroup1)

How to change a User’s Primary group?

To change a user’s primary group, use the usermod command with -g option and the name of the group.

Syntax

# usermod [-g] [GroupName] [UserName]

We have to use -g to change user’s primary group.

# usermod -g mygroup user3

Let’s see the output. Yes, it has been successfully changed. Now, it’s showing mygroup as user3 primary group instead of user3.

# id user3
uid=1010(user3) gid=1012(mygroup) groups=1012(mygroup)

Method-2: Using gpasswd Command?

The gpasswd command is used to administer /etc/group, and /etc/gshadow. Every group can have administrators, members and a password.

How to add an existing user to Secondary or Supplementary group using gpasswd command?

To add an existing user to a secondary group, use the gpasswd command with -M option and the name of the group.

Syntax

# gpasswd [-M] [UserName] [GroupName]

In this example, we are going to add the user1 into mygroup.

# gpasswd -M user1 mygroup

Let us see the output using id command. Yes, user1 is successfully added into mygroup.

# id  user1
uid=1008(user1) gid=1008(user1) groups=1008(user1),1012(mygroup)

How to add  Multiple User’s to Secondary or Supplementary group using gpasswd command?

To add the multiple users to a secondary group, use the gpasswd command with -M option and the name of the group.

Syntax

# gpasswd [-M] [UserName1,UserName2] [GroupName]

In this example, we are going to add the user2 and user3 into mygroup1.

# gpasswd -M user2,user3 mygroup1

Let us see the output using getent command. Yes, user2 and user3 are successfully added into mygroup1.

# getent group mygroup1
mygroup1:x:1013:user2,user3

How to Remove an user from a group using gpasswd Command?

To remove the user from the group, use the gpasswd command with -d option and the name of the user and group.

Syntax

# gpasswd [-d] [UserName] [GroupName]

In this example, we are going to remove the user1 from mygroup.

# gpasswd -d user1 mygroup
Removing user user1 from group mygroup

Method-3: Using Shell Script

Based on the above examples what we came to know is the usermod command is not capable to add multiple users into the group but it can be done through the gpasswd command.

However, it will overwrite the existing users which are currently associated on the group.

For example, user1 has already associated with mygroup. If you would like to add user2 and user3 into the mygroup with gpasswd command, it doesn’t work as expected and it over right the group instead of modifying it.

What would be the solution if you would like to add multiple users to multiple groups?

There is no default option available in both of the commands to achieve this.

Hence, we need to write a small shell script to achieve this.

Method-3a: How to add Multiple Users to Secondary or Supplementary group using gpasswd command?

Create the following small shell script if you would like to add the multiple users to secondary or supplementary group using gpasswd command.

Create The Users list. Each user should be in separate line.

$ cat user-lists.txt
user1
user2
user3

Use the following shell script to add multiple users to single secondary group.

vi group-update.sh

#!/bin/bash
for user in `cat user-lists.txt`
do
usermod -a -G mygroup $user
done

Set an executable permission to group-update.sh file.

# chmod + group-update.sh

Finally run the script to achieve this.

# sh group-update.sh

Let us see the output using getent command. Yes, user1, user2 and user3 are successfully added into mygroup.

# getent group mygroup
mygroup:x:1012:user1,user2,user3

Method-3b: How to add Multiple users to Multiple Secondary or Supplementary group using gpasswd command?

Create the following small shell script if you would like to add the multiple users into multiple secondary or supplementary group using gpasswd command.

Create the Users list. Each user should be in separate line.

$ cat user-lists.txt
user1
user2
user3

Create the Groups list. Each group should be in separate line.

$ cat group-lists.txt
mygroup
mygroup1

Use the following shell script to add multiple users to multiple secondary groups.

#!/bin/sh
for user in `more user-lists.txt`
do
for group in `more group-lists.txt`
do
usermod -a -G $group $user
done
done

Set an executable permission to group-update-1.sh file.

# chmod +x group-update-1.sh

Finally run the script to achieve this.

# sh group-update-1.sh

Let me see the output using getent command. Yes, user1, user2 and user3 are successfully added into mygroup.

# getent group mygroup
mygroup:x:1012:user1,user2,user3

Also, user1, user2 and user3 are successfully added into mygroup1.

# getent group mygroup1
mygroup1:x:1013:user1,user2,user3

Method-4: Manual method to add a user to a group in Linux?

We can manually add the users into any group by editing the /etc/group file.

Open the /etc/group file and search the group name where you want to update the users. Finally update the Users into the corresponding group.

# vi /etc/group

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...