Group Management Commands in Linux
Group is an essential part of Linux system management and security. Check out various commands that you can use for managing groups in Linux.
A group is basically a set of user accounts but treated as a single entity. If you set the file permission for the group to write, all users that are member of this group can modify the content of the file.
Group is an essential part of Linux system management and security. Imagine an enterprise environment where you have multiple users on the same Linux system. You can segregate developers from testers and sysadmins by putting them in different groups. With correct file permissions in place, testers won’t be able to run tools and files specific to developers and vice versa.
I highly recommend reading about file permissions and ownership in Linux to refresh the fundamentals.
Group management commands in Linux
The information about groups are stored in the /etc/group file. While you may modify the content of this file manually, there are various commands that let you manage groups in Linux. These commands eventually change the /etc/group file but it’s a lot better to use the commands then modify configuration files on your own.
Let’s see what commands you can use for group related tasks.
1. groups command: Display groups for a user
The groups command prints the groups of a user.
Without any arguments, it prints the groups you belong to.
abhishek@nuc:~$ groups abhishek adm cdrom sudo dip plugdev lpadmin sambashare docker
You can also specify the user name(s) to display groups of specific users.
abhishek@nuc:~$ groups abhishek prakas abhishek : abhishek adm cdrom sudo dip plugdev lpadmin sambashare docker prakas : prakas sudo
2. groupadd command: Add new groups
With the groupadd command, you can create new groups.
groupadd [options] group_name
You may specify your own choice of group ID (GID) with -g option. If you want to learn more, I have explained groupadd command with examples.
3. groupdel command: Delete existing groups
The groupdel command deletes an existing group in Linux.
There are no options here. You should keep in mind that if you delete a group, all the files owned by this group will still reflect the old group ID. It doesn’t change the group ownership.
4. groupmod command: Modify existing groups
With the groupmod command, you can modify group parameters like group name and group ID (GID).
groupmod [options] group_name
To change the groupname, you can use the -n option:
sudo groupmod -n new_groupname old_groupname
To change the GID, you can use the -g option:
sudo groupmod -g GID groupname
5. chgrp command: Change group ownership of a file
This one is slightly different as it doesn’t deal with groups but files. With chgrp command, you can change the group ownership of a file or directory.
chgrp [options] groupname file
With these commands, you are all set to manage groups in Linux.