Under the Linux operating system, how do I add a new user to a specific group? How do I add users to multiple groups at the same time? How do you move an existing user to a group or add a group to him? For people who don't use Linux, it's not easy to memorize a variety of command-line operations on Linux.
In Linux, adding a user or changing a user's group properties can be used useradd or usermod ordered. useraddadd a new user or update the default new user information. usermodis to change the user account properties, such as adding them to an existing group.
There are two types of groups in the Linux user system. The first class is the primary user group, and the second is the additional user group. All user accounts and related information are stored in the /etc/passwd file, /etc/shadow and /etc/group the file stores the user information.
Catalogue Contents
Useradd Example – Adding a new user to an additional user group
A command is required to add a new user and include it in an existing user group useradd . If you do not have this user group, you can create the user group first.
The command parameters are as follows:
useradd -G {group-name} username
For example, we will create a new user CentOS and add it to the user group developers. You first need to log in to the system as the root user. First confirm that there is developers this user group, enter at the command line:
# grep developers /etc/group
The output is similar to:
developers:x:1124:
If you don't see any output, you'll need to create the user group first, using the groupadd command:
# groupadd developers
Then create the user CentOS and add it to the developers user group:
# useradd -G developers centos
To set a password for the user centos:
# passwd centos
To ensure that the user has been correctly added to the developers user group, you can view the user's properties by using the id command:
# id centos
The output is similar to:
uid=1122 (CentOS) gid=1125 (CentOS) groups=1125 (CentOS), 1124 (CentOS)
The uppercase G (-G) parameter used in the preceding command is intended to add the user to an additional user group, while creating a new group of CentOS for the user. If you want to add the user to more than one additional user group, you can use commas in the English half to separate multiple additional group names (without spaces). For example, add CentOS to the admins, FTP, WWW, and developers user groups, and you can enter the following commands:
# useradd -G admins,ftp,www,developers centos
Useradd Example – Add a new user to the primary user group
To add user CentOS to group developers, you can use the following instructions:
# useradd -g developers centos# id centos
The output is similar to:
Uid=1123 (CentOS) gid=1124 (developers) groups=1124 (developers)
Note that, as in the previous example, the lowercase g (-g) is used as the parameter, when the user's primary user group is no longer centos and is directly developers.
The Small Letter g (-G) Initializes the newly added user to the specified logon group (the primary user group). This group name must already exist. The group number (GID) is the group number of this existing group.
Usermod Example – Adding an existing user to an existing user group
Add an existing user CentOS to an existing user group Apache, which makes this user an additional user group for that user, and can use instructions with the-a parameter usermod . -A represents append, which means that users are added to the new user group without having to leave the original other user group. However, you need to use the-G option with:
# usermod -a -G apache centos
If you want to change the primary CentOS user group to Apache at the same time, use the-G option directly:
# usermod -g apache centos
If you want to remove a user from a group,
gpasswd-d User Group
But this time you need to make sure that group is not the user's primary group.
Attached: tools or commands for managing users and user groups (group)
1) tools or commands for managing users (user)
useradd Note: Add user
adduser Note: Add user
passwd Note: Set the password for the user
Usermod Note: Modify the user command, you can modify the login name through Usermod, the user's home directory, etc.
Pwcov NOTE: Synchronizing users from/etc/ passwd to/etc/shadow
Pwck NOTE: PWCK is verifying that the contents of the user profile/etc/passwd and/etc/shadow files are legitimate or complete;
Pwunconv NOTE: It is pwcov to create a/etc/passwd from/etc/shadow and/etc/passwd, and then delete the/etc/shadow file;
Finger Note: View the user Information tool
ID NOTE: View the UID, GID, and user group to which the user belongs
Chfn NOTE: Change user information tool
Su NOTE: User switch tool
sudo NOTE: sudo is executed by another user (execute a command as another user), Su is used to cut Change the user, and then the user to complete the task by switching to, but Sudo can execute the command directly, such as sudo does not require root password to perform root execution only root can execute the corresponding command; but it has to be edited by Visudo/etc/ Sudoers to implement;
Visudo NOTE: Visodo is the command to edit/etc/sudoers, or you can edit/etc/sudoers directly with vi without this command;
Sudoedit Note: similar to sudo function;
2) tools or commands to manage user groups (group)
groupadd 注:添加用户组;
groupdel 注:删除用户组;
groupmod 注:修改用户组信息
groups 注:显示用户所属的用户组
grpck
grpconv 注:通过/etc/group和/etc/gshadow 的文件内容来同步或创建/etc/gshadow ,如果/etc/gshadow 不存在则创建;
grpunconv 注:通过/etc/group 和/etc/gshadow 文件内容来同步或创建/etc/group ,然后删除gshadow文件;
Linux-user-group Adding and removing