Linux用户管理
越是不想开始,越是需求开始。前两周刚学过一遍,不过又变得模糊。
Linux是一个多用户多任务的操作系统,用户管理很有必要。
一、用户组 1.1 添加用户组
添加用户组
1 2 3 [root@centos7 ~]# groupadd IT [root@centos7 ~]# tail -n1 /etc/group IT:x:1002:
添加用户组时指定 gid
1 2 3 4 5 6 [root@centos7 ~]# groupadd OPS -g 3000 [root@centos7 ~]# groupadd -g 2000 HR [root@centos7 ~]# tail -n2 /etc/group OPS:x:3000: HR:x:2000: [root@centos7 ~]#
1.2 删除用户组
删除用户组
作为用户主组的组,不可以被删除。除非使用 -f 参数强制删除。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 [root@centos7 ~]# groupadd test1 [root@centos7 ~]# groupadd test2 [root@centos7 ~]# tail -n2 /etc/group test1:x:3001: test2:x:3002: [root@centos7 ~]# cat /etc/group |grep qiankong wheel:x:10:qiankong,yangge qiankong:x:1000:qiankong [root@centos7 ~]# [root@centos7 ~]# groupdel test1 [root@centos7 ~]# groupdel test2 [root@centos7 ~]# cat /etc/group |grep test [root@centos7 ~]# [root@centos7 ~]# groupdel qiankong groupdel: cannot remove the primary group of user 'qiankong' [root@centos7 ~]# cat /etc/group |grep qiankong wheel:x:10:qiankong,yangge qiankong:x:1000:qiankong [root@centos7 ~]#
强制删除作为用户主组的组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 [root@centos7 ~]# cat /etc/group |grep qiankong wheel:x:10:qiankong,yangge qiankong:x:1000:qiankong [root@centos7 ~]# groupdel -f qiankong [root@centos7 ~]# cat /etc/group |grep qiankong wheel:x:10:qiankong,yangge [root@centos7 ~]# [qiankong@centos7 ~]$ id uid=1000(qiankong) gid=1000 groups =1000,10(wheel) [qiankong@centos7 ~]$ [root@centos7 ~]# groupadd qiankong -g 1000 [root@centos7 ~]# [qiankong@centos7 ~]$ id uid=1000(qiankong) gid=1000(qiankong) groups =1000(qiankong),10(wheel) [qiankong@centos7 ~]$
1.3 修改用户组(gid、组名) 1 2 groupmod -g 指定的gid 组名 groupmod -n 新的组名 组名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 [root@centos7 ~]# cat /etc/group |grep IT IT:x:1002: [root@centos7 ~]# groupmod -n ITTI IT [root@centos7 ~]# cat /etc/group |grep IT ITTI:x:1002: [root@centos7 ~]# [root@centos7 ~]# groupmod ITTI -n IT [root@centos7 ~]# cat /etc/group |grep IT IT:x:1002: [root@centos7 ~]# [root@centos7 ~]# cat /etc/group |grep OPS OPS:x:3000: [root@centos7 ~]# groupmod -g 4000 OPS [root@centos7 ~]# cat /etc/group |grep OPS OPS:x:4000: [root@centos7 ~]# [root@centos7 ~]# groupmod OPS -g 3000 [root@centos7 ~]# cat /etc/group |grep OPS OPS:x:3000: [root@centos7 ~]#
1.4 查找组信息查看所有的组 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 [root@centos7 ~]# cat /etc/group root:x:0: bin:x:1: daemon:x:2: sys:x:3: adm:x:4: tty :x:5:disk:x:6: lp:x:7: mem:x:8: kmem:x:9: wheel:x:10:qiankong,yangge cdrom:x:11: mail:x:12:postfix man:x:15: dialout:x:18: floppy:x:19: games:x:20: tape:x:33: video:x:39: ftp:x:50: lock:x:54: audio:x:63: nobody:x:99: users :x:100:utmp:x:22: utempter:x:35: input:x:999: systemd-journal:x:190: systemd-network:x:192: dbus:x:81: polkitd:x:998: ssh_keys:x:997: sshd:x:74: postdrop:x:90: postfix:x:89: yangge:x:1001: rpc:x:32: rpcuser:x:29: nfsnobody:x:65534: apache:x:48: slocate:x:21: OPS:x:3000: HR:x:2000: qiankong:x:1000: IT:x:1002: [root@centos7 ~]#
查看某个组下的组成员
1 2 3 [root@centos7 ~]# getent group wheel wheel:x:10:qiankong,yangge [root@centos7 ~]#
二、用户 2.1 添加用户
添加用户
1 2 3 4 5 6 7 [root@centos7 ~]# useradd test001 [root@centos7 ~]# id test001 uid=1002(test001) gid=3001(test001) groups =3001(test001) [root@centos7 ~]# ls -la /home/ |grep test drwx------ 2 test001 test001 62 Oct 6 13:26 test001 [root@centos7 ~]#
添加用户,指定 uid
1 2 useradd 用户名 -u 指定的uid useradd -u 指定的uid 用户名
1 2 3 4 5 6 7 8 9 10 11 [root@centos7 ~]# useradd test002 -u 1005 [root@centos7 ~]# id test002 uid=1005(test002) gid=1005(test002) groups =1005(test002) [root@centos7 ~]# [root@centos7 ~]# useradd -u 1010 test003 [root@centos7 ~]# id test003 uid=1010(test003) gid=1010(test003) groups =1010(test003) [root@centos7 ~]#
添加用户,指定家目录
1 2 useradd 用户名 -d 指定的家目录 useradd -d 指定的家目录 用户名
1 2 3 4 5 6 7 8 9 10 11 [root@centos7 ~]# useradd test004 -d /opt/test004 [root@centos7 ~]# ls -ld /opt/test004/ drwx------ 2 test004 test004 62 Oct 6 13:33 /opt/test004/ [root@centos7 ~]# [root@centos7 ~]# useradd -d /opt/test005 test005 [root@centos7 ~]# ls -ld /opt/test005 drwx------ 2 test005 test005 62 Oct 6 13:33 /opt/test005 [root@centos7 ~]#
添加用户,指定用户的主组。 指定组必须存在。
1 2 useradd 用户名 -g 组名 useradd -g 组名 用户名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [root@centos7 ~]# useradd test006 -g test666 useradd: group 'test666' does not exist [root@centos7 ~]# useradd test006 -g IT [root@centos7 ~]# id test006 uid=1013(test006) gid=1002(IT) groups =1002(IT) [root@centos7 ~]# [root@centos7 ~]# useradd -g OPS test007 [root@centos7 ~]# id test007 uid=1014(test007) gid=3000(OPS) groups =3000(OPS) [root@centos7 ~]#
添加用户,指定用户的附加组
1 2 useradd 用户名 -G 组名 useradd -G 组名 用户名
1 2 3 4 5 6 7 8 9 10 11 [root@centos7 ~]# useradd test008 -G IT [root@centos7 ~]# id test008 uid=1015(test008) gid=1015(test008) groups =1015(test008),1002(IT) [root@centos7 ~]# [root@centos7 ~]# useradd -G OPS test009 [root@centos7 ~]# id test009 uid=1016(test009) gid=1016(test009) groups =1016(test009),3000(OPS) [root@centos7 ~]#
2.2 删除用户
删除用户
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [root@centos7 ~]# cat /etc/passwd |grep test test002:x:1005:1005::/home/test002:/bin/bash test003:x:1010:1010::/home/test003:/bin/bash test004:x:1011:1011::/opt/test004:/bin/bash test005:x:1012:1012::/opt/test005:/bin/bash test006:x:1013:1002::/home/test006:/bin/bash test007:x:1014:3000::/home/test007:/bin/bash test008:x:1015:1015::/home/test008:/bin/bash test009:x:1016:1016::/home/test009:/bin/bash [root@centos7 ~]# [root@centos7 ~]# userdel test002 [root@centos7 ~]# id test002 id : test002: no such user[root@centos7 ~]# ls -ld /home/test002 drwx------ 2 1005 1005 62 Oct 6 13:28 /home/test002 [root@centos7 ~]#
删除用户,同时删除家目录、邮箱目录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 [root@centos7 ~]# cat /etc/passwd |grep test test003:x:1010:1010::/home/test003:/bin/bash test004:x:1011:1011::/opt/test004:/bin/bash test005:x:1012:1012::/opt/test005:/bin/bash test006:x:1013:1002::/home/test006:/bin/bash test007:x:1014:3000::/home/test007:/bin/bash test008:x:1015:1015::/home/test008:/bin/bash test009:x:1016:1016::/home/test009:/bin/bash [root@centos7 ~]# [root@centos7 ~]# userdel -r test003 [root@centos7 ~]# ls -ld /home/test003 ls : cannot access /home/test003: No such file or directory[root@centos7 ~]# id test003 id : test003: no such user[root@centos7 ~]#
2.3 修改用户
修改用户的 uid
1 2 usermod -u 新的uid 用户 usermod 用户 -u 新的uid
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [root@centos7 ~]# id test004 uid=1011(test004) gid=1011(test004) groups =1011(test004) [root@centos7 ~]# [root@centos7 ~]# usermod -u 2011 test004 [root@centos7 ~]# id test004 uid=2011(test004) gid=1011(test004) groups =1011(test004) [root@centos7 ~]# [root@centos7 ~]# usermod test004 -u 3011 [root@centos7 ~]# id test004 uid=3011(test004) gid=1011(test004) groups =1011(test004) [root@centos7 ~]#
修改用户的主组
1 2 usermod -g 主组 用户名 usermod 用户名 -g 主组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [root@centos7 ~]# id test005 uid=1012(test005) gid=1012(test005) groups =1012(test005) [root@centos7 ~]# [root@centos7 ~]# usermod -g IT test005 [root@centos7 ~]# id test005 uid=1012(test005) gid=1002(IT) groups =1002(IT) [root@centos7 ~]# [root@centos7 ~]# usermod test005 -g OPS [root@centos7 ~]# id test005 uid=1012(test005) gid=3000(OPS) groups =3000(OPS) [root@centos7 ~]#
修改用户的附加组
1 2 usermod -G 附加组 用户名 usermod 用户名 -G 附加组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [root@centos7 ~]# id test006 uid=1013(test006) gid=1002(IT) groups =1002(IT) [root@centos7 ~]# [root@centos7 ~]# usermod -G OPS test006 [root@centos7 ~]# id test006 uid=1013(test006) gid=1002(IT) groups =1002(IT),3000(OPS) [root@centos7 ~]# [root@centos7 ~]# usermod test006 -G test005 [root@centos7 ~]# id test006 uid=1013(test006) gid=1002(IT) groups =1002(IT),1012(test005) [root@centos7 ~]#
给用户添加附加组
1 2 usermod -aG 添加的附加组 用户名 usermod 用户名 -G 添加的附加组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [root@centos7 ~]# id test007 uid=1014(test007) gid=3000(OPS) groups =3000(OPS) [root@centos7 ~]# [root@centos7 ~]# usermod -aG IT,test005 test007 [root@centos7 ~]# id test007 uid=1014(test007) gid=3000(OPS) groups =3000(OPS),1002(IT),1012(test005) [root@centos7 ~]# [root@centos7 ~]# [root@centos7 ~]# usermod test007 -aG test004 [root@centos7 ~]# id test007 uid=1014(test007) gid=3000(OPS) groups =3000(OPS),1002(IT),1011(test004),1012(test005) [root@centos7 ~]#
修改用户状态为锁定
1 2 usermod -L 用户名 usermod 用户名 -L
1 2 3 4 5 6 7 8 9 10 11 12 [root@centos7 ~]# cat /etc/shadow |grep qiankong qiankong:$6$.m3a2W7J$m57U /dwjZiKX2Xm/IejNIEh9MfhtnJ1wRgZf11vxeXYuGaLkXCN3Qhu9L2gvD0INHxQUhl65yZ.cKlr7yrTFa.:20367:0:99999:7::: [root@centos7 ~]# [root@centos7 ~]# usermod -L qiankong [root@centos7 ~]# cat /etc/shadow |grep qiankong qiankong:!$6$.m3a2W7J$m57U /dwjZiKX2Xm/IejNIEh9MfhtnJ1wRgZf11vxeXYuGaLkXCN3Qhu9L2gvD0INHxQUhl65yZ.cKlr7yrTFa.:20367:0:99999:7::: [root@centos7 ~]#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 # 锁定前尝试登录 C:\Users \Polish >ssh qiankong @192.168.10.10 qiankong @192.168.10.10's password :Last login : Mon Oct 6 12:56:42 2025 from 192.168.10.1[qiankong @centos7 ~]$ # 锁定后尝试登录 C :\Users \Polish >ssh qiankong @192.168.10.10qiankong @192.168.10.10's password :Permission denied , please try again .qiankong @192.168.10.10's password :Permission denied , please try again .qiankong @192.168.10.10's password :qiankong @192.168.10.10: Permission denied (publickey ,gssapi -keyex ,gssapi -with -mic ,password ).C :\Users \Polish >
修改用户状态为解锁
1 2 usermod -U 用户名 usermod 用户名 -U
1 2 3 4 5 6 7 8 9 10 11 [root@centos7 ~]# cat /etc/shadow |grep qiankong qiankong:!$6$.m3a2W7J$m57U /dwjZiKX2Xm/IejNIEh9MfhtnJ1wRgZf11vxeXYuGaLkXCN3Qhu9L2gvD0INHxQUhl65yZ.cKlr7yrTFa.:20367:0:99999:7::: [root@centos7 ~]# usermod -U qiankong [root@centos7 ~]# cat /etc/shadow |grep qiankong qiankong:$6$.m3a2W7J$m57U /dwjZiKX2Xm/IejNIEh9MfhtnJ1wRgZf11vxeXYuGaLkXCN3Qhu9L2gvD0INHxQUhl65yZ.cKlr7yrTFa.:20367:0:99999:7::: [root@centos7 ~]#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 # 解锁前尝试登录 C:\Users \Polish >ssh qiankong @192.168.10.10 qiankong @192.168.10.10's password :Permission denied , please try again .qiankong @192.168.10.10's password :Permission denied , please try again .qiankong @192.168.10.10's password :qiankong @192.168.10.10: Permission denied (publickey ,gssapi -keyex ,gssapi -with -mic ,password ).# 解锁后尝试登录 C :\Users \Polish >ssh qiankong @192.168.10.10qiankong @192.168.10.10's password :Last failed login : Mon Oct 6 14:21:44 CST 2025 from 192.168.10.1 on ssh:notty There were 3 failed login attempts since the last successful login .Last login : Mon Oct 6 14:20:02 2025 from 192.168.10.1[qiankong @centos7 ~]$
修改用户的昵称
1 2 usermod -l 新用户昵称 用户名 usermod 用户名 -l 新用户昵称
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 [root@centos7 ~]# id qiankong uid=1000(qiankong) gid=1000(qiankong) groups =1000(qiankong),10(wheel) [root@centos7 ~]# id qiankong666 id : qiankong666: no such user[root@centos7 ~]# [root@centos7 ~]# usermod qiankong -l qiankong666 usermod: user qiankong is currently used by process 27443 [root@centos7 ~]# usermod qiankong -l qiankong666 [root@centos7 ~]# [root@centos7 ~]# id qiankong id : qiankong: no such user[root@centos7 ~]# id qiankong666 uid=1000(qiankong666) gid=1000(qiankong) groups =1000(qiankong),10(wheel) [root@centos7 ~]#
修改用户的过期时间
1 2 3 4 5 6 7 usermod -e "2025-11-11" 用户名 usermod -e -1 用户名 usermod -e 偏移天数 用户名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 [root@centos7 ~]# chage -l qiankong Last password change : Oct 06, 2025 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 [root@centos7 ~]# [root@centos7 ~]# usermod -e "2025-11-11" qiankong [root@centos7 ~]# chage -l qiankong Last password change : Oct 06, 2025 Password expires : never Password inactive : never Account expires : Nov 11, 2025 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 [root@centos7 ~]#
2.4 查用户信息
查所有用户
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 [root@centos7 ~]# cat /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync :x:5:0:sync :/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:998:User for polkitd:/:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin yangge:x:1001:1001::/home/yangge:/bin/bash rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin test004:x:3011:1011::/opt/test004:/bin/bash test005:x:1012:3000::/opt/test005:/bin/bash test006:x:1013:1002::/home/test006:/bin/bash test007:x:1014:3000::/home/test007:/bin/bash test008:x:1015:1015::/home/test008:/bin/bash test009:x:1016:1016::/home/test009:/bin/bash qiankong:x:1000:1000:qiankong:/home/qiankong:/bin/bash [root@centos7 ~]#
查某个用户
1 2 3 4 5 6 7 8 [root@centos7 ~]# id uid=0(root) gid=0(root) groups =0(root) [root@centos7 ~]# [root@centos7 ~]# id qiankong uid=1000(qiankong) gid=1000(qiankong) groups =1000(qiankong),10(wheel) [root@centos7 ~]#
查用户的密码和有效期信息
1 2 3 4 5 6 7 8 9 [root@centos7 ~]# chage -l qiankong Last password change : Oct 06, 2025 Password expires : never Password inactive : never Account expires : Nov 07, 2423 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 [root@centos7 ~]#
三、用户组内操作 3.1 组内添加用户 1 2 gpasswd -a 用户名 组名 gpasswd 组名 -a 用户名
1 2 3 4 5 6 7 8 9 10 11 12 13 [root@centos7 ~]# getent group IT IT:x:1002:test008,test007 [root@centos7 ~]# [root@centos7 ~]# gpasswd -a qiankong IT Adding user qiankong to group IT [root@centos7 ~]# getent group IT IT:x:1002:test008,test007,qiankong [root@centos7 ~]#
3.2 组内删除用户 1 2 gpasswd -d 用户名 组名 gpasswd 组名 -d 用户名
1 2 3 4 5 6 7 8 9 10 11 12 13 [root@centos7 ~]# getent group IT IT:x:1002:test008,test007,qiankong,test009 [root@centos7 ~]# [root@centos7 ~]# gpasswd -d test009 IT Removing user test009 from group IT [root@centos7 ~]# getent group IT IT:x:1002:test008,test007,qiankong [root@centos7 ~]#
四、封面图