Linux Commands

How to Create Groups in Linux (groupadd Command)?

How to Create Groups in Linux (groupadd Command) copy

As Ubuntu is a MultiUser Operating System, it also allows the creation of multiple user Groups. System Administrators while working with multiple users, have to restrict resource or file sharing to specific users or specific types of users. Thus, specific types of users are added or assigned to one group which eases the system administrator to keep track of users.

In this article, the methods by which the “groupadd” command can be used are discussed.

How to Create Groups in Linux (groupadd Command)?

The “groupadd” is a useful command in Ubuntu to create and manage groups. It adds the newly created group to the “/etc/group” path. The basic syntax to use the “groupadd” command is:

groupadd <<strong>option</strong>> <<strong>groupName</strong>>

The “options” statement is used to make the “groupadd” command flexible in many different ways. Some of the popular “options” with the “groupadd” command are:

  • -f”: Forcefully Assigns or Overwrites an Existing Group.
  • -g”: Assigns a Custom Group ID to a New Group.
  • -K”: Overrides the Default Values assigned to a New Group.
  • -p”: Sets Password for a New Group.

Below are the different methods in which the “groupadd” command can be used:

  • Use “groupadd” to Create a New Group with the Default Configuration.
  • Use “groupadd” to Overwrite or Replace an Existing Group.
  • Use “groupadd” to Assign a Custom Group ID to a New Group.
  • Use “groupadd” to Assign a Password to a New Group.
  • Use “groupadd” to Create a New Group with a Custom Configuration.

All the mentioned methods are explained in detail.

Creating a New Group Using the “groupadd” Command

A new user group in Ubuntu with default configuration can be created simply by using the name of the group with the “groupadd” command. In our case, we have created a new user group “groupX” with the “groupadd” Command that will have the default group values:

sudo groupadd groupX

The command line moves to the next line indicating the new group “groupX” is created:

To view if the newly created group was added to the system or not, use the “cat” command to list all the groups in Ubuntu:

cat /etc/group/

This will list all the groups in the Terminal:

Scroll down to get to the last groups. As we can see, the group “groupX” is included in the groups list with the Group ID “1024”:

Or you can directly list the last set of groups with the “tail” command:

sudo tail /etc/group

The last 10 groups created or added will be displayed including the “groupX” as well:

Overwriting Already Added Group Using groupadd

The already added group can be overwritten as well using the “groupadd” command with the “-f” option. The “-f” option overwrites the already existing groups:

groupadd -f <groupName>

In our case, we already have a “genz” group as seen by using the “tail” command to list the last set of groups:

sudo tail /etc/group

The “genz” group already exists with the Group ID of “1005”:

Now, If we try to make another group of the same name “genz”, the command will be:

sudo groupadd genz

As the group “genz” already existed and we tried to make another group with the same name, the Terminal warns about the group already existing:

To overwrite the existing group, in our case “genz”, the command is:

sudo groupadd -f genz

Instead of displaying the “group already exists” error, the command line moves to the next line indicating the group was overwritten:

To verify, the command “sudo tail /etc/group” can be used again to list the groups. In our case, the group “genz” exists means it was overwritten:

Assigning Group ID to the New Group Using groupadd

While creating a new group in Ubuntu, Ubuntu assigns the Group ID to the newly created group by default. The custom group IDs can also be assigned with the “groupadd” command. The “-g” option with the “groupadd” command ensures the custom Group ID is to be assigned to the new group having the command:

groupadd -g <groupID> <groupName>

In our case, we have assigned the Group ID “1030” to the new group “linuxWays” with the command:

sudo groupadd -g 1030 linuxWays

The command line moves to the next line indicating the new group “linuxWays” was created:

To verify the newly created group, run the “tail” command to list the last set of groups:

sudo tail /etc/group

The group “linuxWays” can be seen in the list with the custom Group ID of “1030”:

Assigning Password to the New Group Using groupadd

While creating a new Group in Ubuntu, Ubuntu does not ask for a password. Adding the password can be useful for enhancing the overall group security. With the “-p” option along with the “groupadd” command, the password can be assigned to a new group:

groupadd -p <password> <groupName>

In our case, to create a new Group “linuxWays1” with a custom Password, the “groupadd” command will be:

sudo groupadd -p pass#1234 linuxWays1

The command line moves to the next line indicating the new group was created with a custom password:

To verify the creation of the new group, the “tail” command can be used to list the set of last groups added in Ubuntu:

sudo tail /etc/group

As seen in the Terminal, the “linuxWays1” group is created and added to Ubuntu:

Creating a New Group With Custom Values Using groupadd

Ubuntu, while adding and creating a new group, assigns the default values to the Group. The “-K” option along with the “groupadd” command ensures the group will be added with the given custom values:

groupadd -K <customValue> <groupName>

In our case, to add the group “genX” with the custom values of “GID_MIN” and “GID_MAX”, the “groupadd” command is:

sudo groupadd -K GID_MIN=1050 -K GID_MAX=1500 genX

The “GID_MIN=1050” and “GID_MAX=1500” mean the Group ID assigned should be between “1050” and “1500”. The cursor moves to the next line once the group is added:

To verify the addition of the group “genX”, the “tail” command is used:

sudo tail /etc/group

Instead of Ubuntu giving the group “genX” a Group ID of “1032”, a custom Group ID is assigned:

Adding New User to a Group Using groupadd

The “useradd” command is used to create a new user in Ubuntu. By default, the new user is assigned to a group of its own, i.e. the newly created user has its own group. Now to assign the new user to a specific group, the “-g” flag with the “useradd” command is used:

useradd -g <groupName> <userName>

In our case, to assign the new user “micheal” to the group “genX”, the “useradd” command will be:

sudo useradd -g genX micheal

The Terminal cursor moves to the next line which means the new user was added to the group “genX”:

To verify if the new user “micheal” was added to the group “genX”, the command used will be:

sudo /etc/passwd

This will list all the users of Ubuntu with their details. As the new user “micheal” was added to the group “genX”, we can see that the user “micheal” has the group ID of 1050 which is the Group ID of “genX”:

Adding Existing User to a Group Using groupadd

An existing user can also be added to a group. To add an existing user to a specific group, the “usermod” command is used with the syntax:

usermod -g <groupName> <existingUserName>

In our case, the user “aswan” is added by default to group 1022 which is “aswan”, and can be verified using the “tail” command:

sudo tail /etc/passwd

The user “aswan” as seen below, has a group ID of 1022:

Now to assign or add “aswan” to another group, in our case adding to the group “linuxWays”, the “usermod” command is:

sudo usermod -g linuxWays aswan

The “-g” option ensures adding the user to a group:

To verify if the user “aswan” was added to the group “linuxWays”, the “tail” command is used:

sudo tail /etc/passwd

As we can see, the user “aswan” had the Group ID “1022” before, and now the GID changed to “1030” indicating the group of user “aswan” has changed:

That’s all about creating groups in Linux using the groupadd command.

Conclusion

The “groupadd” command is useful for creating user groups with default configuration, custom configuration, custom GID, and can also be used to set passwords to a group in Ubuntu. This article discussed all the methods in which the “groupadd” command can be used.

Similar Posts