Remember that each user has a primary (login) group and may belong to several secondary (supplementary) groups. The primary group is set at login and influences file creation permissions, as files are automatically associated with both the user account and the primary group.
Before proceeding with these exercises, ensure that a user named John exists on your system.
Creating a New Group and Adding a User
To start, ensure that the user John is created and then create the Developers group:gpasswd command. Despite its name originating from “group password,” it is primarily used to manage group memberships. To add John to the Developers group, run:
Changing a User’s Primary Login Group
Sometimes you may need to change John’s primary login group. Use theusermod command with caution, ensuring that you do not confuse the option for modifying secondary groups. The -g (or --gid) option specifically changes the primary group.
Be sure that you correctly distinguish between the primary group and secondary groups. An incorrect adjustment may lead to unintended permission issues.
gpasswd command expects the username first and then the group name, whereas usermod requires the group name before the username.
Renaming and Deleting a Group
To rename the “developers” group to “programmers,” use thegroupmod command. You can choose between the long option or its short alternative:
groupdel command. However, if any user, such as John, is still using that group as their primary group, you will encounter an error:
Before deleting a group, make sure that no user has it set as their primary group. In cases where the group is primary for any user, change that user’s primary group (for example, back to “john”) before deletion.
This concludes our guide on managing local groups and group memberships in Linux. By leveraging these commands, administrators can simplify the management of file permissions and user roles across the system. For more detailed information on Linux user and group management, consider reviewing the Linux Documentation or related user management tutorials.