How to Add User to Group in Linux 2024

Learn how to effortlessly add a user to a group in Linux systems and effectively remove them in this comprehensive tutorial. Our expert guide also covers essential techniques to create, delete, and list groups on your Linux machine. Master the process with our step-by-step instructions today.

How to Add User to Group in Linux

Linux Groups

In Linux, groups serve as units of organization and administration for user accounts. Their main function is to establish a set of privileges, including reading, writing, and executing permissions, for a specific resource that can be shared among group members.

Linux operating systems consist of two distinct categories of groups:

  • The Primary group: After a user generates a file, its group defaults to the user’s primary group, typically sharing the same name as the user. The /etc/passwd file retains the details of the user’s primary group.
  • Secondary or supplementary group: If you wish to assign particular file permissions to a group of users, adding those users to a group can be helpful. By way of illustration, if you add a particular user to the docker group, that user will obtain the group’s access privileges and become capable of executing docker commands.

The primary group of a user is unique, while they can be a member of multiple secondary groups.

Adding a user to a group is a privilege reserved for the root user or users with sudo access.

How to Add an Existing User to a Group

To assign a user to a supplementary group, utilize the command “usermod -a -G” and specify the group name and the user’s name:

ShellScript
sudo usermod -a -G groupname username

To illustrate, if you want to include the user “trioguide” in the sudo group, you can execute the subsequent command:

ShellScript
sudo usermod -a -G sudo trioguide

When adding a user to a new group, it is important to include the -a (append) option to avoid removing the user from any groups not explicitly listed after the -G option. If the user or group does not exist, the usermod command will display a warning message. Successful execution of the command does not yield any output.

How to Add an Existing User to Multiple Groups in One Command

To add a current user to multiple secondary groups simultaneously using a single command, utilize the “usermod” command and specify the “-G” option followed by the group names separated by commas:

ShellScript
sudo usermod -a -G group1,group2 username

How to Remove a User From a Group

If you want to exclude a user from a group, utilize the gpasswd command and specify the -d option.

In this instance, we are eliminating the username of a user from a particular group known as groupname:

ShellScript
sudo gpasswd -d username groupname

How to Create a Group

The groupadd command can be utilized to form a new group by specifying the desired group name:

ShellScript
sudo groupadd groupname

How to Delete a Group

To remove a group that already exists, you can use the groupdel command and specify the name of the group:

ShellScript
sudo groupdel groupname

How to Change a User’s Primary Group

To modify the primary group of a user, utilize the usermod command and then add the -g option:

ShellScript
sudo usermod -g groupname username

The user “trioguide” will have their primary group changed to “developers” in the given example:

ShellScript
sudo usermod -g developers trioguide

How to Create a New User and Assign Groups in One Command

Executing the useradd command will generate a fresh user account designated as “alex“. This user account will have the users group as its primary group and the wheel and developers groups as secondary groups.

ShellScript
sudo useradd -g users -G wheel,developers alex

Display User Groups

If you want to view comprehensive details about a user, including their group memberships, you can utilize the “id” command and provide the user’s username as the argument:

ShellScript
id username

Printing information about the currently logged-in user is possible by omitting the username in the command. To verify the user “trioguide“, we can execute the command:

ShellScript
id trioguide

Output:

uid=1000(trioguide) gid=100(users) groups=100(users),10(wheel),95(storage),98(power),990(libvirt),993(docker),999(kvm)

Based on the displayed information, it is apparent that the user’s primary group is “users” and it is affiliated with supplementary groups such as “wheel“, “storage“, “libvirt“, “docker“, and “kvm“.

To exhibit the additional groups of a user, utilize the “groups” command:

ShellScript
groups trioguide

Output:

wheel storage power users libvirt docker kvm

When the groups command is executed without specifying a username, it will display the groups associated with the user who is currently logged in.

Conclusion

This tutorial has demonstrated the process of adding a user to a group, which can be implemented on various Linux distributions such as Ubuntu, CentOS, RHEL, Debian, and Linux Mint by utilizing identical commands. If you have any inquiries, please don’t hesitate to leave a comment.

Alex

Alex

Hey there! My name is Alex and I'm a professional content writer. I'm also lucky enough to be part of an amazing trio team! I absolutely love what I do and I'm passionate about creating content that engages, informs, and entertains. Whether it's writing blog posts, website copy, or social media content, I always strive to deliver high-quality work that exceeds expectations.

We will be happy to hear your thoughts

Leave a reply

TrioGuide
Logo