How to Add Groups in Linux: The groupadd Command

Learn how to manage your linux system with the groupadd command to add groups and useradd to add users.

How to Add Groups in Linux: The groupadd Command

This blog post will discuss how to use the groupadd command in Linux. This command adds a new group to the system. Understanding how to use this command to manage your Linux system effectively is important. By the end of the post, you will know how to create groups and add users to the groups you make.

Let's get started!

Sudo Privileges

The first step is to open the terminal. You can do this by pressing Ctrl+Alt+T on your keyboard.
You cannot add a group without sudo privileges. Therefore, you must use the Sudo command to gain access or log in as a root user. To do this, type:

sudo -

Enter your password when prompted.

Once you have access, you can manage your system.

groupadd command

We add groups using the groupadd command, followed by the name of the group you want to add.

The syntax for the command is:

groupadd [options] groupname

Example:

If we wanted to add a group called "geekbits", we would type:

groupadd geekbits 

OR: use sudo if not a root user.

sudo groupadd geekbits   

To confirm that the group was added successfully, run this command:

cat /etc/group

Output:

redis:x:119:
ssl-cert:x:120:postgres
postgres:x:121:
superman:x:1001:
`geekbits:x:1002:`

As you can see the group "geekbits" was added successfully.

Creating Users : Useradd command

To create a user, we use the useradd command followed by the user's name.

The syntax is as follows:

useradd [username]

Example

sudo useradd grandmaster

Let's confirm the user add was successful.

cat /etc/passwd

Output:

j4y:x:1000:1000:,,,:/home/j4y:/bin/bash
redis:x:112:119::/var/lib/redis:/usr/sbin/nologin
postgres:x:113:121:PostgreSQL administrator,,,:/var/lib/postgresql:/bin/bash
superman:x:1001:1001::/home/superman:/bin/sh
`grandmaster:x:1002:1003::/home/grandmaster:/bin/bash`

Now that the user "grandmaster" was added successfully, Let's see how to add users to groups.

Adding users to a group

New user:

To add a new user to a group, we use the useradd command with -G flag followed by the group's name and then the user's name.

The syntax is as follows:

useradd -G [group name] [username]

Example

sudo useradd -G geekbits jeff

Output:

ssl-cert:x:120:postgres
postgres:x:121:
superman:x:1001:
geekbits:x:1002:`jeff`

As you can see, the geekbits group has a new user "jeff."

Existing user:

We add existing users to a group using the usermod command.

The syntax is as follows:

usermod -G [group name] [username]

Example

sudo usermod -G geekbits grandmaster

We use the -G option to specify the group to which we want to add the user.

Output:

ssl-cert:x:120:postgres
postgres:x:121:
superman:x:1001:
`geekbits:x:1002:jeff,grandmaster`

And that's it! You have successfully added a new group to your Linux system (geekbits) and added users (jeff, grandmaster) to the group.

Remember, you can only add groups if you have sudo privileges or be logged in as root. If you need help managing your groups, feel free to contact us for assistance. We're always happy to help!

Thanks for reading:)

If you enjoy our content, please consider buying us a coffee to support our work:

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.