1.文件系统权限介绍
1.1 文件系统权限介绍
Linux文件权限简单灵活,易于理解和应用,能够处理大多数权限使用情况。
文件有三个适用权限的用户类别:
- 单个用户拥有者,通常是创建该文件的用户。
- 单个组拥有者,通常是创建该文件的用户的主要组。
- 除了用户拥有者和组拥有者之外的其他用户。
[fengkai@centos7 ~ 21:08:01]$ ls -l
drwxr-xr-x. 2 fengkai fengkai 6 7月 14 10:39 公共
drwxr-xr-x. 2 fengkai fengkai 6 7月 14 10:39 模板
drwxr-xr-x. 2 fengkai fengkai 6 7月 14 10:39 视频
drwxr-xr-x. 2 fengkai fengkai 6 7月 14 10:39 图片
drwxr-xr-x. 2 fengkai fengkai 6 7月 14 10:39 文档
drwxr-xr-x. 2 fengkai fengkai 6 7月 14 10:39 下载
drwxr-xr-x. 2 fengkai fengkai 6 7月 14 10:39 音乐
drwxr-xr-x. 2 fengkai fengkai 6 7月 15 17:28 桌面
[fengkai@centos7 ~ 21:08:22]$ ls -l /etc/passwd
-rw-r–r–. 1 root root 2560 7月 20 20:51 /etc/passwd
对于/etc/passwd文件来说,-rw-r–r–字符串分成四份,格式如下:
- 第一位代表文件类型,例如,-代表普通文件,d代表目录,l(L的小写)代表软链接等。
- 第二到第四位,代表user-owner具有的权限,也就是laoma用户具有的权限。
- 第五到第七位,代表group-owner具有的权限,也就是laoma组中成员具有的权限。
- 第八到第十位,代表user-owner和group-owner之外的用户具有的权限。

1.2 权限优先级
如果文件的用户是laoma,组成员中也有laoma用户,那么laoma获得最终权限是laoma用户拥有者的权限,而不是组中成员具有的权限。

1.3 rwx 权限解读
目录中保存文件,文件中保存数据(例如字符串)。

1.4 综合测试

