This is the 14th day of my participation in the Gwen Challenge.More article challenges
This tutorial step-by-step shows you how to add users to user groups on Linux and how to add users and groups on Linux through several examples using the Linux command line. These commands should work on any Linux distribution and have been tested on CentOS, Debian, and Ubuntu.
Add a new user to the user group
A Linux user can have a Primary group and one or more Secondary groups. These groups can be used as arguments to the adduser command when a user is created.
All commands must be executed as the root user. On Ubuntu, prefix all commands with sudo, or run sudo -s to switch to root.
Adding a User Group
As a first step, I’ll add two new user groups, family and Friends:
groupadd family
groupadd friends
Copy the code
Add a user to a user group
Next I add a new user, Tom, and add the user to the user group family. The family user group is added as a subsidiary group by using the -g parameter.
useradd -G family tom
Copy the code
Add a new user to multiple user groups
Tom is now a user of the family user group. The -g argument allows you to specify multiple user groups, separated by commas. To add user Tom to the family and Friends user groups, use the following command:
useradd -G family,friends tom
Copy the code
Setting a User Password
Please note that the new Linux user Tom does not yet have a password, so he cannot log in. To set the password for this user, run the following command:
passwd tom
Copy the code
Enter the new password twice when the command is requested.
In the example above, we added the user Tom to the secondary group, and the adduser command automatically created a new primary group and assigned it to the primary group.
- The user name:
tom
- The main group:
tom
- The accessory group:
family
(Or use the second case to addfamily
和friends
Two subsidiary groups)
Set a new primary group
If you want to add Tom users and set the primary group to family (instead of the default Tom group) and the secondary group to Friends, you can use this command:
useradd -g family -G friends tom
Copy the code
Use the man command to get a detailed description of all the command-line options of the useradd command:
man useradd
Copy the code
Add an existing user to a user group
For this task, we will use the usermod command. The usermod command can modify a user’s various options, including the user’s group membership.
Colleagues First, I will add a third user group
groupadd colleagues
Copy the code
useusermod
I added the colleagues user group as an associate group
usermod -a -G colleagues tom
Copy the code
Command description: -A indicates append, which can only be used in combination with -g (subordinate group). So we finally added User Tom to the Colleagues group, which is a subordinate group of users.
The -g option allows you to specify multiple user groups, separated by commas. For example, -g group1,group2,group3.
To change the primary group of user Tom to family, run the following command:
usermod -g family tom
Copy the code
Use the man command to get a detailed description of all the command-line options of the usermod command:
man usermod
Copy the code
Add User to Group