This article discusses the various group managment commands available in Linux which can be accessed from the command line.
A group is a set of accounts treated as a single entity. If you give permission for a group to take some action (such as modify a file), then all members of that group can take it. For example, you can give full permissions for the group friends to read, write, and execute the file /tmp/sample :
users smith friends
chgrp friends /tmp/sample
chmod 770 /tmp/sample
ls -l /tmp/sample
-rwxrwx--- 1 smith friends 2874 ... /tmp/sample
To add users to a group, edit /etc/group as root. To change the group ownership of a file, use the chgrp commands.
The groups command prints the Linux groups to which you belong, or to which other users belong:
whoami smith groups smith users groups jones root jones : jones users root : root bin daemon sys adm disk wheel src
groupadd [options] group
The groupadd command creates a group. In most cases, you should use the -f option to prevent duplicate groups from being created:
sudo groupadd -f friends
-g gidSpecify your own numeric group ID instead of letting groupadd
-fIf the specified group exists already, complain and exit.
The groupdel command deletes an existing group:
sudo groupdel friends
Before doing this, it’s a good idea to identify all files that have their group ID set to the given group, so you can deal with them later:
sudo find / -group friends -print
because groupdel does not change the group ownership of any files. It simply removes the group name from the system’s records. If you list such files, you’ll see a numeric group ID in place of a group name.
groupmod [options] group
The groupmod command modifies the given group, changing its name or group ID:
sudo groupmod -n newname friends
groupmod does not affect any files owned by this group: it simply changes the ID or name in the system’s records. Be careful when changing the ID, or these files will have group ownership by a nonexistent group.
-n nameChange the group’s name to name (safe).
-g gidChange the group’s ID to gid (risky).
This article has presented the most common Linux group management commands and options that can be accessed at the command line.