Linux Create Group

In this tutorial, we will explain how to add group in Linux systems. We will also show you how to remove a user from a group, add a user to group and how to delete, and list groups.

There are two types of groups in Linux operating systems:

1. Primary group – When a user creates a file, the file’s group is set to the user’s primary group. Usually, the name of the group is the same as the name of the user. The information about the user’s primary group is stored in the /etc/passwd file.

2. Secondary or supplementary group – Useful when you want to grant certain file permissions to a set of users who are members of the group. For example, if you add a specific user to the docker group, the user will inherit the access rights from the group, and be able to run docker commands.

Linux Create Group Syntax:

Who can create groups?
Only the root or a user with sudo privileges can create new groups.

groupadd [OPTIONS] GROUPNAME

Options are explained at the end this article.

Linux Group Examples:

1. How to create group with the name r2schoolsadmin.

sudo groupadd r2schoolsadmin

To get group information above created one using getent:

root@mongodb1:/home/r2schools/scripts# getent group r2schoolsadmin r2schoolsadmin:x:5569:

2. Create a group specific GID(group id) as 1021 and name as r2admin.

sudo groupadd -g 1021 r2admin

To get group information above created one using getent:

root@mongodb1:/home/r2schools/scripts# getent group r2admin r2admin:x:1021:

3. How to add an existing user to group in Linux:

In the below example r2admin is the group name and james is existing user.

sudo usermod -a -G r2admin james

4. How to get members of group.

getent group r2admin

Output:

r2admin:x:1021:james,david

Users james and david are members of the group r2admin.

5. How to add user to multiple groups in Linux:

sudo usermod -a -G r2admin,r2schoolsadmin venkat

6. How to remove user from the group:

We can remove a user from Linux group by using the command gpasswd.
Lets verify the list users in the group r2admin.

root@mongodb1:/home/r2schools/scripts# getent group r2admin r2admin:x:1021:james,david,venkat

Remove the user david from the group r2admin:

root@mongodb1:/home/r2schools/scripts# sudo gpasswd -d david r2admin Removing user david from group r2admin root@mongodb1:/home/r2schools/scripts# getent group r2admin r2admin:x:1021:james,venkat

We have successfully removed user from the group r2admin. But, not the user from Linux system.

7. How to delete group from Linux System:

We can delete a group from Linux System by using command groupdel

sudo groupdel r2schoolsadmin

Note: There should no space after comma(,) between the groups.

groupadd options are:

-f, –force

This option causes the command to simply exit with success status if the specified group already exists. When used with -g, and the specified GID already exists, another (unique) GID is chosen (i.e. -g is turned off).
-g, –gid GID
The numerical value of the group’s ID. This value must be unique, unless the -o option is used. The value must be non-negative. The default is to use the smallest ID value greater than 999 and greater than every other group. Values between 0 and 999 are typically reserved for system accounts.
-h, –help
Display help message and exit.
-K, –key KEY=VALUE
Overrides /etc/login.defs defaults (GID_MIN, GID_MAX and others). Multiple -K options can be specified.

Note: -K GID_MIN=10,GID_MAX=499 doesn’t work yet.

o, –non-unique
This option permits to add a group with a non-unique GID.
-p, –password PASSWORD
The encrypted password, as returned by crypt(3). The default is to disable the password.
Note: This option is not recommended because the password (or encrypted password) will be visible by users listing the processes.

You should make sure the password respects the system’s password policy.

-r, –system
Create a system group.