The useradd
command in Linux is used to create a new user account. One of its options, -G
or --groups
, allows adding supplementary groups to a user account. This option is particularly useful when you want to grant additional group permissions to a user beyond their primary group.
Here are several examples illustrating the usage of useradd
with the -G
option:
Example 1: Adding a user ‘john’ and assigning supplementary groups ‘admin’ and ‘sudo’.
$ sudo useradd -G admin,sudo john
This command creates a new user ‘john’ and adds him to the ‘admin’ and ‘sudo’ groups.
Example 2: Creating a user ‘alice’ and adding her to the ‘staff’ group.
$ sudo useradd -G staff alice
Here, the user ‘alice’ is created with membership in the ‘staff’ group.
Example 3: Adding a user ‘sam’ to multiple groups ‘developers’ and ‘docker’.
$ sudo useradd -G developers,docker sam
This command sets up the user ‘sam’ with both ‘developers’ and ‘docker’ group memberships.
Example 4: Creating a user ‘jane’ and adding her to ‘ftp’ group for file transfer permissions.
$ sudo useradd -G ftp jane
Here, ‘jane’ is added to the ‘ftp’ group, typically used for FTP file transfers.
Example 5: Adding a user ‘bob’ to ‘audio’ group for sound device access.
$ sudo useradd -G audio bob
This command grants ‘bob’ access to audio devices by adding him to the ‘audio’ group.
Example 6: Creating a user ‘tom’ and assigning him to ‘video’ group.
$ sudo useradd -G video tom
Here, ‘tom’ is included in the ‘video’ group, useful for video device permissions.
Example 7: Adding a user ‘max’ to ‘games’ group for gaming-related access.
$ sudo useradd -G games max
This command allows ‘max’ to access gaming-related resources through the ‘games’ group.
Example 8: Creating a user ‘sarah’ and assigning her to ‘docker’ and ‘wheel’ groups.
$ sudo useradd -G docker,wheel sarah
Here, ‘sarah’ gains membership in both ‘docker’ and ‘wheel’ groups, enabling Docker and administrative privileges.
Example 9: Adding a user ‘tim’ to ‘mail’ group for email server access.
$ sudo useradd -G mail tim
This command configures ‘tim’ with access to mail services through the ‘mail’ group.
Example 10: Creating a user ‘anna’ and assigning her to ‘finance’ group for financial data access.
$ sudo useradd -G finance anna
Here, ‘anna’ is given access to financial data by joining the ‘finance’ group.
Verification Steps: To verify whether the user has been successfully added to the specified groups, you can use the id
command followed by the username:
$ id john uid=1001(john) gid=1001(john) groups=1001(john),27(sudo),5(admin)
In this output, the supplementary groups ‘sudo’ and ‘admin’ are listed under the groups section for user ‘john’, confirming the addition.
Discussion about this post