The short answer
To create a new user group in Linux, you can use the groupadd command as follows:
$ sudo groupadd <group>
Run in Warp
Where:
- <group> is the name of the group you want to create.
For example, the following command will create a new group called developers:
$ sudo groupadd developers
Run in Warp
Note that the sudo command here is used to execute the groupadd command with superuser privileges, which is required in order to make system-wide changes.
You can learn more about sudo by reading our other articles on how to add a user to sudoers and how to spawn a root shell using sudo su.
Creating a group with a specific GID
In Linux, groups are automatically assigned a numeric identifier (GID) by the system upon creation based on a value defined in the /etc/login.defs file.
To create a group with a specific GID instead, you can use the groupadd command with the -g flag as follows:
$ sudo groupadd -g <gid> <group>
Run in Warp
Where:
- <group> is the name of the new group.
- <gid> is the specific GID you want to assign to the new group.
For example, this command will create a new group named testers with a group identifier of 1003:
$ sudo groupadd -g 1003 testers
Run in Warp
Note that when creating a new group, the minimum group ID that can be assigned is typically 1000, as it might otherwise conflict with system groups and potentially cause unexpected system behaviors and issues.
Easily retrieve this command using the Warp’s AI Command Suggestions
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:
Entering the create group with gid into the AI Command Suggestions will prompt a groupadd command that can then be quickly inserted into your shell by doing CMD+ENTER.
Creating a docker group
The docker group allows arbitrary users on the system to run and manage Docker containers without the need of superuser privileges (i.e., sudo) by gaining permission to interact with the Docker daemon.
To verify whether the docker group already exists, you can use the following grep command:
$ grep docker /etc/group
Run in Warp
If the aforementioned command doesn’t produce any output, you can then use the following groupadd command to create the docker group:
$ sudo groupadd docker
Run in Warp
You can then add a new user to the docker group use the following usermod command:
$ sudo usermod -aG docker <username>
Run in Warp
Where:
- <username> is the name of the user you want to add to the docker group.
Finally, to verify that the currently logged in user can execute Docker commands, you can run the following docker command:
$ docker ps
Run in Warp
Note that to ensure that the group membership takes effect, the user may need to log out and log back in again.
Creating a group using a Bash script
To automatically create a new group if it doesn't exist, you can use a Bash script as follows:
#!/bin/bash
# Define a new variable `group` that contains the name of the group
group="developers"
# Check if the group doesn't exist
if grep -q "$group" /etc/group; then
echo "$group already exists"
exit 1
else
# Attempt to create the group using `groupadd`
groupadd $group
# Check if the `groupadd` command succeeded
if [[ $? -eq 0 ]]; then
echo "$group created"
else
echo "Error creating $group"
exit 1
fi
fi
Run in Warp
You can then give the script execution permission using the chmod command as follows:
$ chmod +x create_group.sh
Run in Warp
Finally, you can execute the script with superuser privileges using the sudo command as follows:
$ sudo ./create_group.sh
Run in Warp
List existing groups
In Linux, the /etc/group file is a text-based database used for managing user accounts and group memberships.
To verify the existence of a group on the system, and therefore its successful creation, you can filter the content of this file using the grep command as follows:
$ grep <group> /etc/group
Run in Warp
Where:
- <group> is the name of the group you’re searching for.
For example, this command will output the entries relative to the developers group:
$ grep developers /etc/group
developers:x:1000:johndoe
Run in Warp
Where:
- developers is the group’s name.
- x is a placeholder for the group’s optional password.
- 1000 is the group’s identifier (GID).
- johndoe is the username of the group’s unique member.
You can learn more about fetching users and groups information by reading our other article on how to list users and groups in Linux.
Adding users to a group
To add a user to one or more secondary groups, you can use the usermod command with the -a flag (short for --append) and -G flag (short for --groups) as follows:
$ sudo usermod -a -G <groups> <username>
Run in Warp
Where:
- <groups> is a list of comma-separated group names or GIDs.
- <username> is the username of the user you want to add to the specified groups.
For example, this command will add the group named developers to the user named johndoe:
$ sudo usermod -a -G developers johndoe
Run in Warp
You can learn more about managing users by reading our other article on how to create and configure a new user in Linux.
Creating a shared folder for a specific group
A shared folder is a centralized location in the filesystem where multiple users part of the same group can store, access, and modify files simultaneously.
These folders are often created in locations that are easily accessible to multiple users or services on the system, such as the root folder (i.e., /).
To create a new shared folder, you can use the mkdir command as follows:
$ sudo mkdir <folder>
Run in Warp
Where:
- <folder> is the path to the shared folder.
Next, you can change the shared folder’s ownership by assigning it to a specific group using the chown command as follows:
$ sudo chown :<group> <folder>
Run in Warp
Where:
- <group> is the name of the group.
- <folder> is the path to the shared folder.
Finally, you can change the shared folder’s permissions to only allow the owner and the group to manage it using the chmod command as follows:
$ sudo chmod 770 <folder>
Run in Warp
For example, the following commands will create a new shared folder named projects in the root directory, assign it to the group named developers, and change its permissions so that only the owner (i.e., root) and the group (i.e., developers) can read, write, and execute in it:
$ sudo mkdir /projects
$ sudo chown :developers /projects
$ sudo chmod 770 /projects
Run in Warp
You can learn more about the chmod command by reading our other article on how to change permissions in Linux.
Written by
Durojaye Olusegun
Filed Under
Related Articles
List Open Ports in Linux
Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.
Count Files in Linux
Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.
How to Check the Size of Folders in Linux
Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.
Linux Chmod Command
Understand how to use chmod to change the permissions of files and directories. See examples with various chmod options.
POST JSON Data With Curl
How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.
Format Command Output In Linux
Learn how to filter and format the content of files and the output of commands in Linux using the awk command.
Switch Users In Linux
Learn how to switch between users, log in as another user, and execute commands as another user in Linux.
Remover Users in Linux
Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.
Delete Files In Linux
Learn how to selectively delete files in Linux based on patterns and properties using the rm command.
Find Files In Linux
Learn how to find and filter files in Linux by owner, size, date, type and content using the find command.
Copy Files In Linux
Learn how to safely and recursively copy one or more files locally and remotely in Linux using the cp and scp command.
Create Files In Linux
Learn how to create regular files in Linux using command line interface commands like touch, echo, cat, printf, and in-terminal text editors like Vim.