Add User to Group in Linux Efficiently [5 Methods]


Deepak Prasad

Linux

Linux is a versatile and powerful operating system used by millions of users worldwide. One of the key aspects that make Linux so flexible is its robust user and group management system. This system enables administrators to easily manage access to various system resources, ensuring a secure and efficient working environment. In this comprehensive guide, we will focus on one crucial aspect of Linux user management: how to add user to group in Linux. Whether you are a seasoned system administrator or a Linux beginner, this guide will help you master the process of adding users to groups in Linux, giving you the confidence and knowledge to manage your system effectively.

 

Different types of groups in Linux

In Linux, there are two primary types of groups: primary groups and supplementary groups . Let's explore each type in more detail:

Primary Groups:

  • Every user in Linux is associated with a primary group. When a user is created, a primary group is assigned to them by default.
  • The primary group is specified in the user's entry in the /etc/passwd file.
  • The primary group is used primarily for defining the group ownership of files and directories created by the user.
  • The primary group has a unique group ID (GID) that distinguishes it from other groups on the system.
  • By default, files and directories created by a user are assigned the primary group as their group ownership.
  • Users can only have one primary group.

Supplementary (Secondary) Groups:

  • Supplementary groups, also known as secondary groups, are additional groups that a user can belong to.
  • Supplementary groups allow users to have additional group memberships beyond their primary group, providing them with access to shared resources and granting specific permissions.
  • A user can be a member of multiple supplementary groups simultaneously.
  • Supplementary groups are defined in the /etc/group file, where each group has a unique group ID (GID).
  • The supplementary groups a user belongs to are listed in the user's entry in the /etc/group file.
  • The group permissions and access rights associated with supplementary groups apply to files and directories based on the group ownership or group permissions.
  • Supplementary groups are used to grant additional privileges to users beyond what is available through their primary group.

 

How to choose if user should be added to primary or secondary group?

In Linux and Unix-based operating systems, users are assigned to groups to manage permissions and access to resources. A primary group is the main group that is assigned to a user, while secondary groups are additional groups that a user can belong to. When you create a user, they are automatically assigned a primary group, which is typically the same as their username.

You would add a user to a primary group when:

  1. You want to create a new user and set their default group.
  2. You want to change the primary group of an existing user to better manage their permissions and access.

Example: Suppose you are setting up a new web development project, and you create a user named 'webdev'. You may also create a group called 'webdev_group' and set that as the primary group for the 'webdev' user. This way, any files or directories created by the 'webdev' user will be owned by the 'webdev_group' by default.

You would add a user to a secondary group when:

  1. You want to grant a user access to resources or permissions associated with an additional group.
  2. You want a user to be a member of multiple groups to better manage their access to various resources.

Example: Suppose you have an existing user 'alice' who belongs to the primary group 'staff'. You need to give 'alice' access to a specific directory that only members of the 'project_x' group can access. In this case, you can add 'alice' to the 'project_x' group as a secondary group, which will give her the necessary permissions to access the directory.

 

How to identify primary or secondary group of a user?

Method-1: Creating a temporary file or directory

When a user creates a file or directory in Linux, the ownership of that file or directory is automatically assigned to the user and their primary group. This is an important factor to consider when choosing which group the user should be added to, as it affects the default permissions and access control for their files.

When a user creates a file or directory, the owner is set to the user's UID (User ID) and the group ownership is set to the user's primary GID (Group ID). The primary group ownership can be important for managing permissions and access to shared resources among multiple users within a group.

For example, let's consider a user named 'alice' with the primary group 'staff' and secondary groups 'project_x' and 'developers'. When 'alice' creates a new file, the ownership of the file will be set to the 'alice' user and the 'staff' group by default:

-rw-r--r-- 1 alice staff 0 Apr 28 12:00 file.txt

In this example, the file 'file.txt' is owned by the user 'alice' and the group 'staff'. The file permissions are set such that the owner ('alice') has read and write access, while the group members (other users in the 'staff' group) have read access, and other users have read access as well.

If you want to change the default group ownership for files and directories created by a specific user, you can change their primary group. This will affect newly created files and directories.

For example, if you want 'alice' to create files with the 'project_x' group ownership by default, you can change her primary group to 'project_x':

sudo usermod -g project_x alice

After changing the primary group, when 'alice' creates a new file, the group ownership will be set to 'project_x':

-rw-r--r-- 1 alice project_x 0 Apr 28 12:00 file.txt

This change in default group ownership can help you manage access and permissions for shared resources more effectively within a group.

 

Method-2: Using id command

In Linux, you can use the id command to identify a user's primary and secondary groups. When you run the id command followed by a username, it will display information about the user's UID (User ID), GID (Group ID), and the groups they belong to. The primary group is represented by the GID, and secondary groups are listed as supplementary groups.

Here's how you can use the id command:

id username

