用户管理
1.查看当前用户
- id 0 超级用户
- id 1~999 系统用户
- id 1000~65535 普通用户
[alexw@alexw-device ~]$ id alexw
uid=1001(alexw) gid=1001(alexw) groups=1001(alexw)
[alexw@alexw-device ~]$ id -u alexw
1001
[alexw@alexw-device ~]$ id -g alexw
1001
[alexw@alexw-device ~]$ id -G alexw
1001
[alexw@alexw-device ~]$ id -Gn alexw
alexw
[alexw@alexw-device ~]$ id -gn alexw
alexw
[alexw@alexw-device ~]$ id -un alexw
alexw
2.用户切换
- 带’-',带环境切换
- 不带’-',不带环境切换
[alexw@alexw-device ~]$ pwd
/home/alexw
[alexw@alexw-device ~]$ su -
Password:
Last login: Thu Oct 27 19:52:28 CST 2022 on pts/0
[root@alexw-device ~]# pwd
/root
[root@alexw-device ~]# su alexw
[alexw@alexw-device root]$ pwd
/root
3.手动创建用户并配置家目录
- 用户名称:用户密码:用户id:用户主组id:用户说明:用户家目录:用户默认shell
- 组名称:组密码:组id:组的附加成员
[root@alexw-device ~]# vim /etc/passwd
编辑:
cindy:x:1003:1003::/home/cindy:/bin/bash
[root@alexw-device ~]# vim /etc/group
编辑:
cindy:x:1003:
[root@alexw-device ~]# mkdir /home/cindy
[root@alexw-device ~]# ls -l /etc/skel/ -a
total 24
drwxr-xr-x. 3 root root 78 Jun 22 2021 .
drwxr-xr-x. 139 root root 8192 Nov 5 10:57 ..
-rw-r--r--. 1 root root 18 Aug 30 2019 .bash_logout
-rw-r--r--. 1 root root 141 Aug 30 2019 .bash_profile
-rw-r--r--. 1 root root 312 Aug 30 2019 .bashrc
drwxr-xr-x. 4 root root 39 Jun 22 2021 .mozilla
[root@alexw-device ~]# cp /etc/skel/.* /home/cindy/
cp: -r not specified; omitting directory '/etc/skel/.'
cp: -r not specified; omitting directory '/etc/skel/..'
cp: -r not specified; omitting directory '/etc/skel/.mozilla'
[root@alexw-device ~]# chown cindy.cindy /home/cindy -R
[root@alexw-device ~]# passwd cindy
Changing password for user cindy.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
4.命令行添加用户和组
- 监控几个文件和目录
watch -n 1 "tail -n 4 /etc/passwd /etc/group;echo =======;ls -l /home"
[root@alexw-device ~]# groupadd newgroup
[root@alexw-device ~]# groupdel newgroup
[root@alexw-device ~]# groupadd -g 10086 newgroup
[root@alexw-device ~]# useradd -u 10086 liantong -g 10086 -d /home/liantong/ -s /bin/bash
[root@alexw-device ~]# useradd yidong -M
[root@alexw-device ~]# userdel yidong
如图
5.命令行修改用户和组
usermod
-l #更改用户名称
-u #更改用户id
-g #更改主组id
-G #更改用户附加组身份
-aG #添加用户附加组身份
-c #更改用户说明
-d #更改家目录指向
-md #更改家目录指向同时更改家目录名称
-s #更改默认shell
-L #冻结账号
-U #解锁
groupmod -g ##更改用户组id
6.修改密码
- 交互式和非交互式
[root@alexw-device ~]# passwd cindyee
Changing password for user cindyee.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[root@alexw-device ~]# echo 12345678 | passwd --stdin cindyee
Changing password for user cindyee.
passwd: all authentication tokens updated successfully.
- 其他命令
查看密码状态
[root@alexw-device ~]# passwd -S cindyee
cindyee PS 2022-11-05 -1 -1 -1 -1 (Password set, SHA512 crypt.)
锁定用户
[root@alexw-device ~]# passwd -l cindyee
Locking password for user cindyee.
passwd: Success
解锁用户
[root@alexw-device ~]# passwd -u cindyee
Unlocking password for user cindyee.
passwd: Success
删除用户的密码
[root@alexw-device ~]# passwd -d cindyee
Removing password for user cindyee.
passwd: Success
设置用户密码过期时间,0表示每次登录强制修改密码
[root@alexw-device ~]# passwd -e cindyee 或 chage -d 0 cindyee
Expiring password for user cindyee.
passwd: Success
最长有效时间
[root@alexw-device ~]# passwd -x 30 cindyee 或 chage -M 30 cindyee
Adjusting aging data for user cindyee.
passwd: Success
最短有效时间
[root@alexw-device ~]# passwd -n 1 cindyee 或 chage -m 1 cindyee
Adjusting aging data for user cindyee.
passwd: Success
到期警告时间
[root@alexw-device ~]# passwd -w 2 cindyee 或 chage -W 2 cindyee
Adjusting aging data for user cindyee.
passwd: Success
到期后灵活时间
[root@alexw-device ~]# passwd -i 1 cindyee 或 chage -I 1 cindyee
Adjusting aging data for user cindyee.
passwd: Success
到期时间
chage -E "2023-01-01" cindyee