The Wayback Machine - https://web.archive.org/web/20240901155712/https://www.geeksforgeeks.org/how-to-add-a-user-to-a-group-in-linux/
Open In App

How to Add User to a Group in Linux

Last Updated : 02 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.



Similar Reads

How to Change The Primary Group of a User in Linux?
In the field of Linux system administration, learning the modification of the primary group is essential. In this article, we are going to deliver concise details about user groups and how to manage them. What is a Primary Group?In Linux, every user is associated with a primary group. The primary group is a fundamental concept in Unix-like operatin
7 min read
Manually Add a User Without Tools Like useradd and adduser in Linux
Adding a user manually in Linux without using tools like useradd or adduser involves modifying system files directly. This method is not recommended for regular use, as it requires careful editing of system files and can lead to errors if not done correctly. However, in some situations, such as when these tools are not available or not working, it
9 min read
How to add User in Linux | useradd Command
useradd is a command in Linux that is used to add user accounts to your system. It is just a symbolic link to adduser command in Linux and the difference between both of them is that useradd is a native binary compiled with the system whereas adduser is a Perl script that uses useradd binary in the background. It makes changes to the following file
5 min read
How to add multiple users to a group at once in linux?
Managing user groups in Linux is an essential part of system administration. Often, you'll find the need to add multiple users to a specific group simultaneously. This article provides a detailed explanation of how to accomplish this task, covering every aspect, and includes examples for clarity. Understanding User Groups in LinuxIn Linux, user gro
5 min read
Configure Passwordless Sudo For A Specific User in Linux
Linux has a tough permission rule. Being a root user you are permitted to do anything however the normal user has only some allowed permissions. To run many other commands, they required to ask permissions from the superuser, using keyword sudo after which they need to enter the password. This requirement of the password after sudo command can be r
1 min read
User Management in Linux
A user is an entity, in a Linux operating system, that can manipulate files and perform several other operations. Each user is assigned an ID that is unique for each user in the operating system. In this post, we will learn about users and commands which are used to get information about the users. After installation of the operating system, the ID
4 min read
Anonymity and Privacy For Linux User
Today, more than 3 billion of the world's population is connected to the virtual world known as the internet in some way or another. And the size of the hard-wares and data centers is on the increase every year due to insufficient users' information holding capacity. The lack of awareness and knowledge behind the proper functioning of the internet
6 min read
Deleting a User in Linux using Python Script
Deleting a user from your system or server via a python script is a very easy task. You just need to pass the username of the user and the script will remove the details and all the files of that user.This python script uses userdel Linux command to delete the user.You can directly use userdel command if you remember the command but if you don't th
2 min read
Creating a User With an Expiry Date in Linux
Linux is a multi-user system and thus, it allows more than one person to interact with the system at the same time. There are numerous ways in which we can create new users in Linux. The system administrator has the responsibility to manage different users of the system. For types of users in Linux refer Users in Linux System Administration. User a
2 min read
How to Block or Disable Normal User Logins in Linux?
Here we will see how to block or disable normal user logins in Linux. This is a good idea to prevent Normal users from connecting to your system. We will see how to block Normal user logins using /etc/nologin file. We are going to tell the users that what is actually happening by showing them a message. The main function of /etc/nologin file is to
2 min read
Scylla – Phone Number &amp; User Information Gathering Tool in Kali Linux
Scylla is a free and open-source tool available on Github. Scylla is based upon the concept of Open Source Intelligence (OSINT). This tool is used for information gathering. Scylla is written in python language. You must have python language installed in your Kali Linux in order to use the Scylla tool. Scylla is an advanced tool that allows it's us
4 min read
How to Force User to Change Password at Next Login in Linux
For security reasons, you must frequently change the password of accounts on the system. In this article, we are going to see how we can force the user to change the password on the next login in Linux. We can do that by two methods, one is using `passwd` command and the other is by using the `chage` command. Using passwd commandTo force a user to
3 min read
User Space Debugging Tools in Linux
In this article, we will discuss debugging tools in Linux. Debugging Tools are those programs that allow us to monitor, control, and correct other program's error while they execute. Some of the debugging tools are as follows:- 'print Statement'Querying (/proc, /sys etc)Tracing (strace/ltrace)Valgrind (memwatch)GDB1. 'print' Statement The 'print' s
3 min read
Get Username by User ID in Linux
The need to get user names by User ID is frequently encountered by system administrators in the Linux world. This task is necessary to manage permissions, diagnose, or simply identify users of the Linux system for different management purposes. Fortunately, Linux provides several methods to achieve this objective, each with its different advantages
3 min read
How to list all locked user accounts in linux?
Linux is great for managing administration tasks, which include managing access, authorizations, and file permissions in a system. While managing user accounts and groups you might encounter some accounts which need to be blocked or locked in order to ensure safety. But once you lock an account how can you see which all accounts are locked or banne
4 min read
How to unlock a locked user account in linux?
To regulate access and maintain security, Linux/Unix systems must have the ability to lock and unlock user accounts. It offers the adaptability required to handle a range of security and operational scenarios while making sure that only authorized users have access. Some of the reasons why locking and unlocking user accounts is essential for system
6 min read
How to Grant Admin Privileges to a User in Linux
Linux is an operating system that is open source operating system. There are many distros available in Linux distribution like Ubuntu, Kali Linux, Arch Linux, and Red Hat Linux. In this article, we will understand how to give admin privileges to non-admin users in the Ubuntu Operating system. Introduction to Permissions, Users, and GroupsLinux is a
5 min read
What is umask and how to set umask of Apache user in linux
In the Linux environment, the user mask or umask plays the most important role in specifying default permission for newly created files and directories. Umast filters or subtracts permission bits from the default permissions, ensuring it is a more secure and controlled environment. In Apache, configuring the umask is important for managing the secu
4 min read
How to Check the Groups a User Belongs to in Linux?
This article shows how to check the groups a user belongs to in Linux operating systems. We introduce some concepts related to the topic and then describe how to do so using the groups command available on the terminal. Groups in LinuxAll Linux operating systems are designed as multi-user operating systems. This means that they provide the capabili
8 min read
How to restrict a user's login to certain times of the day or week in linux?
In this article, we explore the significance of time-based user login restrictions in Linux as an important security measure. By leveraging Pluggable Authentication Modules (PAM) and time configuration files, administrators can define specific periods when users are allowed or denied access. This detailed article outlines the step-by-step process,
4 min read
How to use User Quotas in Linux
The control of user quotas in Linux is a very effective facility that provides system administrators with the ability to limit disks usage of particular users or groups of users in order to protect against disk overloading. When quotas are set, it will be easier to stop any user or even a group from using most of the disks hence causing a DOS. Thir
7 min read
Introduction to Graphical User Interface of RedHat Linux Operating System
Red Hat Linux-based operating system from Red Hat designed for business. It's one of the highly used Enterprise Operating System in the Linux market. It's an open-source Operating System. Feasible to work on desktops, on servers, or on the cloud.  Red Hat and Its community-supported counterpart. Fedora, are among the most widely used Linux distribu
3 min read
How to Delete User in Linux | userdel Command
Managing user accounts is an essential aspect of Linux system administration. Whether you need to remove an unused account, revoke access for a departing employee, or clean up your system for security reasons, understanding how to delete a user in Linux is a crucial skill. In this guide, we will explore the userdel command, a powerful tool in Linux
6 min read
How to Change User Password in Linux | passwd Command
Securing user accounts is a fundamental aspect of maintaining a robust and secure Linux system. One essential task is changing user passwords regularly to prevent unauthorized access. The passwd command in Linux provides a straightforward and effective way to modify user passwords. This article will guide you through the process of changing user pa
8 min read
How to create a Shared Folder between two Local User in Linux?
This article shows how to set-up a shared folder between two local users in Linux. The shared directory/folder will be accessible to both the users, they will be able to read/write each other's file. Let us create shared directory /home/shareFolder for user Bob and Alice and add them to a common group named projectA. Note: You can create the uses B
2 min read
How to Add User to Sudoers in Ubuntu
Sudo is a command line tool in a Unix-based system that allows a regular user to run a command as the superuser or another user. Only the super user is allowed to modify the majority of the System-wide settings and configurations. It is not advisable to run any Linux machine as a super user, so always create a normal user for day-to-day usage. To u
5 min read
Group Management in Linux
There are 2 categories of groups in the Linux operating system i.e. Primary and Secondary groups. The Primary Group is a group that is automatically generated while creating a user with a unique user ID simultaneously a group with an ID the same as the user ID is created and the user gets added to the group and becomes the first and only member of
4 min read
How to Remove All Users From a Group in Linux?
A group is a collection object in the Linux operating system, which associates certain properties and access control to all its members. It is an efficient way for a system administrator to manage Access Control for its users. Groups are an important tool for a system administrator to maintain many aspects of the system such as defining roles for u
6 min read
How to Rename a Group in Linux?
Renaming a group in a Linux system is a straightforward but essential administrative task. Whether you're reorganizing your user management structure or enhancing security measures, this quick guide will walk you through the simple steps to rename a group. Linux provides a powerful command, 'groupmod' to make this process smooth and efficient. In t
3 min read
How to Delete a Group in Linux | groupdel command
Group management is a crucial aspect of Linux system administration, and understanding how to create, modify, and delete groups is essential for maintaining a secure and organized environment. In this article, we will delve into the process of deleting a group in Linux using the 'groupdel' command. We will explore the command syntax, options, and p
3 min read