For example, if you want to check the primary and secondary groups for a user named 'alice', you would run:

id alice

The output will look something like this:

uid=1001(alice) gid=1001(staff) groups=1001(staff),1002(project_x),1003(developers)

In this example, the primary group for the user 'alice' is 'staff' (gid=1001), and the secondary groups are 'project_x' (gid=1002) and 'developers' (gid=1003).

 

Method-3: Using /etc/passwd file

Another way to find a user's primary group is to look at the /etc/passwd file. You can use the grep command to search for the specific user:

grep username /etc/passwd

For example, to find the primary group for the user 'alice':

grep alice /etc/passwd

The output will look something like this:

alice:x:1001:1001:Alice,,,:/home/alice:/bin/bash

The fourth field (1001 in this example) represents the GID of the primary group. You can then use the getent command to get the group name associated with this GID:

getent group 1001

The output will show the primary group name:

staff:x:1001:alice

 

Different methods to add user to group in Linux

Here are different methods to add user to group in Linux:

  1. usermod: A command-line utility to modify a user's account, including adding the user to a secondary group.
  2. gpasswd: A command-line utility for managing group memberships, allowing you to add or remove a user from a group.
  3. adduser: A higher-level command-line utility to create a new user or add an existing user to a secondary group (specific to Debian-based systems, like Ubuntu).
  4. Editing /etc/group file: Manually edit the /etc/group file to add the user to a group by appending the username to the appropriate group line.
  5. vigr command: A command-line utility to safely edit the /etc/group file by locking it to prevent concurrent edits.

 

Method-1: Using usermod command

The usermod command is a versatile utility that allows you to modify a user's account, including adding the user to primary and secondary groups. The command offers various options to modify different aspects of a user account, such as home directory, shell, and expiration date.

Syntax:

To add a user to a primary group:

usermod -g group_name user_name

To add a user to a secondary group:

usermod -aG group_name user_name

In both cases, replace group_name with the target group's name and user_name with the target user's name.

Example:

Add 'alice' to the primary group 'project_x':

sudo usermod -g project_x alice

Add 'alice' to the secondary group 'developers':

sudo usermod -aG developers alice

 

Method-2: Using gpasswd command

The gpasswd command is a utility designed for managing group memberships. It allows you to add or remove users from groups by directly modifying the /etc/group file, while also providing additional features, such as setting a group password.

Syntax:

gpasswd -a user_name group_name

Replace user_name with the target user's name and group_name with the target group's name.

Example:

Add 'alice' to the secondary group 'developers':

sudo gpasswd -a alice developers

 

Method-3: Using adduser command

The adduser command is a higher-level utility for creating new users or adding existing users to secondary groups on Debian-based systems, like Ubuntu. It provides a more user-friendly interface than the useradd command and automatically configures user settings based on system defaults.

Syntax:

adduser user_name group_name

Replace user_name with the target user's name and group_name with the target group's name.

Example:

Add 'alice' to the secondary group 'developers':

sudo adduser alice developers

 

Method-4: Editing /etc/group file

The /etc/group file contains group information and group memberships for users. You can manually edit this file to add user to group in Linux by appending the user's name to the appropriate group line, separated by a comma.

Example:

Add 'alice' to the secondary group 'developers' by opening the /etc/group file:

sudo vim /etc/group

Find the line starting with the target group (e.g., 'developers') and append ',alice' to the line:

developers:x:1002:bob,charlie,alice

Save and exit the file.

 

Method-5: Using vigr command

The vigr command is a utility that safely edits the /etc/group file by locking it to prevent concurrent edits. This command ensures that no other process can modify the file while you are editing it.

Example:

Add 'alice' to the secondary group 'developers' by running the vigr command:

sudo vigr

Find the line starting with the target group (e.g., 'developers') and append ',alice' to the line:

developers:x:1002:bob,charlie,alice

 

How to create new user which is part of pre-defined group

Method-1: Using useradd command

To create a user and assign them to specific primary and secondary groups in a non-interactive form, you can use the useradd command along with the usermod command. Here are the steps:

Create a new user with the useradd command. You can assign a primary group during user creation by using the -g option, followed by the primary group name. If the primary group does not exist, create it using the groupadd command before creating the user. To create a user without a home directory, use the -M option. To specify the shell, use the -s option followed by the shell path.

Syntax:

sudo useradd -g primary_group -M -s /bin/bash user_name

To add the user to secondary groups, use the usermod command with the -aG option, followed by a comma-separated list of secondary group names. If the secondary groups do not exist, create them using the groupadd command before adding the user.

Syntax:

sudo usermod -aG secondary_group1,secondary_group2 user_name

Example:

Create a new user 'alice' with the primary group 'project_x' and add her to secondary groups 'developers' and 'testers':

