The useradd
command in Unix-like operating systems is used to create new user accounts. One of the options available with this command is -U
or --user-group
. This option instructs useradd
to create a new group with the same name as the user, and add the user to this group by default. This is particularly useful for ensuring that each user has their own dedicated group without the need for manual group creation.
Here are several examples demonstrating the usage of useradd
with the -U
option:
Example 1: Creating a user ‘john’ and automatically creating a group ‘john’.
$ sudo useradd -U john
Output: No output indicates success. To verify, check the existence of user ‘john’ and group ‘john’.
Example 2: Creating a user ‘alice’ and automatically creating a group ‘alice’.
$ sudo useradd -U alice
Output: No output on success. Verify by checking user ‘alice’ and group ‘alice’.
Example 3: Creating a user ‘mark’ with the --user-group
option.
$ sudo useradd --user-group mark
Output: No output if successful. Check user ‘mark’ and group ‘mark’ for verification.
Example 4: Specifying additional options while using -U
, such as setting a home directory.
$ sudo useradd -U -d /home/sam sam
Output: No output upon success. Verify the user ‘sam’ and group ‘sam’ existence.
Example 5: Creating a user ‘testuser’ with a specified UID and automatically creating group ‘testuser’.
$ sudo useradd -U -u 2001 testuser
Output: No output if successful. Verify the presence of ‘testuser’ and its corresponding group.
Example 6: Creating a user ‘demo’ with -U
and specifying a different shell.
$ sudo useradd -U -s /bin/bash demo
Output: No output if user ‘demo’ and group ‘demo’ are created successfully. Verify both ‘demo’ user and group.
Example 7: Creating a user ‘backup’ with -U
and specifying supplementary groups.
$ sudo useradd -U -G adm,users backup
Output: No output upon success. Verify user ‘backup’ and its supplementary groups ‘adm’ and ‘users’.
Example 8: Creating a user ‘newuser’ with -U
and specifying a comment.
$ sudo useradd -U -c "New User Account" newuser
Output: No output if the command executes successfully. Verify user ‘newuser’ and its corresponding group.
Example 9: Creating a user ‘dev’ with -U
and specifying an expiration date.
$ sudo useradd -U -e 2025-12-31 dev
Output: No output if successful. Verify the presence of ‘dev’ user and ‘dev’ group before the expiration date.
Example 10: Creating a user ‘webadmin’ with -U
and setting a password expiry date.
$ sudo useradd -U -f 30 webadmin
Output: No output upon success. Verify user ‘webadmin’ and ‘webadmin’ group for the changes.
Discussion about this post