哪个常规文件归 operator1 所有并可被所有用户读取? a. lfile1 b. lfile2 c. rfile1 d. rfile2
哪个文件可以被 contractor1 用户修改? a. lfile1 b. lfile2 c. rfile1 d. rfile2
哪个文件无法被 operator2 用户读取? a. lfile1 b. lfile2 c. rfile1 d. rfile2
哪个文件的组所有者为 consultant1? a. lfile1 b. lfile2 c. rfile1 d. rfile2
哪些文件可以被 operator1 用户删除? a. rfile1 b. rfile2 c. 以上都对。 d. 以上都不对。
哪些文件可以被 operator2 用户删除? a. lfile1 b. lfile2 c. 以上都对。 d. 以上都不对。
C B D A C D
2.文件系统权限管理
2.1 chmod 命令
作用:更改文件不同owner权限。
2.1.1 语法1
语法1:chmod WhoHowWhat /path/to/file
- Who: u(user) g(group) o(other) a(all)
- How: +(添加) -(减去) =(精确设置)
- What: r(read) w(write) x(excute) -(不具有权限)
2.1.1.1针对文件
[root@centos7 ~ 08:32:29]# mkdir /lab
[root@centos7 ~ 08:37:06]# cd /lab/
[root@centos7 lab 08:37:41]# cp /etc/passwd .
[root@centos7 lab 08:37:51]# ls -l passwd
-rw-r–r–. 1 root root 2357 7月 21 08:37 passwd
# 给user增加x权限
[root@centos7 lab 08:39:39]# chmod u+x ./passwd
[root@centos7 lab 08:39:43]# ls -l passwd
-rwxr–r–. 1 root root 2357 7月 21 08:37 passwd
# 一次性设置多个
[root@centos7 lab 08:39:46]# chmod u-wx,g+w,o=- passwd
[root@centos7 lab 08:41:01]# ls -l passwd
-r–rw—-. 1 root root 2357 7月 21 08:37 passwd
# 一次性设置所有对象
[root@centos7 lab 08:41:05]# chmod a=rwx passwd
[root@centos7 lab 08:43:14]# ls -l passwd
-rwxrwxrwx. 1 root root 2357 7月 21 08:37 passwd
[root@centos7 lab 08:43:48]# chmod a-wx passwd
[root@centos7 lab 08:43:54]# ls -l passwd
-r–r–r–. 1 root root 2357 7月 21 08:37 passwd
2.1.1.2针对目录
# 针对目录,准备目录和文件
[root@centos7 lab 08:43:55]# mkdir dir01
[root@centos7 lab 08:44:36]# touch dir01/file01
[root@centos7 lab 08:45:12]# ls -ld dir01 dir01/file01
drwxr-xr-x. 2 root root 20 7月 21 08:44 dir01
-rw-r–r–. 1 root root 0 7月 21 08:44 dir01/file01
# 递归清除所有对象所有权限
[root@centos7 lab 08:45:22]# chmod -R a=- dir01
[root@centos7 lab 08:46:33]# ls -ld dir01 dir01/*
d———. 2 root root 20 7月 21 08:44 dir01
———-. 1 root root 0 7月 21 08:44 dir01/file01
# 递归设置user对象权限为rwx
[root@centos7 lab 08:46:40]# chmod -R u+rwx dir01/
[root@centos7 lab 08:47:28]# ls -ld dir01 dir01/*
drwx——. 2 root root 20 7月 21 08:44 dir01
-rwx——. 1 root root 0 7月 21 08:44 dir01/file01
2.1.2 语法2
语法2: chmod ### /path/to/file
- 第1个#,代表user权限
- 第2个#,代表group权限
- 第3个#,代表other权限
#,是一个数字范围是0(—)到7(rwx)。
补充:二进制与十进制转换
二进制 十进制 对应权限
000 0 — 无 001 1 –x 执行 010 2 -w- 写 011 3 -wx 写和执行 100 4 r– 读 101 5 r-x 读和执行 110 6 rw- 读和写 111 7 rwx 读、写、执行
# 示例1,文件权限为 -rw- r– r–
# 用二进制表达权限为 110 100 100,对应十进制为644
# 示例2,文件权限为 -rwx rw- r-x
# 用二进制表达权限为 111 110 101,对应十进制为765
# 文件权限为634对应的权限
[root@centos7 ~ 08:51:57]# mkdir /lab;cd /lab
[root@centos7 lab 08:52:02]# cp /etc/passwd .
[root@centos7 lab 08:52:15]# chmod 634 passwd
[root@centos7 lab 08:52:35]# ls -l passwd
-rw–wxr–. 1 root root 2357 7月 21 08:52 passwd
[root@centos7 lab 08:52:39]# stat -c %A passwd
-rw–wxr–
[root@centos7 lab 08:54:04]# stat -c %a passwd
634
# 文件权限为755对应的权限
[root@centos7 lab 08:54:13]# chmod 755 passwd
[root@centos7 lab 08:54:41]# ls -l passwd
-rwxr-xr-x. 1 root root 2357 7月 21 08:52 passwd
2.3 chown chgrp 命令
作用:更改文件属主。
[root@centos7 lab 08:54:45]# ls -l passwd
-rwxr-xr-x. 1 root root 2357 7月 21 08:52 passwd
# 修改user owner
[root@centos7 lab 08:57:16]# chown fengkai passwd
[root@centos7 lab 08:58:08]# ls -l passwd
-rwxr-xr-x. 1 fengkai root 2357 7月 21 08:52 passwd
# 修改 group owner
[root@centos7 lab 08:58:09]# chgrp wheel passwd
[root@centos7 lab 08:59:08]# ls -l passwd
-rwxr-xr-x. 1 fengkai wheel 2357 7月 21 08:52 passwd
# 同时修改 user和group owner
[root@centos7 lab 08:59:10]# chown laowang:root passwd
[root@centos7 lab 09:00:07]# ls -l passwd
-rwxr-xr-x. 1 laowang root 2357 7月 21 08:52 passwd
# 对目录递归修改
[root@centos7 lab 09:59:23]# ls -R -ld dir01/ dir01/*
drwxr-xr-x. 2 root root 20 7月 21 09:58 dir01/
-rw-r–r–. 1 root root 0 7月 21 09:58 dir01/file01
[root@centos7 lab 10:47:09]# chmod -R 770 dir01/ dir01/*
[root@centos7 lab 10:54:22]# ls -R -ld dir01/ dir01/*
drwxrwx—. 2 root root 20 7月 21 09:58 dir01/
-rwxrwx—. 1 root root 0 7月 21 09:58 dir01/file01
# 对目录递归修改user owner
[root@centos7 lab 10:54:25]# chown -R fengkai dir01/
[root@centos7 lab 10:55:58]# ls -R -ld dir01/ dir01/*
drwxrwx—. 2 fengkai root 20 7月 21 09:58 dir01/
-rwxrwx—. 1 fengkai root 0 7月 21 09:58 dir01/file01
# 对目录递归同时修改user和group owner
[root@centos7 lab 10:56:01]# chown -R fengkai:wheel dir01/
[root@centos7 lab 10:57:03]# ls -R -ld dir01/ dir01/*
drwxrwx—. 2 fengkai wheel 20 7月 21 09:58 dir01/
-rwxrwx—. 1 fengkai wheel 0 7月 21 09:58 dir01/file01
案例:准备一个普通用户家目录
模拟:创建一个用户tom,该用户没有自动创建家目录
[root@centos7 lab 10:57:04]# useradd -M tom
准备用户家目录
[root@centos7 ~ 16:59:18]# cp -r /etc/skel/ /home/tom
[root@centos7 ~ 16:59:58]# ls -ld /home/tom/
drwxr-xr-x. 3 root root 78 7月 21 16:59 /home/tom/
[root@centos7 ~ 17:00:12]# chmod u=rwx,go=- /home/tom/
[root@centos7 ~ 17:02:14]# ls -ld /home/tom/
drwx——. 3 root root 78 7月 21 16:59 /home/tom/
[root@centos7 ~ 17:02:15]# chown -R tom:tom /home/tom/
[root@centos7 ~ 17:02:57]# ls -ld /home/tom/
drwx——. 3 tom tom 78 7月 21 16:59 /home/tom/
# 验证
[root@centos7 ~ 17:02:58]# su – tom
[tom@centos7 ~ 17:03:32]$
2.4 验证 rwx 权限-针对文件
# 初始环境准备,用root权限
[root@centos7 ~ 17:05:56]# mkdir /lab;cd /lab
[root@centos7 lab 17:07:54]# cp /etc/hosts .
[root@centos7 lab 17:08:24]# ls -l
总用量 4
-rw-r–r–. 1 root root 158 7月 21 17:08 hosts
[root@centos7 lab 17:08:27]# chmod o=- hosts
[root@centos7 lab 17:08:50]# cat hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
# 切换普通用户验证
[fengkai@centos7 ~ 17:10:56]$ cd /lab/
[fengkai@centos7 lab 17:11:35]$ ll
总用量 4
-rw-r—–. 1 root root 158 7月 21 17:08 hosts
[fengkai@centos7 lab 17:11:37]$ cat hosts
cat: hosts: 权限不够
[fengkai@centos7 lab 17:11:45]$ echo hello > hosts
-bash: hosts: 权限不够
[fengkai@centos7 lab 17:12:13]$ /lab/hosts
-bash: /lab/hosts: 权限不够
# r权限验证–准备
[root@centos7 lab 17:09:49]# chmod o=r hosts
# r权限验证
[fengkai@centos7 lab 17:12:43]$ ls -l hosts
-rw-r–r–. 1 root root 158 7月 21 17:08 hosts
[fengkai@centos7 lab 17:13:50]$ cat hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
# w权限验证–准备
[root@centos7 lab 17:13:34]# chmod o=w hosts
# w权限验证
[fengkai@centos7 lab 17:14:28]$ ls -l hosts
-rw-r—w-. 1 root root 158 7月 21 17:08 hosts
[fengkai@centos7 lab 17:15:06]$ echo ni hao > hosts
[fengkai@centos7 lab 17:15:18]$ cat hosts
cat: hosts: 权限不够
# 是否可以使用vim修改文件内容
[fengkai@centos7 lab 17:15:22]$ vim hosts
# 仍然无法读取文件内容,只能覆盖修改(命令q/wq后面需要加!,如果wq!强制执行,本质上是删除原文件,创建新文件,新文件的所属会变化成当前执行的fengkai用户)
[fengkai@centos7 lab 17:16:35]$ echo hello world >> hosts
# 切换root用户,查看之前echo的两条命令都写进去了
[root@centos7 lab 17:14:53]# cat hosts
ni hao
hello world
# x权限验证–准备
[root@centos7 lab 17:17:41]# chmod o=x hosts
[root@centos7 lab 17:20:15]# echo 'echo hello world' > mycommand
[root@centos7 lab 17:20:51]# cat /lab/mycommand
echo hello world
[root@centos7 lab 17:20:58]# chmod u+x mycommand
[root@centos7 lab 17:21:17]# ls -l mycommand
-rwxr–r–. 1 root root 17 7月 21 17:20 mycommand
[root@centos7 lab 17:22:02]# /lab/mycommand
hello world
# x权限验证
[fengkai@centos7 lab 17:24:59]$ /lab/mycommand
-bash: /lab/mycommand: 权限不够
# 原因:无法读取文件代码
[root@centos7 lab 17:25:51]# chmod o=rx mycommand
[fengkai@centos7 lab 17:26:04]$ ls -l mycommand
-rwxr–r-x. 1 root root 17 7月 21 17:20 mycommand
[fengkai@centos7 lab 17:26:13]$ /lab/mycommand
hello world
2.5 验证 rwx 权限-针对目录
# 初始环境准备
[root@centos7 lab 17:26:09]# mkdir dir01
[root@centos7 lab 17:27:49]# mv hosts dir01/
[root@centos7 lab 17:27:54]# ls -l dir01/
总用量 4
-rw-r—-x. 1 root root 19 7月 21 17:17 hosts
[root@centos7 lab 17:28:49]# ll
总用量 4
drwxr-xr-x. 2 root root 19 7月 21 17:27 dir01
-rwxr–r-x. 1 root root 17 7月 21 17:20 mycommand
[root@centos7 lab 17:28:53]# chown -R root:root dir01/
[root@centos7 lab 17:30:02]# chmod -R a=- dir01/
[root@centos7 lab 17:30:39]# ll
总用量 4
d———. 2 root root 19 7月 21 17:27 dir01
-rwxr–r-x. 1 root root 17 7月 21 17:20 mycommand
[root@centos7 lab 17:30:56]# ls -l dir01/
总用量 4
———-. 1 root root 19 7月 21 17:17 hosts
[root@centos7 lab 17:31:00]# chmod o=r dir01/hosts
[root@centos7 lab 17:31:22]# ls -l dir01/
总用量 4
——-r–. 1 root root 19 7月 21 17:17 hosts
# 无权限验证
[fengkai@centos7 lab 17:27:37]$ ls dir01/
ls: 无法打开目录dir01/: 权限不够
[fengkai@centos7 lab 17:33:15]$ cd dir01/
-bash: cd: dir01/: 权限不够
[fengkai@centos7 lab 17:33:20]$ touch dir01/file01
touch: 无法创建"dir01/file01": 权限不够
# r权限–准备
[root@centos7 lab 17:31:24]# chmod o=r dir01/
[root@centos7 lab 17:34:08]# ls -l dir01/
总用量 4
——-r–. 1 root root 19 7月 21 17:17 hosts
[root@centos7 lab 17:34:17]# ll
总用量 4
d——r–. 2 root root 19 7月 21 17:27 dir01
-rwxr–r-x. 1 root root 17 7月 21 17:20 mycommand
# r权限–验证
[fengkai@centos7 lab 17:34:52]$ ls dir01/
ls: 无法访问dir01/hosts: 权限不够
hosts
[fengkai@centos7 lab 17:34:55]$ ls -l dir01/
ls: 无法访问dir01/hosts: 权限不够
总用量 0
-????????? ? ? ? ? ? hosts
# x权限–准备
[root@centos7 lab 17:34:19]# chmod o=x dir01/
[root@centos7 lab 17:36:00]# ls -ld dir01/ dir01/*
d——–x. 2 root root 19 7月 21 17:27 dir01/
——-r–. 1 root root 19 7月 21 17:17 dir01/hosts
# x权限–验证
[fengkai@centos7 lab 17:35:15]$ cat dir01/hosts
ni hao
hello world
[fengkai@centos7 lab 17:36:52]$ cd dir01/
[fengkai@centos7 dir01 17:37:02]$ ls
ls: 无法打开目录.: 权限不够
[fengkai@centos7 dir01 17:37:05]$ cat hosts
ni hao
hello world
# w权限–准备
[root@centos7 lab 17:36:16]# chmod o=w dir01/
[root@centos7 lab 17:37:51]# ls -ld dir01/ dir01/*
d——-w-. 2 root root 19 7月 21 17:27 dir01/
——-r–. 1 root root 19 7月 21 17:17 dir01/hosts
# w权限–验证
[fengkai@centos7 lab 17:38:47]$ touch dir01/file01
touch: 无法创建"dir01/file01": 权限不够
[fengkai@centos7 lab 17:39:05]$ rm dir01/hosts
rm: 无法删除"dir01/hosts": 权限不够
# 追加x权限
[root@centos7 lab 17:37:52]# chmod o=wx dir01/
[root@centos7 lab 17:40:05]# ls -ld dir01/ dir01/*
d——-wx. 2 root root 19 7月 21 17:27 dir01/
——-r–. 1 root root 19 7月 21 17:17 dir01/hosts
# 体会wx效果
[fengkai@centos7 lab 17:39:39]$ cat dir01/hosts
ni hao
hello world
[fengkai@centos7 lab 17:40:35]$ touch dir01/file01
[fengkai@centos7 lab 17:40:49]$ rm dir01/hosts
rm:是否删除有写保护的普通文件 "dir01/hosts"?yes
# 切换root用户查看
[root@centos7 lab 17:40:06]# ls -ld dir01/ dir01/*
d——-wx. 2 root root 20 7月 21 17:41 dir01/
-rw-rw-r–. 1 fengkai fengkai 0 7月 21 17:40 dir01/file01
2.6 权限补充说明
- 对于文件来说:
- 赋予 w 权限的时候,也会赋予 r 权限。
- 赋予 x 权限的时候,也会赋予 r 权限。
- 对于目录来说:
- 赋予 r 权限的时候,也会赋予 x 权限。
- 赋予 w 权限的时候,也会赋予 rx 权限。
2.7 常见命令所需权限

2.8 综合测试
1.验证文件权限rwx效果,举例说明。
chmod -u=rwx /lab/passwd
使用chmod命令,-u或者-g或者-o,分别为拥有者或者组或其他人员赋予不同的文件权限(rwx任意单个或组合都可以),/lab/passwd为绝对路径下具体的文件
2.验证目录权限rwx效果,举例说明。
chmod -u=rwx /lab/dir01
使用chmod命令,-u或者-g或者-o,分别为拥有者或者组或其他人员赋予不同的目录权限(rwx任意单个或组合都可以),/lab/dir01为绝对路径下具体的目录
3.解析chown和chgrp命令作用,举例说明。
chown root:root dir01/(或者dir01/file01)
使用chown 可以将目录或者文件的拥有者和组修改为root,通过chown -R root:root dir01/,可以将dir01目录和目录下的文件递归修改为root
chgrp wheel dir01/(或者dir01/file01)
使用chgrp将目录或者文件的组修改为wheel
3.管理文件默认权限
3.1 umask 命令
设置创建新文件时,要取消的权限。
# 默认情况
[fengkai@centos7 ~ 18:02:12]$ mkdir lab;cd lab
[fengkai@centos7 lab 18:02:33]$ touch f1;mkdir d1
[fengkai@centos7 lab 18:05:45]$ ls -l
总用量 0
drwxrwxr-x. 2 fengkai fengkai 6 7月 21 18:05 d1
-rw-rw-r–. 1 fengkai fengkai 0 7月 21 18:05 f1
# umask值中权限是要剔除掉的
[fengkai@centos7 lab 18:05:49]$ umask
0002
[fengkai@centos7 lab 18:06:23]$ umask 0
[fengkai@centos7 lab 18:06:32]$ umask
0000
# 再次创建文件
[fengkai@centos7 lab 18:06:33]$ touch f2;mkdir d2
[fengkai@centos7 lab 18:06:55]$ ls -ld *2
drwxrwxrwx. 2 fengkai fengkai 6 7月 21 18:06 d2
-rw-rw-rw-. 1 fengkai fengkai 0 7月 21 18:06 f2
# 此时目录的权限是777,文件的权限是666
# 根据用户需求定制umask值,例如希望group和other位置不具有权限
[fengkai@centos7 lab 18:08:04]$ umask 077
[fengkai@centos7 lab 18:08:11]$ umask
0077
[fengkai@centos7 lab 18:08:14]$ touch f3;mkdir d3
[fengkai@centos7 lab 18:08:22]$ ls -ld *3
drwx——. 2 fengkai fengkai 6 7月 21 18:08 d3
-rw——-. 1 fengkai fengkai 0 7月 21 18:08 f3
3.2 umask 持久化生效
# 针对单个用户
[fengkai@centos7 lab 18:08:55]$ echo 'umask 077' >> ~/.bashrc
# 针对所有用户
[root@centos7 lab 17:59:13]# echo 'umask 077' >> /etc/bashrc
3.3 综合测试
1.分析umask命令作用,举例说明。
umask=0
用户创建的时候,权限全部放开,文件的-u,-g,-o为rw,目录为rwx
umask=777
用户创建的时候,权限全部收回,文件的-u,-g,-o为—,目录为—
4.管理文件特殊权限
命令对文件能执行哪些操作,取决于执行者。

4.1 SUID 针对文件(共享文件,每个人对这个文件都能操作)
# 普通用户执行passwd命令可以修改/etc/shadow文件原因
[fengkai@centos7 lab 18:08:55]$ passwd
Changing password for user fengkai.
Changing password for fengkai.
(current) UNIX password:
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[fengkai@centos7 lab 18:15:49]$ ls -l /etc/shadow
———-. 1 root root 1320 7月 21 10:57 /etc/shadow
# 查看passwd程序权限
[fengkai@centos7 lab 18:16:03]$ ls -l $(which passwd)
-rwsr-xr-x. 1 root root 27856 4月 1 2020 /usr/bin/passwd
# 普通用户执行passwd的命令时候,有效身份是root用户, root用户是可以修改shadow文件内容。
# 添加 suid 权限
[root@centos7 lab 17:59:13]# ls -l /usr/bin/vim
-rwxr-xr-x. 1 root root 2337208 12月 16 2020 /usr/bin/vim
[root@centos7 lab 18:18:06]# chmod u+s /usr/bin/vim
[root@centos7 lab 18:18:23]# ls -l /usr/bin/vim
-rwsr-xr-x. 1 root root 2337208 12月 16 2020 /usr/bin/vim
# 此时普通用户就可以修改任意文件
[fengkai@centos7 lab 18:16:57]$ vim /etc/passwd
# 删除suid权限
[root@centos7 lab 18:18:26]# chmod u-s /usr/bin/vim
[root@centos7 lab 18:19:09]# ls -l /usr/bin/vim
-rwxr-xr-x. 1 root root 2337208 12月 16 2020 /usr/bin/vim
4.2 SGID 针对目录(共享目录,每个人在该目录下创建的文件,其他人也能操作)
# 准备用户和组
[root@centos7 lab 18:19:11]# pwd
/lab
[root@centos7 lab 18:22:59]# groupadd devops
[root@centos7 lab 18:23:14]# useradd -G devops dev1
[root@centos7 lab 18:23:36]# useradd -G devops dev2
# 准备目录
[root@centos7 lab 18:23:38]# mkdir webapp
[root@centos7 lab 18:23:57]# chgrp devops webapp
[root@centos7 lab 18:24:51]# chmod g=rwx webapp
# 准备默认权限
[root@centos7 lab 18:25:07]# echo "umask 002" >> /etc/bashrc
# 实验一:普通用户创建文件,只有自己可以编辑
[root@centos7 lab 18:26:00]# su dev1
[dev1@centos7 lab 18:26:05]$ touch webapp/dev-f1
[dev1@centos7 lab 18:26:18]$ ll webapp/dev-f1
-rw-rw-r–. 1 dev1 dev1 0 7月 21 18:26 webapp/dev-f1
# 实验二:普通用户dev1创建文件,组中dev2成员也可以编辑
[root@centos7 lab 18:27:36]# chmod g+s webapp
[root@centos7 lab 18:28:40]# su dev1
[dev1@centos7 lab 18:28:45]$ touch webapp/dev-f2
[dev1@centos7 lab 18:28:51]$ ll webapp
总用量 0
-rw-rw-r–. 1 dev1 dev1 0 7月 21 18:26 dev-f1
-rw-rw-r–. 1 dev1 devops 0 7月 21 18:28 dev-f2
[root@centos7 lab 18:29:31]# su dev2
[dev2@centos7 lab 18:29:33]$ echo hello >> webapp/dev-f2
[dev2@centos7 lab 18:29:45]$ cat webapp/dev-f2
hello
4.3 sticky 针对目录(每个人在该目录下,只能对自己创建的文件操作)
# 示例文件
[root@centos7 lab 18:30:54]# ls -ld /tmp
drwxrwxrwt. 45 root root 4096 7月 21 18:30 /tmp
[root@centos7 lab 18:31:05]# stat -c %a /tmp
1777
# 切换用户
[fengkai@centos7 lab 18:32:42]$ rm /tmp/storage.log
rm:是否删除有写保护的普通空文件 "/tmp/storage.log"?yes
rm: 无法删除"/tmp/storage.log": 不允许的操作
# 用户只能删除自己创建的文件
[fengkai@centos7 lab 18:33:20]$ touch /tmp/fengkai-f1
[fengkai@centos7 lab 18:34:13]$ ls /tmp/fengkai-f1
/tmp/fengkai-f1
[fengkai@centos7 lab 18:34:20]$ ls -l /tmp/fengkai-f1
-rw——-. 1 fengkai fengkai 0 7月 21 18:34 /tmp/fengkai-f1
[root@centos7 lab 18:33:15]# su dev1
[dev1@centos7 lab 18:34:55]$ rm /tmp/fengkai-f1
rm:是否删除有写保护的普通空文件 "/tmp/fengkai-f1"?yes
rm: 无法删除"/tmp/fengkai-f1": 不允许的操作
[fengkai@centos7 lab 18:34:26]$ rm /tmp/fengkai-f1
[fengkai@centos7 lab 18:36:03]$ ls -l /tmp/fengkai-f1
ls: 无法访问/tmp/fengkai-f1: 没有那个文件或目录
4.4 查找系统中特殊权限文件
# 查找系统中所有具有suid权限的文件
[root@centos7 lab 18:36:46]# find / -perm -4000
# 或者
[root@centos7 lab 18:37:05]# find / -perm -u+s
4.5 综合测试
1.分析suid对于执行文件的作用,举例说明。
SUID 针对文件:共享文件,每个人对这个文件都能操作
chmod u+s /usr/bin/vim:通过chmod u命令+或者-s,使/usr/bin/vim文件,能够让其他人操作
2.分析sgid对于目录的作用,举例说明。
SGID 针对目录:共享目录,每个人在该目录下创建的文件,其他人也能操作
chmod g+s webapp:通过chmod g命令+或者-s,使webapp目录下的文件,能够让其他人操作
5.管理文件扩展权限
需求:创建一个文件,root用户也无法编辑和删除?
解答:文件扩展属性。
[root@centos7 lab 18:37:15]# chattr –help
Usage: chattr [-RVf] [-+=aAcCdDeijsStTu] [-v version] files…
两个常用属性:
- append only (a),只能追加文件内容。
- immutable (i),不可变更属性。
append only
# 新建一个文件并设置
[root@centos7 lab 18:42:44]# touch /opt/operator.log
[root@centos7 lab 18:43:22]# chattr +a /opt/operator.log
# 文件不能被覆盖
[root@centos7 lab 18:43:39]# echo hello > /opt/operator.log
-bash: /opt/operator.log: 不允许的操作
# 文件可以被追加写入
[root@centos7 lab 18:43:48]# echo hello1 >> /opt/operator.log
[root@centos7 lab 18:44:00]# echo hello2 >> /opt/operator.log
[root@centos7 lab 18:44:07]# cat /opt/operator.log
hello1
hello2
# 文件不能被删除
[root@centos7 lab 18:44:19]# rm -f /opt/operator.log
rm: 无法删除"/opt/operator.log": 不允许的操作
immutable 属性
[root@centos7 ~ 18:46:46]# cp /etc/passwd ./passwd
[root@centos7 ~ 18:47:02]# chattr +i passwd
[root@centos7 ~ 18:47:18]# echo 'lw:x:1000:1000:lw:/home/lw:/bin/bash' >> passwd
-bash: passwd: 权限不够
[root@centos7 ~ 18:47:39]# rm -f passwd
rm: 无法删除"passwd": 不允许的操作
[root@centos7 ~ 18:47:47]# chattr -i passwd
[root@centos7 ~ 18:49:04]# echo 'lw:x:1000:1000:lw:/home/lw:/bin/bash' >> passwd
# 重要的文件,内容改完后,再把i属性加回去。
[root@centos7 ~ 18:49:09]# chattr +i passwd
[root@centos7 ~ 18:49:17]# vim passwd
49 lw:x:1000:1000:lw:/home/lw:/bin/bash
6.管理文件访问控制列表(了解)
需求:如何给不同的用户赋予不同的权限?
解答:访问控制列表。
6.1 针对用户
# 准备文件
[root@centos7 lab]# cp /etc/passwd ./passwd
[root@centos7 lab]# chmod o=- passwd
[root@centos7 lab]# ll passwd
-rw-r—–. 1 root root 2539 7月 22 16:36 passwd
# 赋予laoma读取权限
[root@centos7 lab]# setfacl -m u:laoma:rw passwd
# 此时 group 位置对应的权限是mask权限,也就是特定用户、所有组和other用户能够获得的最大权限。
[root@centos7 lab]# ls -l passwd
-rw-rw—-+ 1 root root 2539 7月 22 16:36 passwd
[root@centos7 lab]# getfacl passwd
# file: passwd
# owner: root
# group: root
user::rw-
user:laoma:rw-
group::r–
mask::rw-
other::—
# 验证
[laoma@centos7 ~]$ ll /lab/passwd
-rw-r—–+ 1 root root 2539 7月 22 16:36 /lab/passwd
[laoma@centos7 ~]$ head -n 1 /lab/passwd
root:x:0:0:root:/root:/bin/bash
# 同时设置多个规则,参照如下
[root@centos7 lab]# setfacl -m u:tom:rwx,u:laoma:r passwd
6.2 针对组
[root@centos7 lab]# setfacl -m g:wheel:rwx passwd
# 此时 mask 值变为 rwx
[root@centos7 lab]# ls -l passwd
-rw-rwx—+ 1 root root 2539 7月 22 16:36 passwd
[root@centos7 lab]# getfacl passwd
# file: passwd
# owner: root
# group: root
user::rw-
user:laoma:rw-
group::r–
group:wheel:rwx
mask::rwx
other::—
6.3 mask 设置
为了防止权限失控,最后一步设置相关用户的最大权限。
[root@centos7 lab]# setfacl -m m:- passwd
[root@centos7 lab]# ls -l passwd
-rw——-+ 1 root root 2539 7月 22 16:36 passwd
[root@centos7 lab]# getfacl passwd
# file: passwd
# owner: root
# group: root
user::rw-
user:laoma:rw- #effective:—
group::r– #effective:—
group:wheel:rwx #effective:—
mask::—
other::—
6.4 针对目录的 acl
在具有默认acl规则的目录中创建文件,文件会继承目录的默认acl。
[root@centos7 lab]# mkdir test
[root@centos7 lab]# setfacl -m u:laoma:rw test
[root@centos7 lab]# ls -ld test
drwxrwxr-x+ 2 root root 6 7月 22 16:47 test
[root@centos7 lab]# touch test/f1
[root@centos7 lab]# ls -l test/f1
-rw-r–r–. 1 root root 0 7月 22 16:47 test/f1
# 设置目录默认 acl
[root@centos7 lab]# setfacl -m d:u:laoma:rw test
[root@centos7 lab]# getfacl test/
# file: test/
# owner: root
# group: root
user::rwx
user:laoma:rw-
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:laoma:rw-
default:group::r-x
default:mask::rwx
default:other::r-x
[root@centos7 lab]# touch test/f2
[root@centos7 lab]# ls -l test/f2
-rw-rw-r–+ 1 root root 0 7月 22 16:48 test/f2
[root@centos7 lab]# getfacl test/f2
# file: test/f2
# owner: root
# group: root
user::rw-
user:laoma:rw-
group::r-x #effective:r–
mask::rw-
other::r–