Create the primary group 'project_x' (if it doesn't already exist):

sudo groupadd project_x

Create the secondary groups 'developers' and 'testers' (if they don't already exist):

sudo groupadd developers
sudo groupadd testers

Add User to Group in Linux Efficiently [5 Methods]

Create a new user 'alice' and assign her to the primary group 'project_x':

sudo useradd -g project_x -M -s /bin/bash alice

Add 'alice' to the secondary groups 'developers' and 'testers':

sudo usermod -aG developers,testers alice

Add User to Group in Linux Efficiently [5 Methods]

Now, 'alice' will be a member of the primary group 'project_x' and the secondary groups 'developers' and 'testers'.

 

Method-2: Using adduser command

Yes, you can use the adduser command in Debian-based systems (like Ubuntu) to create a user and assign them to specific primary and secondary groups in a non-interactive form. Here are the steps:

Create a new user with the adduser command, specifying the primary group using the --ingroup option followed by the primary group name. If the primary group does not exist, create it using the groupadd command before creating the user. You can use the --disabled-password option to create a user without a password, and the --gecos option to skip the prompts for user information. To specify the shell, use the --shell option followed by the shell path.

Syntax:

sudo adduser --ingroup primary_group --disabled-password --gecos "" --shell /bin/bash user_name

To add the user to secondary groups, use the adduser command, followed by the user's name and the secondary group name(s).

sudo adduser user_name secondary_group

Example:

Create a new user 'alice' with the primary group 'project_x' and add her to secondary groups 'developers' and 'testers':

Create the primary group 'project_x' (if it doesn't already exist):

sudo groupadd project_x

Create the secondary groups 'developers' and 'testers' (if they don't already exist):

sudo groupadd developers
sudo groupadd testers

Add User to Group in Linux Efficiently [5 Methods]

Create a new user 'alice' with the primary group 'project_x', without a password and user information prompts:

sudo adduser --ingroup project_x --disabled-password --gecos "" --shell /bin/bash alice

Add User to Group in Linux Efficiently [5 Methods]

Add 'alice' to the secondary groups 'developers' and 'testers':

sudo adduser alice developers
sudo adduser alice testers

Add User to Group in Linux Efficiently [5 Methods]

Now, 'alice' will be a member of the primary group 'project_x' and the secondary groups 'developers' and 'testers'.

 

Frequently Asked Questions

How to add a user to a group in Linux?

sudo usermod -aG group_name user_name

How to add multiple users to a group in Linux?

sudo usermod -aG group_name user_name1
sudo usermod -aG group_name user_name2
sudo usermod -aG group_name user_name3
...

OR

sudo bash -c 'for user in user_name1 user_name2 user_name3; do usermod -aG group_name $user; done'

How to add user to group in Linux CentOS?

sudo usermod -aG group_name user_name

How to add user to group Ubuntu?

sudo adduser user_name group_name

How to create group Linux?

sudo groupadd group_name

How to add a user to a group in Oracle Linux?

Oracle Linux is based on Red Hat Enterprise Linux (RHEL), so the process of adding a user to a group is similar to CentOS. Use the usermod command to add user to group in Linux:

sudo usermod -aG group_name user_name

How do I create a group and add users in Linux?

sudo groupadd group_name
sudo usermod -aG group_name user_name1
sudo usermod -aG group_name user_name2

How do I give permission to a group in Linux?

To give permissions to a group in Linux, you can use the chmod command with the group permission notation (g). Here's a basic example:

sudo chmod g+rwX /path/to/directory_or_file

This command gives read, write, and execute (if it's a directory or already executable) permissions to the group owner of the specified file or directory.

How to list groups in Linux?

The getent command retrieves entries from the Name Service Switch libraries, which includes group information. To list all groups on the system, use the following command:

getent group

You can list all groups by displaying the contents of the /etc/group file, which contains group information in Linux:

cat /etc/group

You can use awk to list only the group names from the /etc/group file:

awk -F: '{print $1}' /etc/group

 

Summary

In Linux, adding users to groups is an essential aspect of managing permissions and access control. Various methods are available to add user to group in Linux, including usermod, gpasswd, adduser, and manual editing of the /etc/group file. The usermod command is a versatile utility that allows modifying user accounts, while gpasswd focuses on managing group memberships. On Debian-based systems, the adduser command provides a user-friendly interface for creating new users and adding them to groups. Alternatively, you can directly edit the /etc/group file or use the vigr command to safely modify the file while preventing concurrent edits. When creating a new user, you can assign them to specific primary and secondary groups using the adduser or useradd command along with additional options. Understanding these methods helps manage access and permissions more effectively in a Linux environment.

 

Views: 52

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can reach out to him on his LinkedIn profile or join on Facebook page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment


We try to offer easy-to-follow guides and tips on various topics such as Linux, Cloud Computing, Programming Languages, Ethical Hacking and much more.

Programming Languages

JavaScript

Python

Golang

Node.js

Java

Laravel