SoFunction
Updated on 2025-04-07

Four ways to add users to specific groups in Linux

Linux groups are organizational units used to manage user accounts in Linux. For every user and group in the Linux system, it has a unique numerical identification number. It is called User ID (UID) and Group ID (GID). The main purpose of a group is to define a set of privileges for the members of the group. They can all perform specific operations, but not others.

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

Primary Group: When creating a user account, the primary group has been added to the user. It is usually the name of the user. When performing anything such as creating a new file (or directory), modifying a file, or executing a command, the main group will be applied to the user. The user's main group information is stored in the /etc/passwd file.

Secondary Group: It is called a secondary group. It allows user groups to perform specific actions in the same group of member files. For example, if you want to allow a small number of users to run the Apache (httpd) service command, it will be very suitable.

You may be interested in the following related user management.

  • Three ways to create a user account in Linux?
  • How to create batch users in Linux?
  • How to update/change user password using different methods in Linux?

The following four methods can be implemented.

  • usermod: Modify the system account file to reflect changes specified in the command line.
  • gpasswd: used to manage /etc/group and /etc/gshadow. Each group can have an administrator, member, and password.
  • Shell scripts: Let administrators automate the required tasks.
  • Manual way: We can manually add users to any group by editing the /etc/group file.

I'm assuming you already have the groups and users you need for this operation. In this example, we will use the following users and groups: user1, user2, user3, and the other groups are mygroup and mygroup1.

I would like to check the user and group information before making the changes. See below for details.

I can see that the users below are associated with their own group, not with other groups.

# 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 can see that there are no associated users in this group.

# getent group mygroup
mygroup:x:1012:
# getent group mygroup1
mygroup1:x:1013:

Method 1: Use the usermod command

The usermod command modifies the system account file to reflect the changes specified on the command line.

How to add an existing user to a secondary or attached group using the usermod command?

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

grammar:

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

If the given user or group does not exist on the system, you will receive an error message. If you don't get any errors, the user has been added to the corresponding group.

# usermod -a -G mygroup user1

Let me use the id command to view the output. Yes, the addition was successful.

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

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

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

grammar:

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

In this case, we will add user2 to mygroup and mygroup1.

# usermod -a -G mygroup,mygroup1 user2

Let me use the id command to view the output. Yes, user2 has been successfully added to myGroup and myGroup1.

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

How to change the main group of users?

To change the user's main group, use the usermod command with the -g option and group name.

grammar:

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

We have to use -g to change the main group of users.

# usermod -g mygroup user3

Let's look at the output. Yes, it has been changed successfully. Now, the main group of user3 is displayed is mygroup instead of user3.

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

Method 2: Use the gpasswd command

The gpasswd command is used to manage /etc/group and /etc/gshadow. Each group can have an administrator, member, and password.
How to add an existing user to a secondary or additional group using the gpasswd command?

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

grammar:

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

In this case, we will add user1 to mygroup.

# gpasswd -M user1 mygroup

Let me use the id command to view the output. Yes, user1 has been successfully added to mygroup.

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

How to add multiple users to a secondary or additional group using the gpasswd command?

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

grammar:

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

In this case, we will add user2 and user3 to mygroup1.

# gpasswd -M user2,user3 mygroup1

Let me use the getent command to view the output. Yes, user2 and user3 have been successfully added to myGroup1.

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

How to delete a user from a group using the gpasswd command?

To delete a user from a group, use the gpasswd command with the -d option and the names of the user and group.

grammar:

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

In this case, we will remove user1 from mygroup.

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

Method 3: Use Shell Scripts

Based on the example above, I know that the usermod command does not have the ability to add multiple users to the group, which can be done with the gpasswd command. However, it will overwrite the existing user currently associated with the group.

For example, user1 is already associated with mygroup. If you want to add user2 and user3 to mygroup using the gpasswd command, it will not take effect as expected, but will modify the group.

What's the solution if you want to add multiple users to multiple groups?

There is no default option in either command to achieve this.

So we need to write a small shell script to achieve this.

How to add multiple users to a secondary or attached group using the gpasswd command?

If you want to add multiple users to a secondary or attached group using the gpasswd command, create the following shell script.

Create a user list. Each user should be in a separate line.

$ cat 
user1
user2
user3

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

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

Set the executable permissions of the file.

# chmod + 

Finally run the script to implement it.

# sh 

Let me see the output using the getent command. Yes, user1, user2, and user3 have been successfully added to mygroup.

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

How to add multiple users to multiple secondary or additional groups using the gpasswd command?

If you want to add multiple users to multiple secondary or additional groups using the gpasswd command, create the following shell script.

Create a user list. Each user should be in a separate line.

$ cat 
user1
user2
user3

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

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

Set the executable permissions of the file.

# chmod +x 

Finally run the script to implement it.

# sh 

Let me see the output using the getent command. Yes, user1, user2, and user3 have been successfully added to mygroup.

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

Additionally, user1, user2, and user3 have been successfully added to mygroup1.

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

Method 4: Manual method to add users to groups in Linux

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

Open the /etc/group file and search for the group name to be updated for the user. Finally, update the user to the corresponding group.

# vi /etc/group

This is the end of this article about four ways to add users to specific groups in Linux. For more related content related to adding users to specific groups, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!