When using the useradd command to create a new user account, a default group with the same name as the user account is automatically created. However, the group name does not have to match the user account name, and a user can be a member of multiple groups to facilitate collaborative group work and resource sharing.
Similar to the important configuration files "/etc/passwd" and "/etc/shadow" for user accounts, the corresponding files for groups are "/etc/group" and "/etc/gshadow". These files are relatively simpler as they lack time-related fields.
# tail -n4 /etc/group aaa:x:500: ←The group "aaa" has a group ID of 500 hr:x:501:ccc,bbb ←The group "hr" has an ID of 501, and its members include "ccc" and "bbb" ccc:x:502: bbb:x:503: |
When using the useradd command with the "-g" option, you can specify the primary group for the user, and with the "-G" option, you can specify supplementary groups for the user. However, in order for a user to be added to a supplementary group, the group name must already be recorded in the "/etc/group" file (and its corresponding entry in "/etc/gshadow"), otherwise, the system will reject the addition.
To create a new group and record its name in "/etc/group," the "groupadd" command is used.
Let's experiment to illustrate this: # useradd ccc -G sub_grp ←Create a new user "ccc" and add them to the supplementary group "sub_grp" useradd: unknown group sub_grp ←The "sub_grp" group is unknown |
As you can see, we couldn't create the user "ccc" and add them to the "sub_grp" supplementary group simultaneously because the group "sub_grp" didn't exist in the "/etc/group" file.
To resolve this, we use the groupadd command to create the group "sub_grp":# groupadd sub_grp ←Create a new group "sub_grp" # tail -4 /etc/group ←Check if it's recorded in "/etc/group" haldaemon:x:68: aaa:x:500: bbb:x:501: sub_grp:x:502: ←New group is added here. :-) # useradd ccc -G sub_grp ←Now, we can create the user "ccc" and add them to the supplementary group "sub_grp" # tail -4 /etc/group ←Check "/etc/group" again vboxsf:x:491: aaa:x:500: bbb:x:501: sub_grp:x:502:ccc ←The "sub_grp" group now includes the user "ccc." ccc:x:503: |
[ccc@localhost ~]$ groups ←List the groups that the current user account belongs to ccc sub_grp ←The user "ccc" belongs to two groups, which are "ccc" and "sub_grp" |
[ccc@localhost ~]$ newgrp sub_grp ←Switch to the "sub_grp" group [ccc@localhost ~]$ groups ←Check if the effective group has changed to "sub_grp" sub_grp ccc ←"sub_grp" is now the first group in the list [ccc@localhost ~]$ echo 'hello' > test_grp.txt ←Create a file and see the group ownership [ccc@localhost ~]$ ls -l test_grp.txt -rw-r--r-- 1 ccc sub_grp 15 2016-09-06 14:26 test_grp.txt ←The file now belongs to "sub_grp" group [ccc@localhost ~]$ exit ←Exit the current subshell, and the group will revert to the original "ccc" group [ccc@localhost ~]$ groups ←Check if the group has returned to the original "ccc" group ccc sub_grp ←"ccc" is now the first group in the list again |
Syntax:gpasswd [-otpiton] [USER_NAME][MEMBER] | ||
Command name/Function/Command user | Options | Function |
gpasswd/ set group password and members/ Superuser & Group administrator |
-a | Add a user to a group (updates the "member" field in both "/etc/group" and "/etc/gshadow") |
-d | Remove a user from a group (updates the "member" field in both "/etc/group" and "/etc/gshadow") | |
-A | Specify a group administrator (Superuser-only option, adds the specified admin to the "Admin" field in "/etc/gshadow") | |
-r | Remove the group password (removes the password field in both "/etc/group" and "/etc/gshadow") | |
-R | Lock the group (prefix the password field in "/etc/gshadow" with "!") | |
--help | Displays the command's built-in help and usage information |
Now, let's explain the concept of the group password. When using the gpasswd command without options, it sets a group password. This password is only required when non-group members want to switch to that group using the newgrp command. For example, if we have a group named "sub_grp" with a member "ccc," switching to "sub_grp" using newgrp won't require a password. However, non-group members would need to enter the password to switch to "sub_grp."
Let's try an example:
(Sign in as Superuser, continuing from the previous example)
# gpasswd sub_grp ←Set a password for the "sub_grp" group Changing the password for group sub_grp New Password: ← Enter the desired password Re-enter new password: ←Confirm the password # su - aaa ← Switch to the "aaa" account (a non-member of "sub_grp" to test) $ newgrp sub_grp ← Join the "sub_grp" group using the "aaa" account Passwd: ←Enter the group's password to join "sub_grp" |
Additionally, if the group has no password set or if the group is locked using gpasswd -R, non-group members will never be able to use that group.
As a shortcut, the Superuser can designate a specific account as the group administrator for each group. The group administrator can then execute gpasswd to manage that particular group (the group administrator doesn't have to be a member of the group).
Here are some other examples: # gpasswd -a aaa sub_grp ← Add the "sub_grp" group to the "aaa" account # gpasswd -d aaa sub_grp ←Remove the "aaa" account from the "sub_grp" group # gpasswd -r sub_grp ←Remove the password for the "sub_grp" group # gpasswd -A aaa sub_grp ←Set the "aaa" account as the group administrator for "sub_grp." # gpasswd -R sub_grp ←Lock the "sub_grp" group (others can't join using newgrp, only reset password to unlock) |
Syntax:groupmod [-otpiton] [NEW_GROUP][OLD_GROUP] | ||
Command name/Function/Command user | Options | Function |
groupmod/ modify group/ Superuser |
-g | Modify the GID (Group ID). |
-o | Used in conjunction with -g to specify a non-unique GID | |
-n | Change the group name | |
--help | Displays the command's built-in help and usage information |
# groupmod -g 800 -o sub_grp ←Change the GID of the group "sub_grp" to 800 # groupmod -n hr_grp sub_grp ←Rename the group "sub_grp" to "hr_grp" (new name comes first) |
It's important to note that groupdel can only delete supplementary groups but not the primary group of any user. The primary group of a user cannot be deleted as it is associated with that user's ownership and permissions on files and directories.
When it comes to directories, one way to achieve resource sharing is by setting the "Set Group ID Bit" (SGID) on a directory. This means that regardless of who adds a file to that directory, the file's group ownership will automatically match the group ownership of the parent directory.
Another method involves having multiple user accounts set to the same group or adding them to an additional group. Then, the shared directory or file can have its group ownership set to a common group name shared among the users. This allows users within that group to access and collaborate on the shared directory or file, provided the permissions of the files and directories are set correctly.
To change the group ownership of a file or directory, the chown and chgrp commands can be used.
However, if the Superuser (root) changes the group name using chgrp, the new group name must be recorded in the system's group database, typically located in the "/etc/group" file, for the change to take effect and be recognized by the system.
Here's the detailed syntax for chgrp:Syntax:chgrp [-otpiton] [GROUP] FILE/DIRECTORY | ||
Command name/Function/Command user | Options | Function |
chgrp/ change group owne/ Any |
-c | Show only the modified parts |
-f | Do not display errors | |
-h | Change the group for symbolic links but not the target file | |
-R | Recursively change the group for all files and subdirectories | |
-v | Display the process of changing ownership | |
--help | Displays the command's built-in help and usage information |
# chgrp rd_grp file ←Change the group ownership of "file" to "rd_grp" # chgrp -R sub_grp ~/homework ←Change the group ownership of all files and subdirectories within the "homework" directory in the home directory to "sub_grp" |
Syntax:chown [-otpiton][USER][.][GROUP] FILE/DIRECTORY | ||
Command name/Function/Command user | Options | Function |
chown/ Change File Ownership/ Any |
-c | Show only the modified parts. |
-f | Do not display errors | |
-h | Change the owner for symbolic links but not the target file | |
-R | Recursively change the owner for all files and subdirectories | |
-v | Display the process of changing ownership | |
--help | Displays the command's built-in help and usage information | |