10 groupadd command examples in Linux [Cheat Sheet]


CheatSheet

Reviewer: Deepak Prasad

In Linux, groups help to set permission on the group level instead of each individual user. Users can be added to a group and have the same privileges of reading, writing, or executing permission for a particular file. Linux has two types of groups: primary group and secondary group. Each user must belong to one primary group. A user can be assigned to zero or multiple secondary groups.

groupadd command in Linux is used to create a new user group in the system. It uses the values specified on the command line plus the default values from the system.

 

Syntax for groupadd command

The syntax for the groupadd command is:

groupadd [options] group

Some important options in groupadd are:

  • -f, --force: This option causes the command to just exit with success status if the specified group already exists.
  • -g GID, --gid GID: This option allows you to specify the group ID for the new group.
  • -K KEY=VALUE: This option overrides /etc/login.defs defaults.
  • -o, --non-unique: This option allows the creation of a group with a non-unique GID.
  • -p, --password PASSWORD: This option sets the password for the group.
  • -r, --system: This option creates a system group.

 

1. Create a new group

The following command creates a new group student in the system.

$ sudo groupadd student

Sample Output:

The file /etc/group contains the group account information.

groupadd command to create a new group

 

2. Create a new group with a specific group ID

When no group ID is specified, groupadd assigns the group ID automatically which is greater than the id of other groups already present. You can create a new group with a specific group ID using -g or --gid option.

$ sudo groupadd staff -g GID

OR

$ sudo groupadd staff --gid GID

Sample Output:

groupadd command to create a new group with a specific group id

 

3. Override /etc/login.defs defaults for new Group

The -K or --key option overrides /etc/login.defs defaults (GID_MIN, GID_MAX and others). Multiple -K options can be specified.

The default GID_MIN and GID_MAX values are 1000 and 60000 respectively. You can set your own values using -K or --key option.

The following command creates a new group computer with group ID from 5000 to 7000.

$ sudo groupadd computer -K GID_MIN=5000 -K GID_MAX=7000

OR

$ sudo groupadd computer --key GID_MIN=5000 --key GID_MAX=7000

Sample Output:

groupadd command to change group id min and maxvalues

 

4. Create a new group with a non-unique GID

Generally, the group ID value must be unique and non-negative. But, the option -o or --non-unique allows you to create groups with duplicate (non-unique) GID.

$ sudo groupadd -o linux -g GID

OR

$ sudo groupadd --non-unique linux --gid GID

Sample Output:

groupadd command to create groups with non-unique gid

 

5. Create a system group

You can create a new system group using -r or --system group. The group IDs of new system groups are chosen in the SYS_GID_MIN (100) to SYS_GID_MAX (999) range, defined in /etc/login.defs.

$ sudo groupadd -r employee

OR

$ sudo groupadd --system employee

Sample Output:

groupadd command to create a system group

 

6. Forcefully create a group (even if it already exists)

The -f or --force option forces groupadd command to simply exit status even if the specified group already exists.

$ sudo groupadd -f employee

OR

$ sudo groupadd --force employee

Sample Output:

groupadd command to exit with success status if the specified group already exists

When -f or --force is used with -g, and the specified GID already exists, another (unique) GID will be assigned (i.e. -g is turned off).

 

7. Use an encrypted password for the new group

The -p or --password option allows you to specify an encrypted password for the new group. The default is to disable the password.

The following command creates a new group company with a password pa55word.

$ sudo groupadd company -p pa55word

OR

$ sudo groupadd company --password pa55word

Sample Output:

The file /etc/gshadow contains the secure group account information.

groupadd command to create a new group with a password

 

8. Add an existing user to a group

You can add a user to a group using the usermod command. The following command changes the primary group of a user bikash to student.

$ sudo usermod -g student bikash

Sample Output:

usermod command to change primary group of a user

 

9. Add an existing user to secondary groups

Each user can have only one primary group, but we can assign them to zero or multiple secondary groups. To add a user to secondary groups, you have to use the -G option.

sudo usermod -G computer bikash

$ id bikash
uid=1003(bikash) gid=1004(student) groups=1004(student),5000(computer)

 

10. Create a new user with a specific group

When creating a new user, useradd command provides the same group name as the username and the same group ID as the user ID. You can use -g option to assign a different group name to a new user. The group name must be already present in the system.

The following command creates a new user deepak with group employee.

sudo useradd -g employee deepak

$ id deepak
uid=1004(deepak) gid=997(employee) groups=997(employee)

 

Conclusion

groupadd command is useful for creating new groups in the Linux system. We hope this article helps you to understand how to use groupadd command to add new groups. We also showed how you can add a user to an existing group. If you still have any confusion, let us know in the comment section below.

 

What's Next

15 useradd command examples in Linux [Cheat Sheet]
15 usermod command examples in Linux [Cheat Sheet]
10 chgrp command examples in Linux [Cheat Sheet]

 

Further Reading

man page for groupadd command

 

Rohan Timalsina

Rohan Timalsina

He is proficient in a wide range of skills, including Page Builder Plugins such as Elementor, Beaver Builder, Visual Composer, and Divi Builder. His expertise extends to Front End Development with HTML5/CSS3, JavaScript, Bootstrap, and React.js. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment