Open In App

How to Add User to a Group in Linux

Last Updated : 02 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A group in Linux is a way to put users with similar access and permissions in a collection. By using groups, an administrator can define access control and permissions for all the users belonging to that group. Without groups, the administrator would have to define roles for individual users however, with groups those roles can be given to a group which in turn, will apply to all the users in the group.

In this article, we shall learn how to add a user to a group. We shall see different methods to do the same.

What is Linux Group?

A Linux group is a collection of user accounts that share common access permissions to files, directories, and other system resources. Each user on a Linux system is associated with one or more groups, and groups are used to simplify the process of managing user access and privileges.

Key Characteristics:

  1. Group Identifier (GID): Each group is assigned a unique numerical identifier known as the Group ID (GID). The GID is used by the system to differentiate between groups and is associated with specific permissions.
  2. Group Membership: Users can belong to one or more groups. When a user is part of a group, they inherit the permissions assigned to that group. This simplifies the process of managing permissions for multiple users who require similar access levels.

Types of Groups:

  1. Primary Group: Every user has a primary group, which is the main group associated with their account. The primary group is specified in the user’s entry in the /etc/passwd file.
  2. Secondary Groups: Users can also belong to additional groups known as secondary groups. These groups provide supplementary permissions beyond those granted by the primary group.

Pre-requisites to Add a User to a Group in Linux

  • A linux machine with root/sudo access.
  • Basic understanding of Linux terminal.
  • Understanding of useradd and usermod command.

How to Add User to a Group in Linux while creating the user.

Step 1: Creating a user while adding it to a group.

We can create a user with the useradd command.

useradd [options] [username]


We can use the -G option followed by the group name to add this user to any group we want. For example, we shall create a user ‘dummy’ and add it to the sudo group with the following command.

useradd -G sudo dummy


This would create the user and simultaneously add it to the sudo group.

Picture4

Creating a new user and adding it to a group

Step 2: Verifying the groups of the user ‘dummy’.

To check the groups of user dummy, we can use the groups command.

#syntax
groups [username]


Type the following command in the terminal.

groups dummy


Output:

Picture5

Checking groups of dummy user.

As we can see, the dummy group is now a member of the sudo group.

How to Add User to a Group in Linux Which Already exist.

In this method, we shall create a new user for demonstration purposes. Then, we shall add it to an already existing group.

Step 1: Creating a user(Optional)

We shall create a test user using the useradd command. This command is used to add users to the linux system.

useradd geek


Here, we are using the username as geek. You are free to use any name of your choice.

Picture1

Adding geek user to the system

Now, to verify whether the user ahas been added or not, use the following command.

cut -d: -f1 /etc/passwd | grep 'geek


The cut command gets content from the file based on the condition given. Here

  • We are selecting the first field with option -f1.
  • The fields are divided by the delimites : passed with -d.
  • We access the /etc/passwd file which contains all the users that exists in the system. (You need either root or sudo access to read this file)
  • Then, we grep our user name ‘geek’ to see if it is added to the users list.
Picture2

Checking the user in /etc/passwd

Here, we are getting the output geek which means that user has been added successfully.

Step 2: Adding the created user to the sudoers group

Now, to add any user to a group, we have the usermod command which is for user modification.

The syntax is:

usermod [options] [other fields...] [username]


Here, we will use the -aG option which means append to group then the first argument after the -aG option we will pass the group name and then, finally the username.

In order to add the geek user to sudo group, we need the following modifications:

usermod -aG sudo geek


Once, this command is executed. You can check all the groups that the geek user belongs to by using the groups command.

groups geek


This will list all the groups that the user ‘geek’ is a member of.

Picture3

Verifying the user’s groups

As we can see, the geek user belongs to 2 groups, geek(every user gets a group of its own name) and sudo. Thus, we successfully added the user geek to sudo group.

Frequesty Asked Questions

Why would I need to add a user to a group in Linux?

Adding a user to a group in Linux is essential for managing access permissions. Groups allow you to organize users and define specific permissions for those users collectively. For example, if you have a project with multiple team members, creating a group for that project allows you to grant permissions to project-related files or directories to the entire group at once, simplifying access control.

What is the command-line method to add a user to a group in Linux?

The primary command for adding a user to a group in Linux is usermod.

The basic syntax is:

sudo usermod -aG <group_name> <username>

This command adds the specified user to the specified group. The -aG flags ensure that the user is appended to the group without affecting their other group memberships.

Are there graphical interfaces available for adding users to groups in Linux?

Yes, Linux desktop environments often provide graphical tools for user management. For instance, the GNOME desktop environment includes a “Users and Groups” application.

You can launch it using:

sudo gnome-control-center user-accounts

This interface allows you to manage user accounts, including group memberships, in a more user-friendly way.

Can I add a user to multiple groups simultaneously in Linux?

Yes, you can add a user to multiple groups simultaneously by including multiple group names in the usermod command, separated by commas.

For example:

sudo usermod -aG group1,group2,group3 username

This command adds the user to group1, group2, and group3 in a single operation.

How can I verify that a user has been successfully added to a group in Linux?

You can verify a user’s group membership using the groups command.

For example:

groups username

This command displays a list of groups to which the specified user belongs. Additionally, you can check the /etc/group file or use other configuration files to confirm group memberships for a particular user.

Conclusion

In this article we discussed hot to add a user to Linux groups . Linux Hroups which are integral for organizing users with shared access requirements, streamlining permissions management, and enhancing system security. Through Group Identifiers (GID) and primary/secondary group distinctions, users inherit permissions efficiently based on group memberships, reducing administrative complexities. This article outlined practical steps for adding users to groups in Linux using commands like useradd and usermod, catering to users with root or sudo access. Linux groups play a crucial role in providing a structured and secure approach to user access within the Linux environment, contributing to effective system administration.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads