系统中的用户管理
1. /etc/shadow 文件
用户认证信息文件
用户名:用户密码加密字符:用户密码最后一次被修改的时间:密码最短有效期:最长有效期:警告期:非活跃天数:账号到期时间:未使用
bash
# 在一个 shell 中使用监控
[root@node1 ~]# watch -n 1 tail -n 3 /etc/shadow
# 在另外一个 shell 中可以修改密码属性
[root@node1 ~]# echo mm | passwd --stdin mm
更改用户 mm 的密码 。
passwd:所有的身份验证令牌已经成功更新。
对于 watch 的补充:
-n, --interval 秒数指定刷新间隔,单位秒,支持小数(如 -n 0.5 半秒刷新)
-d, --differences高亮显示本次和上次输出不一样的地方
-t, --no-title关闭顶部标题栏(隐藏时间、间隔、执行命令)
-b, --beep命令返回非 0(异常)时蜂鸣报警
-e, --errexit命令出错时直接退出 watch
-g, --chgexit输出发生变化时自动退出 watch
2./etc/skel/*
用户骨架文件(用户自己的配置文件模板)
bash
# 监控
[root@node1 ~]# watch -n 1 ls -laR /home/
# ls -laR 以长格式展开全部目录并显示所有隐藏文件
# 在 shell2 中
[root@node1 ~]# userdel -r mm
[root@node1 ~]# touch /etc/skel/hahahaha
[root@node1 ~]# useradd mm
| ls的参数 | 全称 | 功能说明 |
|---|---|---|
-l |
--long |
长格式输出,展示权限、属主、大小、时间等完整信息 |
-a |
--all |
显示全部文件,包含 . 开头的隐藏文件(含 . 和 ..) |
-A |
--almost-all |
显示隐藏文件,但不展示 .、.. |
-h |
--human-readable |
配合 -l,文件大小自动转为 KB/MB/GB 易读格式 |
-d |
--directory |
仅展示目录本身信息,不列出目录内文件 |
-R |
--recursive |
递归遍历所有子目录,逐层列出内容 |
-t |
--sort=time |
按修改时间排序,最新文件排在最上方 |
-r |
--reverse |
反转排序结果(时间 / 大小 / 名称倒序) |
-S |
--sort=size |
按文件大小降序,大文件在前 |
-i |
--inode |
输出每个文件对应的 inode 编号 |
-F |
--classify |
文件末尾加标识:目录/、可执行*、软链接@ |
--color=auto |
- | 开启彩色区分文件类型(系统默认开启) |
3.权限委派
授权一个指定的普通用户能做超级用户才能做的事情
bash
[root@node1 ~]# su - mm
[mm@node1 ~]$ useradd haha
useradd: Permission denied.
useradd: 无法锁定 /etc/passwd,请稍后再试。
#授权
[root@node1 ~]# visudo
mm node1=(root) NOPASSWD: /usr/sbin/useradd, /usr/sbin/userdel
# 用户 主机=(可切换身份) 允许执行的命令(不用输密码)
#测试
[mm@node1 ~]$ sudo useradd haha
[mm@node1 ~]$ id haha
uid=1001(haha) gid=1001(haha) groups=1001(haha)
[mm@node1 ~]$ sudo userdel haha
[mm@node1 ~]$ id haha
id: "haha": 无此用户
Linux 操作系统权限
1. sticky bit(粘滞位)
bash
[root@node1 ~]# mkdir /pub
[root@node1 ~]# chmod 777 /pub/
[root@node1 ~]# watch -n 1 ls -ld /pub/ ; ls -l /mnt/
# ls -ld 以长格式显示目录自身
[root@node1 ~]# useradd mm
[root@node1 ~]# useradd timingmm
[root@node1 ~]# su - timingmm
[timingmm@node1 ~]$ touch /pub/timingmmfile
[timingmm@node1 ~]$ rm -fr /pub/timingmmfile
[timingmm@node1 ~]$ rm -fr /pub/mmfile
[timingmm@node1 ~]$
# 以上操作会出现用户可以在公共目录中删除不属于自己的文件
# 解决问题
[root@node1 ~]# chmod 1777 /pub/
# o+t
[root@node1 ~]# su - mm
[mm@node1 ~]$ touch /pub/mmfile
[root@node1 ~]# su - timingmm
[timingmm@node1 ~]$ touch /pub/timingmmfile
[timingmm@node1 ~]$ rm -fr /pub/timingmmfile
[timingmm@node1 ~]$ rm -fr /pub/mmfile
rm: 无法删除 '/pub/mmfile': 不允许的操作
2. sgid(强制位)
针对目录
当haha建立文件时文件的所有组是haha主组的,不是 /mnt/pub 目录所属组的,相当于在公司做的产品是自己家的,不是公司的
bash
#测试
监控信息:
/mnt:
total 0
drwxrwxrwt. 2 root root 18 May 26 20:21 pub
/mnt/pub:
total 0
-rw-r--r--. 1 hehe hehe 0 May 26 20:17 hehe
[root@localhost Desktop]# chmod g+s /mnt/pub/
# chmod 2775
更改后监控信息:
/mnt:
total 0
drwxrwsrwt. 2 root root 18 May 26 20:21 pub
/mnt/pub:
total 0
-rw-r--r--. 1 hehe hehe 0 May 26 20:17 hehe
建立文件观察:
[hehe@localhost ~]$ touch /mnt/pub/file
监控信息:
/mnt:
total 0
drwxrwsrwt. 2 root root 30 May 26 20:22 pub
/mnt/pub:
total 0
-rw-r--r--. 1 hehe root 0 May 26 20:22 file
-rw-r--r--. 1 hehe hehe 0 May 26 20:17 hehe
suid和sgid针对文件
bash
#监控
[root@node1 ~]# watch -n 1 "ps ax -o comm,user,group | grep cat;ls -l /bin/cat "
# 在 shell2 中
# 未设置前
[hehe@localhost ~]$ cat
#监控效果
hehe hehe cat # 进程是谁开启的那么进程就属于谁(工具是谁在用那么这个事情就是谁在做)
-rwxr-xr-x. 1 root root 36496 Dec 9 2024 /bin/cat
[hehe@localhost ~]$ cat
^C # 结束进程
[root@localhost Desktop]# chmod ug+s /bin/cat
# chmod 6775
# 监控效果:
-rwsr-sr-x. 1 root root 36496 Dec 9 2024 /bin/cat
#hehe从新执行cat
[hehe@localhost ~]$ cat
监控效果:
root root cat #是hehe在使用cat但是进行属于root(当hehe使用cat时就变成了root)
-rwsr-sr-x. 1 root root 36496 Dec 9 2024 /bin/cat