User and user group management study notes

First, create a user:

① basic syntax:

useradd [option] username

Common options:

-u specified uid, that uniquely identifies

-g Specifies the primary group

-G specify additional group

-d Specifies the user's home directory (by default created under / home)

Note: When you specify the home directory, the home directory of the parent directory must exist

-s Specifies the user's default shell (/ bin / sh (soft link) ==> / bin / bash (the default), / sbin / nologin: not local landing)

② verification

id Username: View user information

[root@localhost /]# useradd stu1
[root@localhost /]# id stu1
uid=1001(stu1) gid=1001(stu1) 组=1001(stu1)
[root@localhost /]# tail -3 /etc/passwd
tcpdump:x:72:72::/:/sbin/nologin
tom:x:1000:1000:tom:/home/tom:/bin/bash
stu1:x:1001:1001::/home/stu1:/bin/bash
七列:
用户名:密码占位符:用户id:用户主组id:注释:用户家目录:用户使用的shell编辑器
[root@localhost /]# tail -3 /etc/shadow
tcpdump:!!:18329::::::
tom:$6$YSwlQKVW7cJZccgB$iexoOMRAiNpukg3goSYNoOartc1sYk3A5XTPmmQMGYFIq4G2IUKi7TBe.i1b.gp1AYQNjKYgkA.zYBOkXOxh5/::0:99999:7:::
stu1:!!:18329:0:99999:7:::
第一列,用户名
第二列,加密后的密码
第三列,表示上一次修改密码的时间18329:距离linux元年(1970.1.1)的天数
第四列,表示两次密码修改最小间隔时间(天),0表示可以立可修改密码
第五列,表示密码的最大有效期(单位:天)
第六列,表示提前多少天通知密码失效
第七列,密码宽限期
第八列,密码失效时间
第九列,保留
[root@localhost /]#

③ Expansion

chage

Last update -d user password modification time

-m Change Password minimum survival (after a few days can change the password)

-M Change Password maximum survival (how many days can change the password)

-W warned a few days ago to change the password expiration

-I modification allows password expiration days

-E modify the account expiration time

-l lists the account information

[root@localhost /]# chage -l stu1
最近一次密码修改时间                                    :3月 08, 2020
密码过期时间                                    :从不
密码失效时间                                    :从不
帐户过期时间                                            :从不
两次改变密码之间相距的最小天数          :0
两次改变密码之间相距的最大天数          :99999
在密码过期之前警告的天数        :7
[root@localhost /]# chage -E '2020-03-10' stu1
[root@localhost /]# chage -l stu1
最近一次密码修改时间                                    :3月 08, 2020
密码过期时间                                    :从不
密码失效时间                                    :从不
帐户过期时间                                            :3月 10, 2020
两次改变密码之间相距的最小天数          :0
两次改变密码之间相距的最大天数          :99999
在密码过期之前警告的天数        :7

Second, set a password

root: a passwd username.

        b. echo 密码 | passwd 用户名 --stdin / echo 密码 | passwd --stdin 用户名 (非交互式)

Enter passwd direct representation to modify the current user's password

Third, modify user information

① syntax:

usermod [options] user name

Common options:

-u Specifies the user uid, unique identifier must be unique

-g specified user's primary group

-G specify additional user groups

删除附加组:
gpasswd -d 用户名 附加组名

添加多个附加组:
usermod -aG 附加组名 用户名

-d specifies the user's home directory

Note: When you modify a user's home directory, all new home directory must exist

-s Specifies the user's default shell

After -L Lock user, locking the user can not log into the system

-U Unlock

② Case:

a.修改用户uid
[root@localhost chome]# id user1
uid=1002(user1) gid=1002(user1) 组=1002(user1)
[root@localhost chome]# usermod -u 1111 user1
[root@localhost chome]# id user1
uid=1111(user1) gid=1002(user1) 组=1002(user1)


b.修改用户的家目录
[root@localhost chome]# mkdir -p /chome/redhat/user1
[root@localhost chome]# su - user1
上一次登录:一 3月  9 09:59:07 CST 2020pts/0 上
[user1@localhost ~]$ pwd
/chome/user1
[user1@localhost ~]$ exit
登出
[root@localhost chome]# usermod -d /chome/redhat/user1 user1
[root@localhost chome]# su - user1
上一次登录:一 3月  9 10:46:36 CST 2020pts/0 上
###出现该现象
-bash-4.2$ pwd
/chome/redhat/user1
-bash-4.2$ exit
登出
###解决办法
[root@localhost chome]# cp -a /etc/skel/. /chome/redhat/user1/
[root@localhost chome]# su - user1
上一次登录:一 3月  9 10:47:17 CST 2020pts/0 上
[user1@localhost ~]$ pwd
/chome/redhat/user1
[root@localhost chome]# ls -a /etc/skel/
.  ..  .bash_logout  .bash_profile  .bashrc  .mozilla
[root@localhost chome]#

-----------
修改用户家目录
usermod -md 新目录 用户名


c.修改用户的附加组
[root@localhost chome]# head -1 /etc/group
root:x:0:
[root@localhost chome]# usermod -G 0 stu1
[root@localhost chome]# head -1 /etc/group
root:x:0:stu1


d.修改用户的shell
[root@localhost /]# useradd -s /sbin/nologin stu2
[root@localhost /]# su - stu2
This account is currently not available.
[root@localhost /]# usermod -s /bin/bash stu2
[root@localhost /]# su -stu2
上一次登录:一 3月  9 11:16:53 CST 2020pts/0 上
[stu2@localhost ~]$ exit
登出

Fourth, delete users

grammar:

userdel [options] username

Common options:

-r Delete the user's home directory and delete user mailboxes (/ var / spool / mail)

-f forcibly remove a user is logged on

Fifth, group

① Create Group

groupadd [options] group name

Common options:

-g Specifies the group id, unique

[root@localhost /]# groupadd admin
[root@localhost /]# tail -1 /etc/group
admin:x:1002:

admin    :  x        : 1002       :
组名		 密码占位符	 组id		用户列表

[root@localhost /]# groupadd -g 1234 system
[root@localhost /]# tail -1 /etc/group
system:x:1234:

② Delete Group

groupdel group name

Note: This group if a user's primary group can not be deleted. If only additional group, you can delete

③ group member management

gpasswd [options] user name group name (additional group)

Common options:

-a Adding users to groups (represented added)

-d remove members from a group

-A designated administrator

-M specified group members (a plurality of members separated by commas. Overwriting)

-r remove the password

gpasswd group name ==> to set password

Published an original article · won praise 4 · Views 5436

Guess you like

Origin blog.csdn.net/zkxsf/article/details/104758738