howtouselinux

3 Easy Ways to add user to group in Linux

Table of Contents

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 user groups in Linux

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

Each user is a member of one or more groups, and each group has access permissions that are determined by the group’s owning user and group ID (GID) values.

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.

Procedures to add user to group in Linux

  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 usermod command in Linux

You can easily add a user to a 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) option.

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.

You can use the usermod command to add a user to a 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.

For example, to add the user “bob” to the group “users”, you would use the following command:

usermod -a -G users bob

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.

useradd and adduser can also be used to add a user to a group, but usermod is more commonly used.

Add user to group using useradd command in Linux

The useradd command is used to add users to a Linux system. 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 add the user “bob” to the group “users”, you would use this command:

useradd -g users bob

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 using commas.

To add “bob” to both the “team” and “users” groups, we could 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.

Welcome to howtouselinux.com!

Our website is dedicated to providing comprehensive information on using Linux.

We hope you find our site helpful and informative, and we welcome your feedback and suggestions for future content.

Learn More

Facebook
Twitter
LinkedIn