一世贪欢的私域

一世贪欢的私域

Linux用户管理

3
2025-10-06
Linux用户管理

Linux用户管理

越是不想开始,越是需求开始。前两周刚学过一遍,不过又变得模糊。

Linux是一个多用户多任务的操作系统,用户管理很有必要。

一、用户组

1.1 添加用户组

  1. 添加用户组
groupadd 组名
[root@centos7 ~]# groupadd IT
[root@centos7 ~]# tail -n1 /etc/group
IT:x:1002:
  1. 添加用户组时指定 gid
groupadd -g 指定的gid 组名
[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 删除用户组

  1. 删除用户组
groupdel 组名

作为用户主组的组,不可以被删除。除非使用 -f 参数强制删除。

# 创建两个测试组,用于测试删除
[root@centos7 ~]# groupadd test1
[root@centos7 ~]# groupadd test2
[root@centos7 ~]# tail -n2 /etc/group
test1:x:3001:
test2:x:3002:

# 证明作为用户主组的qiankong组存在
[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 ~]# 

# 删除 作为用户主组的qiankong组,删除失败
[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. 强制删除作为用户主组的组
# 证明作为用户主组的qiankong组存在
[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 ~]# 

# 删除后用户查到的主组为gid,是一个数字
[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、组名)

groupmod -g 指定的gid 组名
groupmod -n 新的组名 组名
# 修改组名
## 一次修改组名   IT ---> ITTI
[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 ~]# 

## 二次修改组名   ITTI ---> IT
[root@centos7 ~]# groupmod ITTI -n IT
[root@centos7 ~]# cat /etc/group |grep IT
IT:x:1002:
[root@centos7 ~]# 

# 修改组gid
# 一次修改组gid   3000-->4000
[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 ~]# 

## 二次修改组gid  4000-->3000
[root@centos7 ~]# groupmod OPS -g 3000
[root@centos7 ~]# cat /etc/group |grep OPS
OPS:x:3000:
[root@centos7 ~]# 

1.4 查找组信息查看所有的组

[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. 查看某个组下的组成员
getent group 组名
[root@centos7 ~]# getent group wheel
wheel:x:10:qiankong,yangge
[root@centos7 ~]# 

二、用户

2.1 添加用户

  1. 添加用户
useradd 用户名
# 添加用户 test001
[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 ~]# 
  1. 添加用户,指定 uid
useradd 用户名 -u 指定的uid
useradd -u 指定的uid 用户名
# 添加用户test002 ,指定uid为1005
[root@centos7 ~]# useradd test002 -u 1005
[root@centos7 ~]# id test002
uid=1005(test002) gid=1005(test002) groups=1005(test002)
[root@centos7 ~]# 

# 添加用户test003 ,指定uid为1010
[root@centos7 ~]# useradd -u 1010 test003
[root@centos7 ~]# id test003
uid=1010(test003) gid=1010(test003) groups=1010(test003)
[root@centos7 ~]# 
  1. 添加用户,指定家目录
useradd 用户名 -d 指定的家目录
useradd -d 指定的家目录 用户名
# 添加用户test004,指定家目录
[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 ~]# 

# 添加用户test005,指定家目录
[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. 添加用户,指定用户的主组。 指定组必须存在。
useradd 用户名 -g 组名
useradd -g 组名 用户名
# 添加用户test006,指定主组为test666
[root@centos7 ~]# useradd test006 -g test666
useradd: group 'test666' does not exist

# 添加用户test006,指定主组为IT
[root@centos7 ~]# useradd test006 -g IT
[root@centos7 ~]# id test006
uid=1013(test006) gid=1002(IT) groups=1002(IT)
[root@centos7 ~]# 

# 添加用户test007,指定主组为OPS
[root@centos7 ~]# useradd -g OPS test007
[root@centos7 ~]# id test007
uid=1014(test007) gid=3000(OPS) groups=3000(OPS)
[root@centos7 ~]# 
  1. 添加用户,指定用户的附加组
useradd 用户名 -G 组名
useradd -G 组名 用户名
# 添加用户,指定附加组
[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. 删除用户
userdel 用户名
[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 ~]# 

# 删除用户test002,发现家目录还在
[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. 删除用户,同时删除家目录、邮箱目录
userdel -r 用户名
[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 修改用户

  1. 修改用户的 uid
usermod -u 新的uid 用户
usermod 用户 -u 新的uid
[root@centos7 ~]# id test004
uid=1011(test004) gid=1011(test004) groups=1011(test004)
[root@centos7 ~]# 

# 1011 ---> 2011
[root@centos7 ~]# usermod -u 2011 test004
[root@centos7 ~]# id test004
uid=2011(test004) gid=1011(test004) groups=1011(test004)
[root@centos7 ~]# 

# 2011 ---> 3011
[root@centos7 ~]# usermod test004 -u 3011
[root@centos7 ~]# id test004
uid=3011(test004) gid=1011(test004) groups=1011(test004)
[root@centos7 ~]#
  1. 修改用户的主组
usermod -g 主组 用户名
usermod 用户名 -g 主组
[root@centos7 ~]# id test005
uid=1012(test005) gid=1012(test005) groups=1012(test005)
[root@centos7 ~]# 

# 修改主组    test005 ---> IT
[root@centos7 ~]# usermod -g IT test005
[root@centos7 ~]# id test005
uid=1012(test005) gid=1002(IT) groups=1002(IT)
[root@centos7 ~]# 

# 修改主组     IT  ---> OPS
[root@centos7 ~]# usermod test005 -g OPS
[root@centos7 ~]# id test005
uid=1012(test005) gid=3000(OPS) groups=3000(OPS)
[root@centos7 ~]# 
  1. 修改用户的附加组
usermod -G 附加组 用户名
usermod 用户名 -G 附加组
[root@centos7 ~]# id test006
uid=1013(test006) gid=1002(IT) groups=1002(IT)
[root@centos7 ~]# 

# 修改附加组   空 ---> OPS
[root@centos7 ~]# usermod -G OPS test006
[root@centos7 ~]# id test006
uid=1013(test006) gid=1002(IT) groups=1002(IT),3000(OPS)
[root@centos7 ~]# 

# 修改附加组   OPS ---> test006
[root@centos7 ~]# usermod test006 -G test005
[root@centos7 ~]# id test006
uid=1013(test006) gid=1002(IT) groups=1002(IT),1012(test005)
[root@centos7 ~]# 
  1. 给用户添加附加组
usermod -aG 添加的附加组 用户名
usermod 用户名 -G 添加的附加组
[root@centos7 ~]# id test007
uid=1014(test007) gid=3000(OPS) groups=3000(OPS)
[root@centos7 ~]# 

# 添加附加组,IT,test005
[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 ~]# 

# 添加附加组  test004
[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. 修改用户状态为锁定
usermod -L 用户名
usermod 用户名 -L
# 锁定前密码
[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 ~]# 
# 锁定前尝试登录
C:\Users\Polish>ssh [email protected]
[email protected]'s password:
Last login: Mon Oct  6 12:56:42 2025 from 192.168.10.1
[qiankong@centos7 ~]$

# 锁定后尝试登录
C:\Users\Polish>ssh [email protected]
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
[email protected]: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

C:\Users\Polish>
  1. 修改用户状态为解锁
usermod -U 用户名
usermod 用户名 -U
# 解锁前密码文件
[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 ~]# 
# 解锁前尝试登录
C:\Users\Polish>ssh [email protected]
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
[email protected]: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

# 解锁后尝试登录
C:\Users\Polish>ssh [email protected]
[email protected]'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. 修改用户的昵称
usermod -l 新用户昵称 用户名
usermod 用户名 -l 新用户昵称
# 修改前
[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. 修改用户的过期时间
usermod -e 时间 用户名
usermod -e "2025-11-11" 用户名

# 永不过期,数字 负一
usermod -e -1 用户名

# 天数,距离1970-1-1 的天数
usermod -e 偏移天数 用户名
# 修改前,小写L
[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 ~]# 

# 修改,    Never ---> 2025-11-11
[root@centos7 ~]# usermod -e "2025-11-11" qiankong

# 修改后,小写L
[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. 查所有用户
cat /etc/passwd
[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/sync
shutdown: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. 查某个用户
id 用户名
[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. 查用户的密码和有效期信息
chage -l 用户名
[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 组内添加用户

gpasswd -a 用户名 组名
gpasswd 组名 -a 用户名
# 添加前
[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 组内删除用户

gpasswd -d 用户名 组名
gpasswd 组名 -d 用户名
# 删除前
[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 ~]# 

四、封面图

封面图