Linux用户管理
身份标识:UID 唯一标识
组标识:GID 唯一标识
=====用户介绍(user)=====
Linux里都有哪些用户和作用?
1) 超级用户
系统管理员,掌握系统最高权限,相当于皇帝。家目录/root
用户名是root,UID为0。
UID为0的用户就是root。
登录shell,/bin/bash
登录安全: 企业级应用。
a. 配置ssh,禁用root通过SSH远程登录,权限非常大,所有人都知道。使用普通用户登 录,然后切换到root。
b.平时使用普通用户登录,有必要的话在登录root。
2)普通用户
普通的权限,写的权限范围 家目录 /home/用户名,/tmp。相当于老百姓。
由管理员用户创建的。日常登录应该首先登录普通用户。
UID为C6 500-6000,C7 1000-60000
登录she11,/bin/bash
普通用户如何管理系统?
a.切换root,su – oldboy #角色改变,由普通用户变成了root。
b.不切换到root,可以使用root的权限去做事,sudo useradd oldboy
赋予普通用户一部分权限。
3) 虚拟用户(傀儡用户)
多数情况装系统就存在的,不能登录的。
登录shell,/sbin/nologin
存在还不能登录?
linux 文件、进程这样的东西如果要存在,必须要有对应的用户和组。
类似人出生,一定有父母。家庭 (组1),老男孩教育 (组2)
文件创建时就要有对应的用户和组。
进程启动时就要有对应的用户和组。
虚拟用户存在的目的就是满足进程启动时对用户和组的要求。
用普通用户行不行?行
最小化原则:
1.安装软件最小化。
2.登录安全最小化(普通用户)。
3.进程启动权限最小化。
4.权限最小化。
=====用户组介绍(group)=====
GID Group Identify
组名
用户的用户组 类似于[人]的家庭,学校等组织
1个用户可以在多个组里。
1个组可以有多个用户。
用户组怎么产生的?
1) 创建用户的时候默认产生的,创建一个oldboy,默认情况就会生成oldboy组,
用户和组同名,且UID和GID相同
2) 由root用户直接创建。
=====用户相关的配置文件=====
直接相关的有4个:
用户文件:
/etc/passwd ##用户主配置文件,用户的各种属性 (UID,GID,家目录,登录SHELL)。*****
/etc/shadow ##用户密码文件,存放密码及密码的属性 (失效时间,修改密码时间等)。
用户组文件:
/etc/group ##组文件,存放用户组及属性。
/etc/gshadow ##用户组的密码文件 (废弃)。
和创建用户相关的有3个:
/etc/default/useradd ##创建用户命今useradd的配置文件。
/etc/skel ##创建用户环境变量原始文件存放地。
/etc/login.defs ##创建用户系统配置,对应文件。
/etc/passwd:
[root@localhost opt]# tail -2 /etc/passwd
ntp:x:38:38::/etc/ntp:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
[root@localhost opt]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
/etc/shadow:
[root@localhost opt]# tail /etc/shadow
games:*:18353:0:99999:7:::
ftp:*:18353:0:99999:7:::
nobody:*:18353:0:99999:7:::
/etc/group:
[root@localhost opt]# 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:
cdrom:x:11:
mail:x:12:postfix
man:x:15:
/etc/gshadow :
[root@localhost opt]# cat /etc/gshadow
root:::
bin:::
daemon:::
sys:::
adm:::
tty:::
disk:::
lp:::
mem:::
kmem:::
wheel:::
cdrom:::
mail:::postfix
man:::
dialout:::
floppy:::
games:::
tape:::
video:::
ftp:::
lock:::
audio:::
nobody:::
users:::
utmp:!::
utempter:!::
input:!::
systemd-journal:!::
systemd-network:!::
=====和用户相关的命令=====
useradd 添加用户
usermod 修改用户
userdel 删除用户
1.useradd添加用户命令:
1)添加用户:
[root@localhost ~]# useradd test2
查看:
[root@localhost ~]# id test2
uid=1001(test2) gid=1001(test2) groups=1001(test2)
[root@localhost ~]# tail -n 1 /etc/passwd /etc/group /etc/shadow /etc/gshadow
==> /etc/passwd /etc/group /etc/shadow /etc/gshadow
2) 指定uid添加用户(-u):
[root@localhost ~]# useradd -u 5000 test3 #创建test3用户,并指定uid为5000
[root@localhost ~]# id test3
uid=5000(test3) gid=5000(test3) groups=5000(test3)
3) 指定用户的登录shell(-s):
[root@localhost ~]# useradd -s /sbin/nologin test4 #创建test4用户,禁止登录
[root@localhost ~]# id test4
uid=5001(test4) gid=5001(test4) groups=5001(test4)
[root@localhost ~]# tail -1 /etc/passwd
test4:x:5001:5001::/home/test4:/sbin/nologin
[root@localhost ~]# su - test4
This account is currently not available.
企业应用:配置网络服务时候,编译安装需要创建虚拟用户。
4) 指定用户家目录(-d):
[root@localhost ~]# useradd -d /opt test5
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
[root@localhost ~]# tail -1 /etc/passwd
test5:x:5002:5002::/opt:/bin/bash
su – test4
正常报错:
-bash-4.1$ ##缺少用户环境变量所致。
5) 添加用户指定属于其他用户组(-g):
[root@localhost ~]# useradd test6 -g root #添加用户属于root组。
[root@localhost ~]# id test6
uid=5003(test6) gid=0(root) groups=0(root)
6) 添加用户设置过期时间(-e):
企业应用: 给非运维人员设置用户登录的固定权限,到期了自动无法登录。
[root@localhost ~]# date
Tue May 30 07:47:50 CST 2023
[root@localhost ~]# useradd -e "2030/05/30" test7
[root@localhost ~]# passwd test7
Changing password for user test7.
New password:
BAD PASSWORD: The password fails the dictionary check - it is too simplistic/systematic
Retype new password:
Sorry, passwords do not match.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[root@localhost ~]# date -s "2030/06/01"
Sat Jun 1 00:00:00 CST 2030
[root@localhost ~]# ssh test7@127.0.0.1
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:26VYvDFPCA1/zST0jEXnLNBMMDotyuwnDOtYMbY/qZk.
ECDSA key fingerprint is MD5:18:95:91:9a:9a:9a:6e:52:c1:4f:63:55:66:97:2a:3f.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': y
Please type 'yes' or 'no': yes
Warning: Permanently added '127.0.0.1' (ECDSA) to the list of known hosts.
test7@127.0.0.1's password:
Your account has expired; please contact your system administrator
Authentication failed.
7) 添加用户不创建家目录(-M):
配合创建虚拟用户
[root@localhost ~]# useradd -s /sbin/nologin test3 -M
总结:
-u 指定UID
-g 指定属于的组
-e 指定过期时间
-d 指定家目录
-M 不能创建家目录
-s 指定登录的解释器
2. usermod修改用户命令:
-u 指定UID
-g 指定属于的组
-e 指定过期时间
-s 指定登录的解释器
1) 修改test3的UID:
[root@localhost ~]# id test3
uid=50001(test3) gid=5000(test3) groups=5000(test3)
[root@localhost ~]# usermod -u 500001 test3
[root@localhost ~]# id test3
uid=500001(test3) gid=5000(test3) groups=5000(test3)
UID已改,组ID没改。
2) 修改test4属于的新组root:
[root@localhost ~]# usermod -g root test4
[root@localhost ~]# id test4
uid=5001(test4) gid=0(root) groups=0(root)
3) 修改过期时间为:…….
[root@localhost ~]# id test7
uid=5004(test7) gid=5004(test7) groups=5004(test7)
[root@localhost ~]# chage -l test7
Last password change : May 29, 2023
Password expires : never
Password inactive : never
Account expires : May 30, 2030
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@localhost ~]# usermod -e "2031/7/18" test7
[root@localhost ~]# chage -l test7
Last password change : May 29, 2023
Password expires : never
Password inactive : never
Account expires : Jul 18, 2031
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
4) 更改test解释器为/bin/bash(-s):
[root@localhost ~]# grep test4 /etc/passwd
test4:x:5001:0::/home/test4:【/sbin/nologin】
[root@localhost ~]# usermod -s /bin/bash test4
[root@localhost ~]# grep test4 /etc/passwd
test4:x:5001:0::/home/test4:【/bin/bash】
3.userdel删除用户命令:
-r 删除家目录,是有风险的。
[root@localhost ~]# userdel -r test7
[root@localhost ~]# ls -l /home/
total 0
drwx------ 2 1000 1000 62 May 30 2023 test1
drwx------ 2 test2 test2 62 May 30 2023 test2
drwx------ 2 test3 test3 62 May 30 2023 test3
drwx------ 2 test4 root 62 May 30 2023 test4
drwx------ 2 test6 root 62 May 30 2023 test6
企业应用:人员离职,他的用户怎么管?
a.uesrdel -r 离职人员 #容易把离职人员数据删除,是不明智的。
b.userdel 离职人员用户名 #家目录保留
c.设置过期时间,/etc/passwd注释掉。
编辑:注释掉test6,生产场景推荐。
[root@localhost ~]# vim /etc/passwd
[root@localhost ~]# su - test6
[test6@localhost ~]$
ctrl+d返回到
[root@localhost ~]
4.useradd命令3个配置文件:
和创建用户相关的有3个:
/etc/default/useradd ##创建用户命今useradd的配置文件。
/etc/skel ##创建用户环境变量原始文件存放地。
/etc/login.defs ##创建用户系统配置,对应文件。
======/etc/skel用户环境变量原始文件存放地=====
每个用户家目录默认都会有.bash_logout .bash_profile .bashrc,
创建用户的同时,从/etc/skel里复制到/home/用户名下的。
[root@localhost ~]# useradd test8
[root@localhost ~]# su - test8
[test8@localhost ~]$ pwd
/home/test8
[test8@localhost ~]$ ls -al
total 12
drwx------ 2 test8 test8 62 Jun 1 01:49 .
drwxr-xr-x. 8 root root 84 Jun 1 01:49 ..
-rw-r--r-- 1 test8 test8 18 Apr 1 2020 .bash_logout
-rw-r--r-- 1 test8 test8 193 Apr 1 2020 .bash_profile
-rw-r--r-- 1 test8 test8 231 Apr 1 2020 .bashrc
创建用户的时候为什么会从/etc/skel下面拷贝环境变量。
/etc/default/useradd #创建用户命令useradd的配置文件。
useradd相当于你,/etc/default/useradd 相当于对象。
备份:
[root@localhost ~]# cp /etc/default/useradd {,.ori}
[root@localhost ~]# cp /etc/default/useradd /etc/default/useradd.ori
[root@localhost ~]# vim /etc/default/useradd
[root@localhost ~]# cat /etc/default/useradd
# useradd defaults file
GROUP=100
HOME=/opt #把默认的/home改为/opt
INACTIVE=-1
EXPIRE="2031/12/31"
SHELL=/sbin/nologin #把默认的/bin/bash改为/sbin/nologin
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
[root@localhost ~]# useradd test9
[root@localhost ~]# tail -l /etc/passwd
ntp:x:38:38::/etc/ntp:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
test2:x:1001:1001::/home/test2:/bin/bash
test1:x:10001:1002::/home/test1:/bin/bash
test3:x:500001:5000::/home/test3:/bin/bash
test4:x:5001:0::/home/test4:/bin/bash
test5:x:5002:5002::/opt:/bin/bash
test6:x:5003:0::/home/test6:/bin/bash
test8:x:10002:10002::/home/test8:/bin/bash
test9:x:10003:10003::/opt/test9:/sbin/nologin
[root@localhost ~]# chage -l test9
Last password change : May 31, 2030
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
服务器租用托管,机房租用托管,主机租用托管,https://www.e1idc.com