Create Groups In Linux
The short answer
To create a new user group in Linux, you can use the [.inline-code]groupadd[.inline-code] command as follows:
Where:
- [.inline-code]<group>[.inline-code] is the name of the group you want to create.
For example, the following command will create a new group called [.inline-code]developers[.inline-code]:
Note that the [.inline-code]sudo[.inline-code] command here is used to execute the [.inline-code]groupadd[.inline-code] command with superuser privileges, which is required in order to make system-wide changes.
You can learn more about [.inline-code]sudo[.inline-code] by reading our other articles on how to add a user to sudoers and how to spawn a root shell using sudo su.
[#create-group-with-gid] Creating a group with a specific GID [#create-group-with-gid]
In Linux, groups are automatically assigned a numeric identifier (GID) by the system upon creation based on a value defined in the [.inline-code]/etc/login.defs[.inline-code] file.
To create a group with a specific GID instead, you can use the [.inline-code]groupadd[.inline-code] command with the [.inline-code]-g[.inline-code] flag as follows:
Where:
- [.inline-code]<group>[.inline-code] is the name of the new group.
- [.inline-code]<gid>[.inline-code] is the specific GID you want to assign to the new group.
For example, this command will create a new group named [.inline-code]testers[.inline-code] with a group identifier of [.inline-code]1003[.inline-code]:
Note that when creating a new group, the minimum group ID that can be assigned is typically [.inline-code]1000[.inline-code], as it might otherwise conflict with system groups and potentially cause unexpected system behaviors and issues.
[#easily-recallsyntax-with-ai] Easily retrieve this command using the Warp’s AI Command Suggestions [#easily-recallsyntax-with-ai]
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:
Entering the [.inline-code]create group with gid[.inline-code] into the AI Command Suggestions will prompt a [.inline-code]groupadd[.inline-code] command that can then be quickly inserted into your shell by doing [.inline-code]CMD+ENTER[.inline-code].
[#create-a-docker-group] Creating a docker group[#create-a-docker-group]
The [.inline-code]docker[.inline-code] group allows arbitrary users on the system to run and manage Docker containers without the need of superuser privileges (i.e., [.inline-code]sudo[.inline-code]) by gaining permission to interact with the Docker daemon.
To verify whether the [.inline-code]docker[.inline-code] group already exists, you can use the following [.inline-code]grep[.inline-code] command:
If the aforementioned command doesn’t produce any output, you can then use the following [.inline-code]groupadd[.inline-code] command to create the [.inline-code]docker[.inline-code] group:
You can then add a new user to the [.inline-code]docker[.inline-code] group use the following [.inline-code]usermod[.inline-code] command:
Where:
- [.inline-code]<username>[.inline-code] is the name of the user you want to add to the [.inline-code]docker[.inline-code] group.
Finally, to verify that the currently logged in user can execute Docker commands, you can run the following [.inline-code]docker[.inline-code] command:
Note that to ensure that the group membership takes effect, the user may need to log out and log back in again.
[#create-a-group-with-a-script] Creating a group using a Bash script [#create-a-group-with-a-script]
To automatically create a new group if it doesn't exist, you can use a Bash script as follows:
You can then give the script execution permission using the [.inline-code]chmod[.inline-code] command as follows:
Finally, you can execute the script with superuser privileges using the [.inline-code]sudo[.inline-code] command as follows:
[#list-existing-groups] List existing groups [#list-existing-groups]
In Linux, the [.inline-code]/etc/group[.inline-code] 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 [.inline-code]grep[.inline-code] command as follows:
Where:
- [.inline-code]<group>[.inline-code] is the name of the group you’re searching for.
For example, this command will output the entries relative to the [.inline-code]developers[.inline-code] group:
Where:
- [.inline-code]developers[.inline-code] is the group’s name.
- [.inline-code]x[.inline-code] is a placeholder for the group’s optional password.
- [.inline-code]1000[.inline-code] is the group’s identifier (GID).
- [.inline-code]johndoe[.inline-code] 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.
[#add-users-to-a-group] Adding users to a group [#add-users-to-a-group]
To add a user to one or more secondary groups, you can use the [.inline-code]usermod[.inline-code] command with the [.inline-code]-a[.inline-code] flag (short for [.inline-code]--append[.inline-code]) and [.inline-code]-G[.inline-code] flag (short for [.inline-code]--groups[.inline-code]) as follows:
Where:
- [.inline-code]<groups>[.inline-code] is a list of comma-separated group names or GIDs.
- [.inline-code]<username>[.inline-code] is the username of the user you want to add to the specified groups.
For example, this command will add the group named [.inline-code]developers[.inline-code] to the user named [.inline-code]johndoe[.inline-code]:
You can learn more about managing users by reading our other article on how to create and configure a new user in Linux.
[#create-a-shared-group-folder] Creating a shared folder for a specific group [#create-a-shared-group-folder]
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., [.inline-code]/[.inline-code]).
To create a new shared folder, you can use the [.inline-code]mkdir[.inline-code] command as follows:
Where:
- [.inline-code]<folder>[.inline-code] 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 [.inline-code]chown[.inline-code] command as follows:
Where:
- [.inline-code]<group>[.inline-code] is the name of the group.
- [.inline-code]<folder>[.inline-code] 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 [.inline-code]chmod[.inline-code] command as follows:
For example, the following commands will create a new shared folder named [.inline-code]projects[.inline-code] in the root directory, assign it to the group named [.inline-code]developers[.inline-code], and change its permissions so that only the owner (i.e., [.inline-code]root[.inline-code]) and the group (i.e., [.inline-code]developers[.inline-code]) can read, write, and execute in it:
You can learn more about the [.inline-code]chmod[.inline-code] command by reading our other article on how to change permissions in Linux.