The groupadd
command in Linux is used to create a new group in the system. It allows system administrators to define user groups that can be assigned to files or directories for easier management of access control.
sudo groupadd [options] groupname
Requires sudo or root privileges. Replace [options]
with the appropriate flags to modify group attributes.
What it does: Creates a new group with the specified group name.
Syntax:
groupadd groupname
Example:
groupadd developers
Creates a new group called developers
.
What it does: Specifies a custom Group ID (GID) for the new group. By default, the system will automatically assign the next available GID.
Syntax:
groupadd -g GID groupname
Example:
groupadd -g 1001 developers
Creates a new group called developers
with GID 1001
.
What it does: If the group already exists, this option prevents an error and forces the command to exit successfully.
Syntax:
groupadd -f groupname
Example:
groupadd -f developers
If the developers
group already exists, the command will succeed without an error.
What it does: Creates a system group. System groups have GID values lower than 1000 (or the value set in /etc/login.defs
).
Syntax:
groupadd -r groupname
Example:
groupadd -r sysadmins
Creates a system group called sysadmins
.
What it does: Allows creating a group with a duplicate GID. This can be useful if you want multiple groups to have the same GID.
Syntax:
groupadd -o -g GID groupname
Example:
groupadd -o -g 1001 developers
Creates the developers
group with GID 1001
, even if that GID is already assigned to another group.
What it does: Sets the encrypted password for the new group. It allows the creation of a password for a group, but it's rarely used as groups typically don't have passwords.
Syntax:
groupadd -p 'encrypted_password' groupname
Example:
groupadd -p '$1$dr5MGUev$uZ8QHz4ilePmWugMkj7yA.' developers
Sets an encrypted password for the group developers
.
The groupdel
command in Linux is used to delete an existing group from the system. It removes the group from the /etc/group
and /etc/gshadow
files, effectively removing the group from the system.
sudo groupdel groupname
Requires sudo or root privileges. Replace [groupname]
with the group to be deleted.
What it does: Deletes the specified group from the system.
Syntax:
groupdel groupname
Example:
groupdel developers
Deletes the developers
group from the system.
Command | Description | Example |
---|---|---|
groupadd |
Create a new group | groupadd developers |
groupadd -g GID |
Create a group with a specific GID | groupadd -g 1001 developers |
groupadd -f |
Force command execution if group exists | groupadd -f developers |
groupadd -r |
Create a system group | groupadd -r sysadmins |
groupadd -o |
Allow non-unique GID | groupadd -o -g 1001 developers |
groupadd -p |
Set encrypted password | groupadd -p '$1$dr5MGUev$uZ8QHz4ilePmWugMkj7yA.' developers |
groupdel |
Delete a group | groupdel developers |