When adding new user accounts in Linux using the useradd
command, administrators can specify various options to customize user attributes. One crucial option is -g
(or --gid
), which allows assigning a primary group to the new user. This group determines the default group ownership of files created by the user.
Here are several examples demonstrating how to use useradd
with the -g
option:
Example 1: Assigning the group “staff” to a new user “john”:
useradd -g staff john
This command creates a new user account named “john” with the primary group set to “staff”. To verify, use:
id john
Output should include “staff” as the primary group.
Example 2: Assigning a numerical group ID (GID) directly:
useradd -g 1002 sarah
This command assigns the group with GID 1002 to the new user “sarah”. Verify using:
id sarah
Output should show the group associated with GID 1002.
Example 3: Specifying an existing group by name and creating a new user:
useradd -g developers alice
Here, “alice” is added with the primary group “developers”. Verify with:
id alice
Example 4: Creating a user without specifying a primary group (defaults to user’s name as group):
useradd charlie
Check the primary group assignment for “charlie” using:
id charlie
Example 5: Using the --gid
long option:
useradd --gid managers dave
This assigns the group “managers” to the user “dave”. Verify the group association with:
id dave
Each of these commands utilizes the useradd
utility with the -g
option to manage the primary group assignment for new user accounts in Linux. Verifying the results involves checking the output of id username
where “username” is the newly created user to confirm the group association.
Discussion about this post