In RHEL and other Linux systems, a primary group is the default group assigned to a user upon creation. Any file created by the user will, by default, be owned by this primary group. Each user must belong to exactly one primary group.
Example 1: Create a new user which also creates a primary group with the same name
# useradd newuser1
# passwd newuser1
# id newuser1
useradd newuser1 creates a new user named newuser1.
passwd newuser1 sets the password for newuser1.
id newuser1 displays the user and group information for newuser1.
Explanation The useradd command creates the user newuser1. In RHEL 8, if a primary group is not specified, a group with the same name as the user is created and assigned as the primary group. The id command confirms that the user's primary group (gid) is newuser1.
Example 2: Create a new user with a specific primary group
# groupadd developers
# useradd -g developers newuser2
# id newuser2
groupadd developers creates a new group named developers.
useradd -g developers newuser2 creates a new user newuser2 and assigns developers as the primary group.
id newuser2 verifies the user's group information.
Explanation First, the groupadd command creates the developers group. The useradd command is then used with the -g option to specify that developers should be the primary group for the newly created newuser2.
Example 3: Change an existing user's primary group
# usermod -g developers newuser1
# id newuser1
usermod -g developers newuser1 modifies the user newuser1, setting developers as the new primary group.
id newuser1 displays the updated user information.
Explanation The usermod command with the -g option allows you to change the primary group of an existing user. In this case, newuser1's primary group is changed to developers.
Example 4: Create a new user and a primary group with a specific GID
# groupadd -g 2025 projectalpha
# useradd -g 2025 newuser3
# id newuser3
groupadd -g 2025 projectalpha creates a group named projectalpha with a specific Group ID (GID) of 2025.
useradd -g 2025 newuser3 creates newuser3 with the primary group identified by GID 2025.
id newuser3 confirms the group assignment.
Explanation The -g option with groupadd allows specifying a unique GID for the new group. Subsequently, useradd uses this GID to assign the primary group to newuser3.
Example 5: View the primary group of the current user
# id -gn
id -gn displays the name of the current user's primary group.
Explanation The id command with the -g option shows the GID, and the -n option resolves the GID to its name. This combination provides a quick way to see the primary group name for the logged-in user.
Example 6: Command for changing primary group in Solaris (a different UNIX OS)
# usermod -g newprimarygroup solarisuser
This command is the same in both RHEL 8 and Solaris for changing a user's primary group.
Explanation Interestingly, the command to change a user's primary group, usermod -g, is consistent across both RHEL 8 and Solaris. This demonstrates a degree of standardization in basic user management commands across different UNIX-like systems.
Example 7: Create a system user with a specific primary group
# groupadd -r sys_tasks
# useradd -r -g sys_tasks taskrunner
# id taskrunner
groupadd -r sys_tasks creates a system group.
useradd -r -g sys_tasks taskrunner creates a system user taskrunner and assigns it to the sys_tasks primary group.
id taskrunner verifies the system user's information.
Explanation The -r option with groupadd and useradd creates a system group and a system user, respectively. System users are not intended for interactive logins and typically have UIDs in a lower range.
Example 8: Temporarily change the current primary group for the shell session
# newgrp developers
newgrp developers starts a new shell session where developers is the primary group.
Explanation The newgrp command allows a user to temporarily switch their primary group to another group of which they are a member. This change only applies to the current shell session.