How To Create Groups in Linux: A Step-by-Step Guide

Codes - Create Groups in Linux

Codes - Create Groups in Linux

Linux makes it easy to give users varied access levels and assign them specific responsibilities. All you have to do is create groups specifying the permissions you want to give the users and then put the users in the group. 

But what exactly is a group in Linux? Are groups of different types? And how do you create them? 

These are the questions we answer in this post to help you get started with using groups on Linux. 

What are Groups in Linux?

A group in Linux is simply a collection of users having the same access privileges. Information about groups is stored in /etc/group, which is the default group folder. There are two kinds of groups in Linux: system groups and regular groups.

When a new user is created in a Linux system, the user is put in a group by default. This group is called the system group. So, these groups are created in tandem with the user’s creation. Every file this new user creates becomes a part of the user’s system group.

On the other hand, a regular or normal group is a group that a user creates. Users can be a part of several regular groups. These groups are made so the users in them can manage access to apps and files in the group. All the members in a regular group have the same access privileges.

How to Create a Group in Linux

Not every user can create groups. Before a user can create a group, they must obtain superuser privileges. So, log into your terminal as a root user, and then you can run the following command:

groupadd groupname


In the above command, the “groupadd” command creates a group called “groupname.” If you try to give a group the same name as another, you will see an error message and will need to give your group another name.

But if group creation is successful, the command will add the newly created group’s data to the /etc/group directory and also to /etc/gshadow. What’s more, Linux automatically assigns a GID to every group when it is created. 

The GID is a number that helps identify a group, and you can also assign a GID to a group (so long as another group doesn’t have the same GID). You can specify the GID when creating a group with the -g flag, like so:

groupadd -g 2385 groupname


How to Add a User to a Linux Group

You can start adding users to a group right after it is created. Doing this is as simple as using the usermod command with the -a and the -G flags. Note that just like the groupadd command, you cannot use usermod without root access.

Consider that you want to add a user called username1 to your groupname group. Here’s how you would use the following command:

sudo usermod -a -G groupname username1


If the command runs without hitches, you will not see any output on the terminal. You will only see an output if there is an error. Bear in mind that using the -a flag, which is the append flag, is crucial. If you forget to use this flag, Linux will remove the user from all the groups except the group you’re adding them to. 

Interestingly, you can add a user to several groups in one shot. All it takes is writing the names of all the groups the user should be added to before typing in the user’s name. 

So, if you wanted to add username1 to the groups “anewgroup” and “secondnewgroup,” you could run:

sudo usermod -a -G anewgroup secondnewgroup username1


Options You Can Use with the groupadd Command

The groupadd command supports several flags. For instance, the -K flag overrides the default /etc/login/.defs file when creating a new group. On the other hand, you can use the -o flag to create a new group but give it the same GID as an existing group. 

The groupadd command also facilitates the creation of password-protected groups. All it takes is the use of the -p option. 

How to Remove a User from a Linux Group

The gpasswd command is the one you should use when it’s time to remove a user from a group. You will need to use the gpasswd command in conjunction with the -d flag. 

Of course, you will need to mention the user’s name and the group’s name in the command. Here’s what this looks like:

sudo gpasswd -d username1 anewgroup


How to Delete a Linux Group

If a group has served its purpose, you might want to delete it from your system. To do this, you can use the groupdel command with the group’s name. Here’s what this looks like: 

sudo groupdel anewgroup


With these easy-to-use commands, you can manage users and groups effectively on Linux.