linux-user-group add and delete

How to add a new user to a specific group under Linux operating system? How to add users to multiple groups at the same time? How to move an existing user to a group or add a group to him? For people who don't use Linux frequently, it is really difficult to memorize the various command-line operations of Linux.

useradd In Linux, the or  usermod command can be used to add a user or change a user's group attributes  . useraddAdd a new user or update the default new user information. usermod Change the user account properties, such as adding it to an existing group.

There are two types of groups in the Linux user system. The first category is the main user group and the second category is the additional user group. All user accounts and related information are stored in  /etc/passwd files, /etc/shadow and  /etc/group files store user information.

Directory Contents

useradd example - add a new user to the additional user group

Adding a new user and adding it to an existing user group requires  useradd commands. If there is no such user group, you can create the user group first.

The command parameters are as follows:

useradd -G {group-name} username

For example, we want to create a new user centos and add it to the user group developers. First you need to log in to the system as the root user. First confirm whether there is a user group developers, enter at the command line:

# grep developers /etc/group

The output is similar to:

developers:x:1124:

If you don't see any output, then you need to create this user group first, using the  groupadd command:

# groupadd developers

Then create the user centos and add it to the developers group:

# useradd -G developers centos

Set password for user centos:

# passwd centos

To ensure that the user has been correctly added to the developers user group, you can view the properties of the user, use the  id command:

# id centos

The output is similar to:

uid = 1122 (hundreds) gid = 1125 (hundreds) groups = 1125 (hundreds), 1124 (hundreds)

The capitalized G (-G) parameter used in the previous command is to add the user to an additional user group, while also creating a new group centos of his own for the user. If you want to add the user to multiple additional user groups at the same time, you can use a comma to separate multiple additional group names (do not add spaces). For example, to add centos to the admins, ftp, www, and developers groups at the same time, enter the following command:

# useradd -G admins,ftp,www,developers centos

useradd example - add a new user to the main user group

To add user centos to group developers, use the following command:

# useradd -g developers centos
# id centos

The output is similar to:

uid=1123(centos) gid=1124(developers) groups=1124(developers)

Please note the difference as in the previous example, where the lowercase letter g (-g) is used as the parameter, and the user's main user group is no longer centos but directly developers.

Lowercase g (-g) initializes the newly added user to be designated as the login group (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

To add an existing user centos to an existing user group apache to make this user group an additional user group for the user, you can use the  usermod  command with the -a parameter. -a stands for append, which is to add users to a new user group without leaving the original other user groups. However, it needs to be used in conjunction with the -G option:

# usermod -a -G apache centos

If you want to change the main user group of centos 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, then

gpasswd -d user group

But at this time, you need to ensure that group is not the main group of user.

Attachment: related tools or commands for managing users (user) and user groups (group)

1) Tools or commands to manage users (user)

useradd    注:添加用户
adduser    注:添加用户
passwd     注:为用户设置密码
usermod    注:修改用户命令,可以通过usermod 来修改登录名、用户的家目录等等;
pwcov      注:同步用户从/etc/passwd 到/etc/shadow
pwck       注:pwck是校验用户配置文件/etc/passwd 和/etc/shadow 文件内容是否合法或完整;
pwunconv   注:是pwcov 的立逆向操作,是从/etc/shadow和 /etc/passwd 创建/etc/passwd ,然后会删除 /etc/shadow 文件;
finger     注:查看用户信息工具
id         注:查看用户的UID、GID及所归属的用户组
chfn       注:更改用户信息工具
su         注:用户切换工具
sudo       注:sudo 是通过另一个用户来执行命令(execute a command as another user),su 是用来切换用户,然后通过切换到的用户来完成相应的任务,但sudo 能后面直接执行命令,比如sudo 不需要root 密码就可以执行root 赋与的执行只有root才能执行相应的命令;但得通过visudo 来编辑/etc/sudoers来实现;
visudo     注:visodo 是编辑 /etc/sudoers 的命令;也可以不用这个命令,直接用vi 来编辑 /etc/sudoers 的效果是一样的;
sudoedit   注:和sudo 功能差不多;

2) Tools or commands for managing user groups (groups)
groupadd    注:添加用户组;
groupdel    注:删除用户组;
groupmod    注:修改用户组信息
groups      注:显示用户所属的用户组
grpck
grpconv     注:通过/etc/group和/etc/gshadow 的文件内容来同步或创建/etc/gshadow ,如果/etc/gshadow 不存在则创建;
grpunconv   注:通过/etc/group 和/etc/gshadow 文件内容来同步或创建/etc/group ,然后删除gshadow文件;

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324829226&siteId=291194637