用户管理命令
用户管理命令
- useradd
- usermod
- userdel
用户创建
useradd 命令可以创建新的Linux用户
格式:
useradd [options] LOGIN
常见选项:
-u UID
-o 配合-u 选项,不检查UID的唯一性
-g GID 指明用户所属基本组,可为组名,也可以GID
-c "COMMENT“ 用户的注释信息
-d HOME_DIR 以指定的路径(不存在)为家目录
-s SHELL 指明用户的默认shell程序,可用列表在/etc/shells文件中
-G GROUP1[,GROUP2,...] 为用户指明附加组,组须事先存在
-N 不创建私用组做主组,使用users组做主组
-r 创建系统用户 CentOS 6之前: ID<500,CentOS 7以后: ID<1000
-m 创建家目录,用于系统用户
-M 不创建家目录,用于非系统用户
范例:
useradd -r -u 48 -g apache -s /sbin/nologin -d /var/www -c "Apache" apache
useradd 命令默认值设定由/etc/default/useradd定义
[root@centos8 ~]#cat /etc/default/useradd
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1 #对应/etc/shadow文件第7列,即用户密码过期的宽限期
EXPIRE= #对应/etc/shadow文件第8列,即用户帐号的有效期
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
显示或更改默认设置
useradd -D
useradd –D -s SHELL
useradd –D –b BASE_DIR
useradd –D –g GROUP
新建用户的相关文件
- /etc/default/useradd
- /etc/skel/*
- /etc/login.defs
批量创建用户
newusers passwd 格式文件
批量修改用户口令
echo username:passwd | chpasswd
用户属性修改
usermod 命令可以修改用户属性
格式:
usermod [OPTION] login
常见选项:
-u UID: 新UID
-g GID: 新主组
-G GROUP1[,GROUP2,...[,GROUPN]]]:新附加组,原来的附加组将会被覆盖;若保留原有,则要同时使用-a选项
-s SHELL:新的默认SHELL
-c 'COMMENT':新的注释信息
-d HOME: 新家目录不会自动创建;若要创建新家目录并移动原家数据,同时使用-m选项
-l login_name: 新的名字
-L: lock指定用户,在/etc/shadow 密码栏的增加 !
-U: unlock指定用户,将 /etc/shadow 密码栏的 ! 拿掉
-e YYYY-MM-DD: 指明用户账号过期日期
-f INACTIVE: 设定非活动期限,即宽限期
删除用户
userdel 可删除Linux 用户
格式:
userdel [OPTION]... Login
常见选项:
-f, --force 强制
-r, --remove 删除用户家目录和邮箱
查看用户相关的ID信息
id 命令可以查看用户的UID,GID等信息
id [OPTION]... [USER]
常见选项:
-u: 显示UID
-g: 显示GID
-G: 显示用户所属的组的ID
-n: 显示名称,需配合ugG使用
切换用户或以其他用户身份执行命令
su: 即 switch user,命令可以切换用户身份,并且以指定用户的身份执行命令
格式:
su [options...] [-] [user [args...]]
常见选项:
-l --login su -l UserName 相当于 su - UserName
-c, --command <command> pass a single command to the shell with -c
切换用户的方式:
- su UserName:非登录式切换,即不会读取目标用户的配置文件,不改变当前工作目录,即不完全切换
- su – UserName:登录式切换,会读取目标用户的配置文件,切换至自已的家目录,即完全切换
说明:root su至其他用户无须密码;非root用户切换时需要密码
注意:su 切换新用户后,使用 exit 退回至旧的用户,而不要再用 su 切换至旧用户,否则会生成很多的bash子进程,环境可能会混乱。
换个身份执行命令:
su [-] UserName -c 'COMMAND'
范例:
[root@centos8 ~]#getent passwd mage
mage:x:1001:1001::/home/mage:/bin/bash
[root@centos8 ~]#usermod -s /bin/false mage
[root@centos8 ~]#getent passwd mage
mage:x:1001:1001::/home/mage:/bin/false
[root@centos8 ~]#su - mage
Last login: Fri Mar 27 09:18:57 CST 2020 on pts/0
[root@centos8 ~]#whomai
-bash: whomai: command not found
[root@centos8 ~]#whoami
root
范例:
[root@centos8 ~]#su -s /sbin/nologin wang
This account is currently not available.
[root@centos8 ~]#whoami
root
[root@centos8 ~]#su -s /bin/false wang
[root@centos8 ~]#whoami
root
范例:
[wang@centos8 ~]$su - root -c "getent shadow"
范例:
[root@centos8 ~]#su - wang -c 'touch wang.txt'
[root@centos8 ~]#ll ~wang/
total 0
-rw-rw-r-- 1 wang wang 0 Mar 27 09:31 wang1.txt
-rw-rw-r-- 1 wang wang 0 Mar 27 09:32 wang2.txt
范例:
[root@centos8 ~]#su bin
This account is currently not available.
[root@centos8 ~]#su -s /bin/bash bin
bash-4.4$ whoami
bin
bash-4.4$
[root@centos8 ~]#getent passwd tss
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
[root@centos8 ~]#su - -s /bin/bash tss
Last login: Fri Mar 27 09:46:43 CST 2020 on pts/0
su: warning: cannot change directory to /dev/null: Not a directory
-bash: /dev/null/.bash_profile: Not a directory
[tss@centos8 root]$pwd
/root
[tss@centos8 root]$whoami
tss
设置密码
passwd 可以修改用户密码
格式:
passwd [OPTIONS] UserName
常用选项:
-d:删除指定用户密码
-l:锁定指定用户
-u:解锁指定用户
-e:强制用户下次登录修改密码
-f:强制操作
-n mindays:指定最短使用期限
-x maxdays:最大使用期限
-w warndays:提前多少天开始警告
-i inactivedays:非活动期限
--stdin:从标准输入接收用户密码,Ubuntu无此选项
范例:
echo "PASSWORD" | passwd --stdin USERNAME
范例:设置用户下次必须更改密码
[root@centos8 ~]#useradd wang
[root@centos8 ~]#echo 123456 | passwd --stdin wang
Changing password for user wang.
passwd: all authentication tokens updated successfully.
[root@centos8 ~]#getent shadow wang
wang:$6$4f78ko7hJ4fcMvIH$lpbOkFfziDBLT.8XBCi8c/N7wysDAejN5H9Fgxkt99HRDLTEosO43CKYi2XSSVHxAK568Olj3C5bwfNExlves/:18348:0:99999:7:::
[root@centos8 ~]#passwd -e wang
Expiring password for user wang.
passwd: Success
[root@centos8 ~]#getent shadow wang
wang:$6$4f78ko7hJ4fcMvIH$lpbOkFfziDBLT.8XBCi8c/N7wysDAejN5H9Fgxkt99HRDLTEosO43CKYi2XSSVHxAK568Olj3C5bwfNExlves/:0:0:99999:7:::
[root@centos8 ~]#su - mage
Last login: Fri Mar 27 09:55:27 CST 2020 on pts/0
[mage@centos8 ~]$su - wang
Password:
You are required to change your password immediately (administrator enforced)
Current password:
New password:
BAD PASSWORD: The password is shorter than 8 characters
New password:
BAD PASSWORD: The password fails the dictionary check - it is too simplistic/systematic
su: Have exhausted maximum number of retries for service
[mage@centos8 ~]$su - wang
Password:
You are required to change your password immediately (administrator enforced)
Current password:
New password:
Retype new password:
Last login: Fri Mar 27 10:01:20 CST 2020 on pts/0
Last failed login: Fri Mar 27 10:02:37 CST 2020 on pts/0
There was 1 failed login attempt since the last successful login.
[wang@centos8 ~]$exit
logout
[mage@centos8 ~]$exit
logout
[root@centos8 ~]#getent shadow wang
wang:$6$TX0iLjF52ByHh1zH$g.WI4LNfauuwgnxpRhd7ePqFKHZ85YU3r6Lh2S0PWRXWGjGlDVtomLWqpdiWrT.vwqD/Wzok.kzQhUHc8UCs91:18348:0:99999:7:::
修改用户密码策略
chage 可以修改用户密码策略
格式:
chage [OPTION]... LOGIN
常见选项:
-d LAST_DAY #更改密码的时间
-m --mindays MIN_DAYS
-M --maxdays MAX_DAYS
-W --warndays WARN_DAYS
-I --inactive INACTIVE #密码过期后的宽限期
-E --expiredate EXPIRE_DATE #用户的有效期
-l 显示密码策略
范例:
[root@centos8 ~]#chage -m 3 -M 42 -W 14 -I 7 -E 2020-10-10 wang
[root@centos8 ~]#chage -l wang
Last password change : Dec 18, 2019
Password expires : Jan 29, 2020
Password inactive : Feb 05, 2020
Account expires : Oct 10, 2020
Minimum number of days between password change : 3
Maximum number of days between password change : 42
Number of days of warning before password expires : 14
[root@centos8 ~]#getent shadow wang
wang:$6$82L7A37XJgzKTegH$lFzqrMHmFwW740U32bvWHUuakPDKOiULE/CxcyDzSe1qi1X2ALulDw1WYrhd2wE00.lWO0im5//7biyV.juk5.:18248:3:42:14:7:18545:
#下一次登录强制重设密码
[root@centos8 ~]#chage -d 0 wang
[root@centos8 ~]#getent shadow wang
wang:$6$82L7A37XJgzKTegH$lFzqrMHmFwW740U32bvWHUuakPDKOiULE/CxcyDzSe1qi1X2ALulDw1WYrhd2wE00.lWO0im5//7biyV.juk5.:0:3:42:14:7:18545:
[root@centos8 ~]#chage -l wang
Last password change : password must be changed
Password expires : password must be changed
Password inactive : password must be changed
Account expires : Oct 10, 2020
Minimum number of days between password change : 3
Maximum number of days between password change : 42
Number of days of warning before password expires : 14
[root@centos8 ~]#getent shadow wang
wang:$6$82L7A37XJgzKTegH$lFzqrMHmFwW740U32bvWHUuakPDKOiULE/CxcyDzSe1qi1X2ALulDw1WYrhd2wE00.lWO0im5//7biyV.juk5.:0:3:42:14:7:18545:
用户相关的其它命令
- chfn 指定个人信息
- chsh 指定shell,相当于usermod -s
- finger 可看用户个人信息
范例:
[root@centos7 ~]#chfn wang
Changing finger information for wang.
Name [wang]: wangxiaochun
Office []:it
Office Phone []: 10000
Home Phone []: 11111
Finger information changed.
[root@centos7 ~]#finger wang
Login: wang Name: wangxiaochun
Directory: /home/wang Shell: /bin/bash
Office: it, x1-0000 Home Phone: x1-1111
Never logged in.
No mail.
No Plan.
[root@centos7 ~]#getent passwd wang
wang:x:1000:1000:wangxiaochun,it,10000,11111:/home/wang:/bin/bash
[root@centos7 ~]#chsh -s /bin/csh wang
Changing shell for wang.
Shell changed.
[root@centos7 ~]#getent passwd wang
wang:x:1000:1000:wangsicong,wanda,10000,11111:/home/wang:/bin/csh
[root@centos7 ~]#usermod -s /bin/bash wang
[root@centos7 ~]#getent passwd wang
wang:x:1000:1000:wangsicong,wanda,10000,11111:/home/wang:/bin/bash
范例: 修改用户使用不可登录的shell类型
[root@centos8 ~]#getent passwd wang
wang:x:1000:1000:wangxiaochun,IT,110,119,:/home/wang:/bin/bash
[root@centos8 ~]#chsh -s /sbin/nologin wang
Changing shell for wang.
chsh: Warning: "/sbin/nologin" is not listed in /etc/shells.
Shell changed.
[root@centos8 ~]#su - wang
This account is currently not available.
[root@centos8 ~]#chsh -s /bin/false wang
Changing shell for wang.
chsh: Warning: "/bin/false" is not listed in /etc/shells.
Shell changed.
[root@centos8 ~]#su - wang
[root@centos8 ~]#id
uid=0(root) gid=0(root) groups=0(root)
[root@centos8 ~]#chsh -s /bin/bash wang
Changing shell for wang.
Shell changed.
[root@centos8 ~]#su - wang
[wang@centos8 ~]$