groupadd Command Examples
While the useradd command creates new users, the groupadd command in Linux creates new groups. It updates the /etc/group file accordingly.
There are not many options with groupadd and its syntax is pretty basic:
groupadd [options] group_name
Let’s see how to use groupadd command for creating groups in Linux.
1. Create a new group
To create a new group in Linux, you can use it in the following manner:
sudo groupadd new_group_name
You can verify that the new group has been created by checking the /etc/group file:
abhishek@nuc:~$ sudo groupadd testing
abhishek@nuc:~$ grep testing /etc/group
testing:x:1008:
What do you do with a group? You should have some users in it, right? You can add users to group using the usermod command. If you want to add multiple users to a group at the same time, you can use the gpasswd command like this:
sudo gpasswd -M user1,user2,user3 new_group_name
2. Create a group with specific group ID (gid)
By default, the new group gets created with a group id higher than the GID_MIN value defined in /etc/login.defs. On most Linux system, this value is 1000.
In other words, you get one of the first available GID after 1000.
But you are not restricted to that. You can create a group with a specific GID in this manner:
sudo groupadd new_group_namep -g GID
Here’s an example:
abhishek@nuc:~$ sudo groupadd test_group -g 678
abhishek@nuc:~$ grep test_group /etc/group
test_group:x:678:
3. Create a new system group
When you create a new group, it’s a normal group with GID higher than 1000. You can also create a system group that automatically takes a group id between SYS_GID_MIN and SYS_GID_MAX as defined in /etc/login.defs.
sudo groupadd -r new_group_name
You can see in the example that the group id is less than 1000 and thus indicating that it is not a normal group but a system group (used for system programs).
abhishek@nuc:~$ sudo groupadd -r tesla
[sudo] password for abhishek:
abhishek@nuc:~$ grep tesla /etc/group
tesla:x:998:
I hope you find this quick little tutorial helpful in using groupadd command. You may also checkout how to delete groups with groupdel command.
Any questions or suggestions are always welcomed.