用户管理与用户组管理
目录用户配置文件
用户配置文件:查看和修改用户信息
-
passwd(7)
man 5 passwd
查看配置文件格式account:passwd:UID:GID:GECOS:Directory:shell
- UID(用户ID)
UID 0 超级用户 1-499 系统用户/伪用户 500-65535 普通用户 centos7普通用户从1000开始
-
GID(组ID)
初始组:用户同名组,只能有一个,可修改
附加组:可加入其他用户组,拥有其权限,多个
-
GECOS(用户说明)
-
Directory(家目录)
/home/用户名/和/root/
初始登录目录,目录权限普通用户700,root 500(ll 目录 -d)
-
shell(命令解释器)
/bin/bash/
/sbin/nologin/
-
shadow(9)
用户名:加密密码:密码最后修改日期:修改间隔:有效期:密码到期警告时间(前):过期宽限时间(后):账号失效时间:保留
[root@localhost /]# cat /etc/shadow root:$6$LTfUqz93SCp1l/5f$kUs9vL1Id3R0oJX..87IBppsNTUUzIjOtJqX5QLBcR0og9nWKkKtNmKxbuNh7c.ig3iRmwHCN9CFTkmoZmDR.1::0:99999:7::: bin:*:17834:0:99999:7::: daemon:*:17834:0:99999:7::: ... postfix:!!:18530:::::: tcpdump:!!:18530:::::: jen:$6$DxyOAZQ9KLjyTlOR$jgFRNe890lqam.6af1UcNPO3QnMKcT2fCoZOUOedVH8gcCP/YL8/2XAediNz/fO/gE2ljAPTEHrrm0u0HzCpg.:18530:0:99999:7::: user1:$6$IAozruFF$q/NV8tXYtIq.Z9IzWAy8.cJaYAx4y/o/wc8KNLzgVMmtT1K0iX7OZaPScpeDZUKDzU1A/ZraSGU9efGz0V/cN1:18530:0:99999:7::: tuser:!!:18530:0:99999:7:::
-
2加密密码:sha512,!!或*表示无密码。可能被爆破但密码加盐撞库成功可能性不大
-
日期均为时间戳(1970-01-01到当天的天数)
date -d "1970-01-01 16066 days" #时间戳-日期 echo $(($(date--date="2014/01/06"+%s)/86400+1)) #日期-时间戳
-
-
/etc/group
组名:组密码:GID:组中附加用户
root:x:0: bin:x:1:bin,dacmon(附加用户)
-
/etc/gshadow
组名:组密码:组管理管名:组中附加用户
”root“中的小组长
用户管理相关文件
-
用户家目录
初始登录位置
普通用户:/home/用户,所属者和所属组均为此用户,权限700
超级用户:/root/,----,权限550
注意:普通用户变为超级用户只需在/etc/passwd将uid变为0,重新登录家目录不变,权限升级
-
用户邮箱 /var/spool/mail/用户名
-
用户模板文件 /etc/skel/
[root@localhost /]# ls -a /etc/skel/ . .. .bash_logout .bash_profile .bashrc .mozilla [root@localhost /]# ls -a /home/jen . .bashrc Documents .mozilla Videos .. .cache Downloads Music .viminfo .bash_history .config .esd_auth Pictures 桌面 .bash_logout .dbus .ICEauthority Public .bash_profile Desktop .local Templates
用户管理命令
-
添加用户useradd
[root@localhost /]# useradd user2 [root@localhost /]# passwd user2 Changing password for user user2. New password: BAD PASSWORD: The password is shorter than 8 characters Retype new password: passwd: all authentication tokens updated successfully. [root@localhost /]# grep user2 /etc/passwd user2:x:1089:1089::/home/user2:/bin/bash [root@localhost /]# grep user2 /etc/shadow user2:$6$bKGQ1LLd$iYnb61gldgzq6ePyvYXi9jOXRYL8mzTEGGJp3cq96wh8a0u2AdYQPJFqOc.qZcwyXUdKYuVVQH1cZt3lLI9QX.:18530:0:99999:7::: [root@localhost /]# grep user2 /etc/group user2:x:1089: [root@localhost /]# grep user2 /etc/gshadow user2:!:: [root@localhost /]# ls /home jen tuser user1 user2 [root@localhost /]# ls /var/spool/mail/ jen root rpc tuser user1 user2
手动创建: 指定uid(-u), 用户组(-g初始组gid,-G附加组),备注(-c),家目录(-d),shell(-s)
[root@localhost /]# useradd -u 1088 -G root,bin -c "test user" -d /home/tuser -s /bin/bash tuser [root@localhost /]# ls /home jen tuser user1 [root@localhost /]# cat /etc/passwd ... tuser:x:1088:1088:test user:/home/tuser:/bin/bash [root@localhost /]# grep tuser /etc/group root:x:0:tuser bin:x:1:tuser tuser:x:1088:
缺省选项/默认
/etc/default/useradd
GROUP=100 #用户默认组 Home=/home #家目录 INACTIVE=-1 #密码过期宽限天数(shadow文件第7字段) EXPIRE= #密码失效时间(8) SHELL=/bin/bash #默认shell目录 SKEL=/etc/skel #默认模板 CREATE_MAIL_SPOOL=yes #是否建立邮箱
/etc/login.defs
[root@localhost /]# grep -v ^# /etc//login.defs MAIL_DIR /var/spool/mail PASS_MAX_DAYS 99999 #密码有效期5 PASS_MIN_DAYS 0 #修改间隔4 PASS_MIN_LEN 5 #密码最小位PAM PASS_WARN_AGE 7 #到期警告6 UID_MIN 1000 #最小最大UID范围 UID_MAX 60000 SYS_UID_MIN 201 SYS_UID_MAX 999 GID_MIN 1000 GID_MAX 60000 SYS_GID_MIN 201 SYS_GID_MAX 999 CREATE_HOME yes UMASK 077 USERGROUPS_ENAB yes ENCRYPT_METHOD SHA512
-
修改密码passwd
普通用户修改自己密码:passwd
管理员修改他人:passwd user
管理员修改密码可无视密码复杂度要求,但普通用户必须遵循
[root@localhost /]# passwd -S jen #查看密码状态 jen PS 2020-09-25 0 99999 7 -1 (Password set, SHA512 crypt.) 用户名 密码设定时间 修改间隔 密码有效期 警告时间 密码不失效 [root@localhost /]# passwd -l user1 #锁定用户 Locking password for user user1. passwd: Success /etc/shadow 密码前加入!! user1:!!$6$IAozruFF$q/NV8tXYtIq.Z9IzWAy8.cJaYAx4y/o/wc8KNLzgVMmtT1K0iX7OZaPScpeDZUKDzU1A/ZraSGU9efGz0V/cN1:18530:0:99999:7::: [root@localhost /]# passwd -u user1 #解锁用户 Unlocking password for user user1. passwd: Success user1:$6$IAozruFF$q/NV8tXYtIq.Z9IzWAy8.cJaYAx4y/o/wc8KNLzgVMmtT1K0iX7OZaPScpeDZUKDzU1A/ZraSGU9efGz0V/cN1:18530:0:99999:7::: shell编程批量添加密码 # echo "123"|passwd -stdin user1
-
修改用户信息usermod
[root@localhost /]# usermod --help Usage: usermod [options] LOGIN Options: -c, --comment COMMENT new value of the GECOS field -d, --home HOME_DIR new home directory for the user account -e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE -f, --inactive INACTIVE set password inactive after expiration to INACTIVE -g, --gid GROUP force use GROUP as new primary group -G, --groups GROUPS new list of supplementary GROUPS -a, --append append the user to the supplemental GROUPS mentioned by the -G option without removing him/her from other groups -h, --help display this help message and exit -l, --login NEW_LOGIN new value of the login name -L, --lock lock the user account -m, --move-home move contents of the home directory to the new location (use only with -d) -o, --non-unique allow using duplicate (non-unique) UID -p, --password PASSWORD use encrypted password for the new password -R, --root CHROOT_DIR directory to chroot into -s, --shell SHELL new login shell for the user account -u, --uid UID new UID for the user account -U, --unlock unlock the user account -Z, --selinux-user SEUSER new SELinux user mapping for the user account
# usermod -c "tede" user1 # usermod -G root user1 # usermod -L user1 # usermod -U user1
锁定用户:
- passwd文件shell 改为/sbin/nologin
- shadow密码前加! 换算失效
- 注释用户
-
修改密码信息chage
[root@localhost /]# chage -l jen #查看密码状态 Last password change : Sep 25, 2020 Password expires : never Password inactive : never Account expires : never Minimum number of days between password change : 0 Maximum number of days between password change : 99999 Number of days of warning before password expires : 7
# chage -d 0 user #要求用户一登录就修改密码
[root@localhost /]# chage --help Usage: chage [options] LOGIN Options: -d, --lastday LAST_DAY 最后更改日期 -E, --expiredate EXPIRE_DATE 失效时间 -h, --help display this help message and exit -I, --inactive INACTIVE 失效宽限天数 -l, --list show account aging information -m, --mindays MIN_DAYS 修改间隔 -M, --maxdays MAX_DAYS 密码有效期 -R, --root CHROOT_DIR directory to chroot into -W, --warndays WARN_DAYS 警告天数
-
删除用户userdel
[root@localhost /]# userdel -r user2 [root@localhost /]# ls /home jen tuser user1 [root@localhost /]# ls /var/spool/mail/ jen root rpc tuser user1 [root@localhost /]# grep user2 /etc/gshadow [root@localhost /]# grep user2 /etc/group [root@localhost /]# grep user2 /etc/shadow [root@localhost /]# grep user2 /etc/passwd
验证完全删除:再次添加不报错
-
查询id
[root@localhost /]# id jen uid=1000(jen) gid=1000(jen) groups=1000(jen),10(wheel) [root@localhost /]# id user1 uid=1001(user1) gid=1001(user1) groups=1001(user1) [root@localhost /]# usermod -G root user1 [root@localhost /]# id user1 uid=1001(user1) gid=1001(user1) groups=1001(user1),0(root)
-
切换用户su
# su - root - 连带env切换 # su - root -c "useradd user3" -c 不切换用户身份,执行一次命令
用户组管理命令
-
添加用户组groupadd
# groupadd tg jen:x:1000: user1:x:1001: [root@localhost /]# groupadd tg [root@localhost /]# cat /etc/group ... tcpdump:x:72: jen:x:1000: user1:x:1001: tg:x:1002: --------------------- Options: -g, --gid GID 修改gid -h, --help display this help message and exit -n, --new-name NEW_GROUP change the name to NEW_GROUP -o, --non-unique allow to use a duplicate (non-unique) GID -p, --password PASSWORD change the password to this (encrypted) PASSWORD -R, --root CHROOT_DIR directory to chroot into
-
groupmod
[root@localhost /]# groupmod -n lamp tg #修改,一般不用 [root@localhost /]# cat /etc/group root:x:0:user1 bin:x:1: ... lamp:x:1002:
-
groupdel
# groupdel 组名
组内有初始用户,不可删除,附加用户不影响
lamp:x:1002: [root@localhost /]# useradd tt -g lamp [root@localhost /]# useradd tt1 -G lamp [root@localhost /]# cat /etc/group root:x:0:user1 ... lamp:x:1002:tt1 tt1:x:1003: [root@localhost /]# groupdel lamp groupdel: cannot remove the primary group of user 'tt' [root@localhost /]# userdel tt [root@localhost /]# groupdel lamp
-
修改已存在用户用户组gpasswd
[root@localhost /]# gpasswd -a user1 lamp #user1加入附加组lamp Adding user user1 to group lamp [root@localhost /]# id user1 uid=1001(user1) gid=1001(user1) groups=1001(user1),0(root),1004(lamp) [root@localhost /]# gpasswd -d user1 lamp #从附加组中移除 Removing user user1 from group lamp [root@localhost /]# gpasswd -d user1 root Removing user user1 from group root [root@localhost /]# id user1 uid=1001(user1) gid=1001(user1) groups=1001(user1)