The groupadd command in Linux – A complete guide

In this article, we’ll talk about the functionality of the groupadd command, which is limited to Linux operating systems and used for creating Linux groups. Linux also supplies groupmod and groupdel in addition to groupadd command to manage the Linux groups.

What is a Linux group?

A Linux group has a set of permissions which may include read, write, or execute permissions. For example, users working on the same project can be put together in the same group to share files and other system resources. By default, whenever a new user is added by, adduser command, the user is added to the own personal group of that user. Each file in Linux has a single user as file owner (generally the one who created the file) and a group having access to the file.

suryansh@journaldev:~$ ls -l
total 12335
drwxr-xr-x   360 root  wheel       361 May 18 03:58 science
drwxr-xr-x  1350 root  wheel      1351 May 18 03:58 security
drwxr-xr-x    62 root  whebn l        63 May 18 03:58 shells
drwxr-xr-x  1599 root  wheel      1600 May 18 03:58 sysutils
drwxr-xr-x  1966 root  wheel      1967 May 18 03:58 textproc

We can determine the file owner and group using the ls -l command. From the output above, we can determine the file owner is root and the group is wheel.

Syntax for the groupadd command

The syntax for the groupadd command:

sudo groupadd [options] group

The /etc/group file contains the name and members of each group. Here is sample output from a Ubuntu machine.

suryansh@journaldev:~$ cat /etc/group
root:x:0:
adm:x:4:syslog,suryansh
...
tty:x:5:syslog
suryansh:x:1000:

How to create a Linux group?

To create a Linux group, execute groupadd command with sudo:

sudo groupadd groupName

If the new group is created successfully, there will be no output.

Group Added Successfully
Group added successfully

or else it will show you an error.

Error Creating A Linux Group
Error creating a Linux group

How to create a Linux group with a password?

One can set a group password to temporarily enter a group using the following command:

sudo group add groupName -p groupPassword

However, it is not recommended as the password is visible to users listing the processes.

How to create a Linux group with a specific Group ID?

To create a Linux group with a specific Group ID, execute the following groupadd command and replace groupName with the name of your choice, and groupID must be some non-negative unique value (unless -o option is used).

sudo groupadd -g groupID groupName

If the new group created successfully, there will be no output.

Creating Linux Group With A Group ID
Creating a Linux group with a Group ID

or else it will output an error.

Error While Creating A Linux Group With A Group ID
Error while creating a Linux group with a Group ID

What is the force option -f in the groupadd command?

The force option -f, –force is used to force the groupadd command to end with success status even if the group already exists.

Forces Groupadd Command To End With Success Status
Forces groupadd command to end with success status

If we specify a Group ID using the -g option with force option and if the Group ID already exists, then a unique Group ID used.

Force Option With Specifying Group ID
Force option with specifying Group ID

Then we can see in the /etc/group that since the 1001 Group ID already existed, then it will choose another unique Group ID that is the -g option is off.

suryansh@journaldev:~$ cat /etc/group
...
suryansh:x:1000:
groupName:x:1001:
NewGroup:x:1002:

What is the key option -K in the groupadd command?

The key option -K, –key is used to override the defaults set in /etc/login.defs for GID_MIN,GID_MAX, and others.

sudo groupadd -K GID_MIN=100  -K GID_MAX=499 groupName

In the above command, it will choose the Group ID between that of GID_MAX and GID_MIN specified with command.

How to create a system Linux group?

To create a system group -r, –system options tells to use SYS_GID_MIN and SYS_GID_MAX values defined in /etc/login.defs instead of GID_MIN and GID_MAX.

sudo group add -r systemGroup

How to add a Linux group with a non-unique Group ID?

To add a Linux group with a non-unique GID -o, –non-unique option used.

sudo groupadd -g 1001 -o newGroup
Adding A Group With Non Unique Group ID
Adding a group with non-unique Group ID

We can see the changes in the /etc/group output.

suryansh@journaldev:~$ cat /etc/group
...
suryansh:x:1000:
groupName:x:1001:
newGroup:x:1001:

How to get help/manual of groupadd command in terminal?

You can easily get help or manual of groupadd command directly in the shell/terminal by executing the man command to open groupadd’s man page (manual page).

man groupadd

Use arrow keys to navigate up and down and once you are done press Q key on your keyboard to exit manual page.

Conclusion

The groupadd command is available across all Linux distributions and is used to create Linux groups. It is simple and easy-to-use command, frequently used across all Linux distributions.

Thank you for reading! 🙂