Skip to Content

3 Easy Ways to add user to group in Linux

This article is part of the following series.

 

In Linux, you can add a user to a group in several ways. In this post, we’ll show you three different methods for doing so.

To add a user to a group in Linux, we can use usermod command, useradd command or GUI. Each has its own advantages and disadvantages, so be sure to choose the one that best suits your needs.

Understanding users and groups in Linux

Users and groups in Linux allow system administrators to control access to files, directories, and other resources on a system.

A user is an individual who has access to the system and can execute commands and perform various tasks on the computer. Each user is identified by a unique username and a user ID (UID).

A group is a collection of users who have some common permissions and access rights. Each group is identified by a unique group name and a group ID (GID).

Each user is a member of one or more groups.

Group membership can be controlled through the use of the /etc/group file, which contains a list of all user groups on the system, as well as the GID values for each group.

Add user to group using usermod command in Linux

You can easily add a existing user to a existing group in Linux using usermod command. It is used to modify a user’s account information, including the user’s group membership. we just need to use the -a (–append) and -G options.

For example, to add the user john to the group sales, we would run the following command:

usermod -a -G sales john

Adding a user to a group allows the user to inherit the permissions of that group.

The syntax for this command is as follows:

usermod -a -G groupname username

  • The -a flag tells usermod to add a user to a group.
  • The -G flag specifies the name of the secondary group to which you want to add the user.

 

Replace group with the name of the group you want to add the user to, and replace username with the name of the user you want to add.

The -a flag stands for “append”, so this command will append the user to the specified group.

If you want to add a user to multiple groups, you can use the same command as above.

But, you should separate the group names to which you want to add the user.

To add “bob” to both the “team” and “users” groups, we could use this command:

usermod -a -G team,users bob

Adding a user to a group is a simple way to give that user additional permissions without changing the permissions of the files themselves.

To check if a user is a member of a specific group, you can use the following commands:

id -Gn username | grep groupname

groups username

Replace username with the name of the user you want to check, and groupname with the name of the group you want to verify the user’s membership in.

The output of the above command will display the groups that the user is a member of, and if the specified group is in the list, then the user is a member of that group.

Procedures to add user to group in Linux with usermod command

  1. Log in to your Linux server
  2. Switch to the root user 
  3. Add the user to the desired group with usermod -a -G groupname username
  4. Exit from the root user 
  5. Log out of your server

 

Add user to group using useradd command in Linux

The useradd command is typically used to create new users. If you need to add a new user to group, you can use useradd command.  

The syntax for the useradd command is as follows:

useradd -g groupname -G groups username

  •   -g, –gid GROUP        force use GROUP as new primary group
  •  -G, –groups GROUPS      new list of supplementary GROUPS

 

Replace groupname with the name of the group you want to add the user to, and replace username with the name of the user you want to add.

For example, to create a new user bob and add this user to the group “users”, you can use this command:

useradd -g users bob

If you want to add a new user to multiple groups, you can use the same command as above, but you should separate the group names using commas.

To add a new user “bob” to both the “team” and “users” groups, we can use this command:

useradd -G team,users bob

Suppose we want to create a new user called bob and add them to the primary group “team” and secondary group “users”. We could do so using this command:

sudo useradd -g team -G users bob

Add user to group using graphical interface in Linux

In order to add a user to a group using the graphical interface, open the “Users and Groups” tool from the System Settings window.

Click on the “Groups” tab, then double-click on the group you want to add the user to. In the window that appears, click on the “Members” tab, then click on the “Add” button.

A new window will appear; from here, you can search for the user you want to add by name or by account type. Once you have found the user, select them and click on the “Add” button. The user will now be added to the group.

Understanding primary group and secondary group in Linux

In Linux, there are two types of groups: primary groups and secondary groups.

A user’s primary group is the group that is assigned to the user when the user is created. The primary group ID (GID) is stored in the /etc/passwd file.  This is the same as your login name and is the main group of which your user is a part.

A user’s secondary groups are any other groups that the user is a member of. The secondary group IDs (GID) are stored in the /etc/group file.  Secondary groups, also known as supplementary groups.

In this post, we’ve shown you how to add a user to a group using both the command line and the graphical interface. We hope you found this information helpful! Have you tried adding users to groups before? What was your experience like? Let us know in the comments below.