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.

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.

Hend Adel
Hi! I'm Hend Adel, a freelancer technical geek with successful experience in Database, Linux and many other IT fields. I help to build solutions to suit business needs and creating streamlined processes. I love Linux and I'm here to share my skills via FOSS Linux! Thanks for reading my article.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,872FansLike
267FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

How to create Manjaro Linux Live USB drive in Windows PC

Create Manjaro Live USB flash drive
Manjaro Linux is based on the independently developed Arch operating system.  Arch Linux is already known to be an exceptionally fast, powerful, and lightweight distribution. But Arch Linux is typically targeted to geeks who have some Linux background and command-line usage.
clipboard manager linux
We have all had that moment when we copied a text, but we first needed another one, and in the process, lost the first one. If you're lucky, you can get it back quickly with a bit of work.
Best video editors for linux
This article is for all the Linux users out there who are looking for video editors to create their own professional videos, which can range from short documentaries to even movies.