Home Beginner's Guide Creating and adding a new user to a group in Linux

Creating and adding a new user to a group in Linux

Groups can be used to set privileges on users, privileges like running applications, reading, or writing in directories.

add user to group linux

Groups in Linux act as organization blocks that are used to sort and govern Linux user accounts. Groups can be used to set privileges on users, privileges like running applications, reading, or writing in directories.

There are two types of groups namely, Primary groups and Secondary groups. Primary groups are created when a Linux user creates a new file. Typically the group name is similar to the user who created the file. However, Secondary groups are used when you need to give a group of users specific permission on a file. Every Linux user must belong to only one primary group and none or more than one secondary group.

After following the guide, you shall be able to create a group and assign users to it. We shall also provide you some tips on Linux groups.

This tutorial was tested on Ubuntu, but the commands should work on all Linux distributions.

Adding Users to a Group in Linux

Linux Groups examples on Ubuntu

Example 1. To add a new group.

sudo groupadd fosslinuxgrp
Add New Group
Add New Group

Example 2. To add an existing user to an existing group.

sudo usermod -a -G fosslinuxgrp hendadel
Add Existing User To Existing Group
Add Existing User To Existing Group

Example 3. To change the primary group for an existing user.

sudo usermod -g fosslinuxgrp hendadel

Note that the “-g” option is to assign a primary group while the “-G” option from the previous example s to assign a secondary group.

Change Primary Group For Existing User
Change Primary Group For Existing User

Example 4. List all the assigned groups to the current logged in user.

groups
Display Groups of the Current User
Display Groups of the Current User

Example 5. To display all the assigned groups for a specific user.

groups hendadel
Display Groups of User
Display Groups of User

Example 6. To create a new user and assign an existing group to it.

sudo useradd -G fosslinuxgrp celine

This command creates a new user called “celine” and assigns it to an already existing group, which is “fosslinuxgrp.”

Create A New User and Add to Existing Group
Create A New User and Add to Existing Group

But do not forget to assign a password to the newly created user using the below command:

sudo passwd celine
Assign Password To New User
Assign Password To New User

Example 7. Add an existing user to multiple groups.

sudo usermod -a -G fosslinuxgrp,backup celine
Add Existing User to Multiple Groups
Add Existing User to Multiple Groups

Example 8. To display all the groups that are found on the Linux system, you can use the  command as follows:

getent group
Display All Groups in The System
Display All Groups in The System

That’s all about adding users to groups on Linux. I hope you enjoyed it.

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here