First, the common usage of the two commands is expounded separately.
Usermod function: Modify user information
usage: usermod option parameter user name
Common options:
-C (comment) : Modify user's comment information
- D (home directory): Modify the user's home directory
- L (login name): Modify user name (login name is user name)
- G (group name): Modify the group to which the user belongs
What needs to be explained is that after Usermod modifies the user's information, it is viewed in/etc/passwd, and each user's information consists of 7 fields separated by a colon.
gpasswd function: Add or remove group members
usage: gpasswd option parameter Group name
Common options:
-A (ADD): Add Group member
-D (delete): Delete Group members
The basic usage of the two commands is as follows, which illustrates the difference between usermod-g and gpasswd-a, in order to illustrate the problem, create a scene here
below Create group members in two ways
First Kind
Useradd User3
Groupadd Student
USERADD-G Student User3
[Email protected] ~]# Useradd User3
[Email protected] ~]# tail-1/etc/passwd
User3:x:503:504::/home/user3:/bin/bash
[Email protected] ~]# Groupadd Student
[Email protected] ~]# tail-1/etc/group
student:x:505:
[Email protected] ~]# usermod-g Studentuser3
[Email protected] ~]# tail-3/etc/passwd
User1:x:501:501:df,fd,df,df:/home/user1:/bin/bash
User2:x:502:502::/home/user2:/bin/bash
User3:x:503:505::/home/user3:/bin/bash
[Email protected] ~]# tail-3/etc/group
Group2:x:502:user1
user3:x:504:
student:x:505:
Two kinds of brother
groupadd student
Useradd User3
Gpasswd-a User3 Student
When you practice the second command, you can remove the users and groups created in the first method, and then reestablish them to avoid problems
[Email protected] ~]# Groupadd Student
[Email protected] ~]# Useradd User3
Creating Mailbox File:file exists
[Email protected] ~]# tail-3/etc/passwd
User1:x:501:501:df,fd,df,df:/home/user1:/bin/bash
User2:x:502:502::/home/user2:/bin/bash
User3:x:503:504::/home/user3:/bin/bash
[Email protected] ~]# tail-1/etc/group
user3:x:504:
[Email protected] ~]# gpasswd-a user3student
Adding user User3 to group student
[Email protected] ~]# Tail-2/etc/group
Student:x:503:user3
user3:x:504:
[Email protected] ~]# tail-3/etc/passwd
User1:x:501:501:df,fd,df,df:/home/user1:/bin/bash
User2:x:502:502::/home/user2:/bin/bash
User3:x:503:504::/home/user3:/bin/bash
The first is that the student group in/ect/group has no User3 user but the User3 group ID in/ETC/PASSWD is the ID of the student group, and when the User3 user is created, the default user group is the User3 group ( when new users are added, If you do not specify a group for the user, automatically create a group named after the user name, and then add the user to the group , and then use Usermod-g to modify the base group of the User3 user to modify the User3 user's base group to student, At this point the student group has become the base group for User3 users, so there are no user3 users in the student group (basic groups in/etc/group are not displayed, additional groups are displayed), and the base group ID in the User3 user is the student ID.
The second is the presence of student and user3 groups in/etc/group and User3 users in the student group, which is to add User3 users to the group student, for student users added to the User3 group, The student group is an additional group of User3 users. Note, however, that the User3 user is added to the student group, but the User3 group still exists independently, which is like a person who originally belonged to the development department and was temporarily transferred to the testing department to work, so he was added to the Test team so that he could work with the other members of the testing department. But the development department is his basic group and still exists.
To summarize: Gpasswd-a is to add additional groups to the user or remove additional groups, is an additional group of a user operation, that is, to add a user to the additional group or to remove a user's additional group, the user's basic group remains unchanged.
usermod-g is to modify the basic group of a user, is to modify the user's basic group, that is, modify the fourth field of the/etc/passwd.
This article is from the Linux learning blog, so be sure to keep this source http://yongtao.blog.51cto.com/10183629/1687595
Differences between Linux commands gpasswd-a and usermod-g