Linux文件权限管理

Linux 文件权限管理

文件系统权限介绍

文件系统权限介绍

Linux文件权限简单灵活,易于理解和应用,能够处理大多数权限使用情况。

文件有三个适用权限的用户类别:

  • 单个用户拥有者,通常是创建该文件的用户。
  • 单个组拥有者,通常是创建该文件的用户的主要组。
  • 除了用户拥有者和组拥有者之外的其他用户。
[sh@centos7 ~ 10:26:11]$ ls
data  dir1  lab
[sh@centos7 ~ 10:31:57]$ ls -l 
总用量 0
drwxrwxr-x. 2 sh   sh   16 724 10:23 data
drwx------. 2 sh   sh   19 724 10:01 dir1
drwxr-xr-x. 2 root root 20 724 10:05 lab

对于/etc/passwd文件来说,-rw-r–r–字符串分成四份,格式如下:

  • 第一位代表文件类型,例如,**-**代表普通文件,d代表目录,l(L的小写)代表软链接等。
  • 第二到第四位,代表user-owner具有的权限,也就是laoma用户具有的权限。
  • 第五到第七位,代表group-owner具有的权限,也就是laoma组中成员具有的权限。
  • 第八到第十位,代表user-owner和group-owner之外的用户具有的权限。

权限优先级

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

在这里插入图片描述

rwx 权限解读

目录中保存文件,文件中保存数据(例如字符串)。

在这里插入图片描述

文件系统权限管理

chmod 命令

作用:更改文件不同ower权限。

语法1

**语法1:**chmod WhoHowWhat /path/to/file

  • Who: u(user) g(group) o(other) a(all)
  • How: +(添加) -(减去) =(精确设置)
  • What: r(read) w(write) x(excute) -(不具有权限)
针对文件
[sh@centos7 ~ 10:33:28]$ mkdir test
[sh@centos7 ~ 10:33:36]$ cd test
[sh@centos7 test 10:33:39]$ cp /etc/passwd .
# 给user增加x权限
sh@centos7 test 10:33:45]$ ll
总用量 4
-rw-r--r--. 1 sh sh 2391 724 10:33 passwd
[sh@centos7 test 10:34:47]$ chmod u+x passwd 
[sh@centos7 test 10:34:56]$ ll
总用量 4
-rwxr--r--. 1 sh sh 2391 724 10:33 passwd
# 一次性设置多个
[sh@centos7 test 10:34:57]$ chmod u-wx,g+w,o=- passwd 
[sh@centos7 test 10:35:31]$ ll
总用量 4
-r--rw----. 1 sh sh 2391 724 10:33 passwd
[sh@centos7 test 10:35:32]$ 
# 一次性设置所有对象
[sh@centos7 test 10:35:32]$ chmod a=rw passwd 
[sh@centos7 test 10:36:01]$ ll
总用量 4
-rw-rw-rw-. 1 sh sh 2391 724 10:33 passwd
[sh@centos7 test 10:36:03]$ 
[sh@centos7 test 10:36:03]$ chmod a-w passwd 
[sh@centos7 test 10:36:31]$ ll
总用量 4
-r--r--r--. 1 sh sh 2391 724 10:33 passwd
[sh@centos7 test 10:36:32]$ 
针对目录
# 针对目录,准备目录和文件
[root@centos7 lab 13:48:31]# mkdir dir1
[root@centos7 lab 13:48:41]# touch dir1/f1
[root@centos7 lab 13:48:47]# ls -ld dir1/ dir1/f1 
drwxr-xr-x. 2 root root 16 724 13:48 dir1/
-rw-r--r--. 1 root root  0 724 13:48 dir1/f1
# 递归清除所有对象所有权限
[root@centos7 lab 13:48:56]# chmod -R a=- dir1
[root@centos7 lab 13:49:42]# ls -ld dir1/ dir1/*
d---------. 2 root root 16 724 13:48 dir1/
----------. 1 root root  0 724 13:48 dir1/f1
# 递归设置user对象权限为rwx
[root@centos7 lab 13:49:51]# chmod -R u+rwx dir1
[root@centos7 lab 13:50:17]# ls -ld dir1/ dir1/*
drwx------. 2 root root 16 724 13:48 dir1/
-rwx------. 1 root root  0 724 13:48 dir1/f1
语法2

语法2: chmod ### /path/to/file

  • 第1个#,代表user权限
  • 第2个#,代表group权限
  • 第3个#,代表other权限

#,是一个数字范围是0(—)到7(rwx)。

补充:二进制与10进制转换

二进制 十进制 对应权限

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,对应10进制为644

# 示例2,文件权限为 -rwx rw- r-x
# 用二进制表达权限为 111 110 101,对应10进制为765

# 文件权限为634对应的权限
[root@centos7 ~ 13:51:57]# mkdir /lab;cd /lab
[root@centos7 lab 13:52:17]# cp /etc/passwd .
[root@centos7 lab 13:52:41]# chmod 634 passwd 
[root@centos7 lab 13:52:49]# ls -l passwd 
-rw--wxr--. 1 root root 2391 724 13:52 passwd
[root@centos7 lab 13:52:57]# stat -c %A passwd 
-rw--wxr--
[root@centos7 lab 13:53:06]# stat -c %a passwd 
634

# 文件权限为755对应的权限
[root@centos7 lab 13:53:13]# chmod 755 passwd 
[root@centos7 lab 13:53:46]# ls -l passwd 
-rwxr-xr-x. 1 root root 2391 724 13:52 passwd
[root@centos7 lab 13:53:49]# 

chown chgrp 命令

作用:更改文件属主。

[root@centos7 lab 14:00:29]# ls -l passwd 
-rwxr-xr-x. 1 root root 2391 724 13:52 passwd
[root@centos7 lab 14:01:12]# 
# 修改user owner
[root@centos7 lab 14:01:12]# chown sh passwd 
[root@centos7 lab 14:01:38]# ll
总用量 4
-rwxr-xr-x. 1 sh root 2391 724 13:52 passwd
# 修改group owner
[root@centos7 lab 14:01:39]# chgrp sh passwd 
[root@centos7 lab 14:02:01]# ll
总用量 4
-rwxr-xr-x. 1 sh sh 2391 724 13:52 passwd
# 同时修改 user和group owner
[root@centos7 lab 14:02:35]# chown yun:yun passwd 
[root@centos7 lab 14:02:46]# ll
总用量 4
-rwxr-xr-x. 1 yun yun 2391 724 13:52 passwd
# 对目录递归同时修改user和group owner
[root@centos7 lab 14:03:39]# ls -R -ld dir1 dir1/*
drwxr-xr-x. 2 root root 17 724 14:03 dir1
-rw-r--r--. 1 root root  0 724 14:03 dir1/233
[root@centos7 lab 14:03:46]# chown -R yun:yun dir1
[root@centos7 lab 14:04:13]# ls -R -ld dir1 dir1/*
drwxr-xr-x. 2 yun yun 17 724 14:03 dir1
-rw-r--r--. 1 yun yun  0 724 14:03 dir1/233

案例:准备一个普通用户家目录

模拟:创建一个用户tom,该用户没有自动创建家目录

[root@centos7 lab 14:16:42]# useradd -M tom

准备用户家目录

[root@centos7 lab 14:18:12]# cp -r /etc/skel/ /home/tom
[root@centos7 lab 14:18:45]# ls -ld /home/tom/
drwxr-xr-x. 3 root root 78 724 14:18 /home/tom/
[root@centos7 lab 14:20:20]# chmod 700 /home/tom/
[root@centos7 lab 14:21:00]# ls -ld /home/tom/
drwx------. 3 root root 78 724 14:18 /home/tom/
[root@centos7 lab 14:21:06]# chown -R tom:tom /home/tom/
[root@centos7 lab 14:21:35]# ls -ld /home/tom/
drwx------. 3 tom tom 78 724 14:18 /home/tom/
[root@centos7 lab 14:21:45]# 
# 验证
[tom@centos7 ~ 14:21:55]$ su - tom
[tom@centos7 ~ 14:22:06]$ pwd
/home/tom

验证文件权限rwx效果

# 初始环境准备
[root@centos7 ~ 14:45:48]# mkdir /lab
[root@centos7 ~ 14:46:14]# cd /lab/
[root@centos7 lab 14:46:28]# cp /etc/hosts .
[root@centos7 lab 14:46:50]# ll
总用量 4
-rw-r--r--. 1 root root 158 724 14:46 hosts
[root@centos7 lab 14:46:52]# chmod o=- hosts 
[root@centos7 lab 14:47:02]# ll
总用量 4
-rw-r-----. 1 root root 158 724 14:46 hosts
[root@centos7 lab 14:47:09]# cat hosts 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@centos7 lab 14:47:12]# 

# 验证
[sh@centos7 lab 14:47:37]$ ls -ld hosts 
-rw-r-----. 1 root root 158 724 14:46 hosts
[sh@centos7 lab 14:47:44]$ cat hosts 
cat: hosts: 权限不够
[sh@centos7 lab 14:47:48]$ echo 123 >> hosts 
-bash: hosts: 权限不够
[sh@centos7 lab 14:47:53]$ ./hosts
-bash: ./hosts: 权限不够
[sh@centos7 lab 14:48:10]$ 

# r权限验证--准备
[root@centos7 lab 14:47:12]# chmod o=r hosts

# r权限验证
[sh@centos7 lab 14:48:10]$ ls -l hosts 
-rw-r--r--. 1 root root 158 724 14:46 hosts
[sh@centos7 lab 14:48:53]$ cat hosts 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[sh@centos7 lab 14:48:58]$ echo 123 >> hosts 
-bash: hosts: 权限不够
[sh@centos7 lab 14:49:06]$ ./hosts
-bash: ./hosts: 权限不够
[sh@centos7 lab 14:49:10]$ 

# w权限验证--准备
[root@centos7 lab 14:49:37]# chmod o=w hosts 

# w权限验证
[sh@centos7 lab 14:49:10]$ ls -l hosts 
-rw-r---w-. 1 root root 158 724 14:46 hosts
[sh@centos7 lab 14:50:08]$ cat hosts 
cat: hosts: 权限不够
[sh@centos7 lab 14:50:13]$ echo 123 >> hosts 
[sh@centos7 lab 14:50:21]$ ./hosts
-bash: ./hosts: 权限不够
[root@centos7 lab 14:49:48]# tail hosts 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
123
# 是否可以使用vim修改文件内容
[sh@centos7 lab 14:50:27]$ vim hosts 
# 仍然无法读取文件内容,只能覆盖修改

在这里插入图片描述

# x权限验证--准备
[root@centos7 lab 14:52:15]# chmod o=x hosts 
[root@centos7 lab 14:53:20]# echo 'echo hello world' > mycommand
[root@centos7 lab 14:53:47]# cat /lab/mycommand
echo hello world
[root@centos7 lab 14:53:52]# chmod u+x mycommand 
[root@centos7 lab 14:54:24]# ls -l mycommand 
-rwxr--r--. 1 root root 17 724 14:53 mycommand
[root@centos7 lab 14:54:31]# ./mycommand 
hello world
[root@centos7 lab 14:54:38]# 

# x权限验证 
[sh@centos7 lab 15:01:44]$ ./mycommand
-bash: ./mycommand: 权限不够

[root@centos7 lab 14:54:38]# chmod o=rx mycommand 
[root@centos7 lab 15:02:17]# 

[sh@centos7 lab 15:02:00]$ ./mycommand 
hello world

验证目录权限rwx效果

# 初始环境准备
root@centos7 lab 15:03:20]# mkdir dir1
[root@centos7 lab 15:03:25]# mv hosts dir1/
[root@centos7 lab 15:03:30]# chown -R root:root dir1
[root@centos7 lab 15:03:47]# chmod -R a=- dir1/
[root@centos7 lab 15:04:00]# chmod o=r dir1/hosts 
[root@centos7 lab 15:04:15]# ls -ld dir1/ dir1/*
d---------. 2 root root 19 724 15:03 dir1/
-------r--. 1 root root  8 724 14:52 dir1/hosts

# 验证
[sh@centos7 lab 15:02:22]$ ls dir1/
ls: 无法打开目录dir1/: 权限不够
[sh@centos7 lab 15:04:45]$ cd dir1/
-bash: cd: dir1/: 权限不够
[sh@centos7 lab 15:04:47]$ touch dir1/f1
touch: 无法创建"dir1/f1": 权限不够

# r权限验证--准备
[root@centos7 lab 15:04:24]# chmod o=r dir1/
d------r--. 2 root root 19 724 15:03 dir1/

# r权限验证
[sh@centos7 lab 15:06:18]$ ls dir1/
ls: 无法访问dir1/hosts: 权限不够
hosts
[sh@centos7 lab 15:06:27]$ ls -l dir1/
ls: 无法访问dir1/hosts: 权限不够
总用量 0
-????????? ? ? ? ?            ? hosts

# x权限验证--准备
[root@centos7 lab 15:08:02]# chmod o=x dir1/
[root@centos7 lab 15:08:22]# ls -ld dir1/ dir1/*
d--------x. 2 root root 19 724 15:03 dir1/
-------r--. 1 root root  8 724 14:52 dir1/hosts

# x权限验证
[sh@centos7 lab 15:06:42]$ cat dir1/hosts
echo hello world
[sh@centos7 lab 15:08:52]$ cd dir1/
[sh@centos7 dir1 15:08:59]$ ls
ls: 无法打开目录.: 权限不够
[sh@centos7 dir1 15:09:01]$ cat hosts
echo hello world

# w权限验证--准备
[root@centos7 lab 15:09:42]# chmod o=w dir1/
[root@centos7 lab 15:10:02]# ls -ld dir1/ dir1/*
d-------w-. 2 root root 19 724 15:03 dir1/
-------r--. 1 root root  8 724 14:52 dir1/hosts

# w权限验证
[sh@centos7 lab 15:15:12]$ touch dir1/f1
touch: 无法创建"dir1/f1": 权限不够
[sh@centos7 lab 15:15:27]$ rm dir1/hosts
rm: 无法删除"dir1/hosts": 权限不够

# 追加x权限
[root@centos7 lab 15:15:59]# chmod o=wx dir1/
[root@centos7 lab 15:16:06]# ls -ld dir1/ dir1/*
d-------wx. 2 root root 19 724 15:03 dir1/
-------r--. 1 root root  8 724 14:52 dir1/hosts

# 体会wx效果
[sh@centos7 lab 15:16:46]$ cat dir1/hosts
echo hello world
[sh@centos7 lab 15:17:15]$ touch dir1/f1
[sh@centos7 lab 15:17:22]$ rm dir1/hosts
rm:是否删除有写保护的普通文件 "dir1/hosts"?y

[sh@centos7 lab 15:17:36]$ ls -ld dir1/ dir1/*
ls: 无法访问dir1/*: 没有那个文件或目录
d-------wx. 2 root root 16 724 15:17 dir1/

[sh@centos7 lab 15:18:12]$ ls -ld dir1/ dir1/f1
d-------wx. 2 root root 16 724 15:17 dir1/
-rw-rw-r--. 1 sh   sh    0 724 15:17 dir1/f1

权限补充说明

  • 对于文件来说:
    • 赋予 w 权限的时候,也会赋予 r 权限。
    • 赋予 x 权限的时候,也会赋予 r 权限。
  • 对于目录来说:
    • 赋予 r 权限的时候,也会赋予 x 权限。
    • 赋予 w 权限的时候,也会赋予 rx 权限。

文件权限总结

在这里插入图片描述

管理文件默认权限

umask 命令

设置文件默认要取消的权限。

# 默认情况
[sh@centos7 ~ 15:23:56]$ mkdir lab;cd lab
[sh@centos7 lab 15:24:23]$ touch f1;mkdir d1
[sh@centos7 lab 15:24:37]$ ls -l
总用量 0
drwxrwxr-x. 2 sh sh 6 724 15:24 d1
-rw-rw-r--. 1 sh sh 0 724 15:24 f1

# umask值中权限是要剔除掉的
[sh@centos7 lab 15:24:40]$ umask 
0002
# 设置为0
[sh@centos7 lab 15:24:59]$ umask 0
[sh@centos7 lab 15:25:16]$ umask 
0000

# 再次创建文件
[sh@centos7 lab 15:25:35]$ ls -ld *2
drwxrwxrwx. 2 sh sh 6 724 15:25 d2
-rw-rw-rw-. 1 sh sh 0 724 15:25 f2
# 此时目录的权限是777,文件的权限是666,文件默认不给予x权限

# 根据用户需求定制umask值,例如希望group和other位置不具有权限
[sh@centos7 lab 15:25:45]$ umask 077
[sh@centos7 lab 15:26:46]$ umask 
0077
[sh@centos7 lab 15:26:50]$ touch f3;mkdir d3
[sh@centos7 lab 15:26:59]$ ls -ld *3
drwx------. 2 sh sh 6 724 15:26 d3
-rw-------. 1 sh sh 0 724 15:26 f3

umask 持久化生效

# 针对单个用户
[sh@centos7 lab 15:27:08]$ echo 'umask 077' >> ~/.bashrc

# 针对所有用户
[root@centos7 ~ 15:24:04]# echo 'umask 077' >> /etc/bashrc

管理文件特殊权限

命令对文件能执行哪些操作,取决于执行者。

在这里插入图片描述
在这里插入图片描述

SUID 针对文件

# 普通用户执行passwd命令可以修改/etc/shadow文件原因
[sh@centos7 ~ 15:48:59]$ passwd 
更改用户 sh 的密码 。
为 sh 更改 STRESS 密码。
(当前)UNIX 密码:
新的 密码:
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新
[sh@centos7 ~ 15:49:42]$ ls -l /etc/shadow
----------. 1 root root 1238 724 15:49 /etc/shadow

# 查看passwd程序权限
[sh@centos7 ~ 15:50:06]$ ls -l $(which passwd)
-rwsr-xr-x. 1 root root 27856 41 2020 /usr/bin/passwd

# 普通用户执行passwd的命令时候,有效身份是root用户, root用户是可以修改shadow文件内容。
# suid应用
[root@centos7 ~ 15:28:11]# chmod u+s /usr/bin/vim
[root@centos7 ~ 15:52:07]# ls -l /usr/bin/vim
-rwsr-xr-x. 1 root root 2337208 1216 2020 /usr/bin/vim

# 添加suid权限
[root@centos7 ~ 15:52:11]# chmod u+s /usr/bin/vim
[root@centos7 ~ 15:53:21]# ls -l /usr/bin/vim
-rwsr-xr-x. 1 root root 2337208 1216 2020 /usr/bin/vim
# 此时普通用户就可以修改任意文件
[sh@centos7 ~ 15:51:57]$ vim /etc/passwd

# 删除suid权限
[root@centos7 ~ 15:53:26]# chmod u-s /usr/bin/vim
[root@centos7 ~ 15:55:41]# ls -l /usr/bin/vim
-rwxr-xr-x. 1 root root 2337208 1216 2020 /usr/bin/vim

在这里插入图片描述

SGID 针对目录

# 准备用户和组
[root@centos7 ~ 15:55:45]# groupadd dev
[root@centos7 ~ 15:57:52]# useradd -G dev dev1
[root@centos7 ~ 15:58:05]# useradd -G dev dev2

# 准备目录
[root@centos7 ~ 15:58:08]# mkdir webapp
[root@centos7 ~ 15:58:25]# chgrp dev webapp
[root@centos7 ~ 15:58:36]# chmod g=rwx webapp/
# 准备默认权限
[root@centos7 ~ 15:58:51]# echo "umask 002" >> /etc/bashrc

# 实验一:普通用户创建文件,只有自己可以编辑
[root@centos7 lab 16:03:15]# su dev1
[dev1@centos7 lab 16:03:29]$ touch webapp/dev-f1
[dev1@centos7 lab 16:03:41]$ ll webapp/dev-f1
-rw-rw-r--. 1 dev1 dev1 0 724 16:03 webapp/dev-f1
[dev1@centos7 lab 16:03:46]$ 

# 实验二:普通用户创建文件,组中成员也可以编辑
[root@centos7 lab 16:04:22]# chmod g+s webapp/
[root@centos7 lab 16:04:27]# su dev1
[dev1@centos7 lab 16:04:33]$ touch webapp/dev-f2
[dev1@centos7 lab 16:04:44]$ ll webapp/
总用量 0
-rw-rw-r--. 1 dev1 dev1 0 724 16:03 dev-f1
-rw-rw-r--. 1 dev1 dev  0 724 16:04 dev-f2
[dev1@centos7 lab 16:04:50]$ 

[root@centos7 lab 16:05:15]# su dev2
[dev2@centos7 lab 16:05:19]$ echo hello world >> webapp/dev-f2
[dev2@centos7 lab 16:05:25]$ cat webapp/dev-f2
hello world

sticky 针对目录

# 示例文件
[root@centos7 lab 16:06:26]# ls -ld /tmp
drwxrwxrwt. 26 root root 4096 724 16:04 /tmp
[root@centos7 lab 16:06:32]# stat -c %a /tmp
1777
[sh@centos7 ~ 16:07:29]$ rm /tmp/storage.log 
rm:是否删除有写保护的普通空文件 "/tmp/storage.log"?y
rm: 无法删除"/tmp/storage.log": 不允许的操作

# 用户只能删除自己创建的文件
[sh@centos7 ~ 16:07:37]$ touch /tmp/sh-1
[sh@centos7 ~ 16:08:08]$ ls /tmp/sh-1
/tmp/sh-1
[sh@centos7 ~ 16:08:10]$ rm /tmp/sh-1
[sh@centos7 ~ 16:08:15]$ ls /tmp/sh-1
ls: 无法访问/tmp/sh-1: 没有那个文件或目录

查找系统中所有特殊权限文件

# 查找系统中所有具有suid权限的文件
[root@centos7 lab 16:08:38]# find / -perm -4000
# 或者
[root@centos7 lab 16:08:47]# find / -perm -u+s

在这里插入图片描述

管理文件扩展权限

需求:创建一个文件,root用户也无法编辑和删除?

解答:文件扩展属性。

[root@centos7 lab 16:46:44]# chattr --help
Usage: chattr [-RVf] [-+=aAcCdDeijsStTu] [-v version] files...

两个常用属性:

  • append only (a),只能追加文件内容。
  • immutable (i),不可变更属性。

append only

[root@centos7 lab 16:47:32]# touch /opt/operator.log
[root@centos7 lab 16:48:20]# chattr +a /opt/operator.log 
[root@centos7 lab 16:48:31]# echo hello> /opt/operator.log
-bash: /opt/operator.log: 不允许的操作
[root@centos7 lab 16:48:40]# echo hello>> /opt/operator.log
[root@centos7 lab 16:48:47]# echo hello>> /opt/operator.log
[root@centos7 lab 16:48:49]# rm -f /opt/operator.log
rm: 无法删除"/opt/operator.log": 不允许的操作

immutable 属性

[root@centos7 lab 16:49:57]# echo 'yun:x:1000:1000:yun:/home/yun:/bin/bash' >> passwd 
-bash: passwd: 权限不够
[root@centos7 lab 16:50:51]# rm -f passwd 
rm: 无法删除"passwd": 不允许的操作

[root@centos7 lab 16:51:14]# chattr -i passwd 
[root@centos7 lab 16:51:20]# echo 'yun:x:1000:1000:yun:/home/yun:/bin/bash' >> passwd 

# 重要的文件,内容改完后,再把i属性加回去。
[root@centos7 lab 16:51:22]# chattr +i passwd 

管理文件访问控制列表

需求:如何给不同的用户赋予不同的权限?

解答:访问控制列表。

针对用户

# 准备文件
[root@centos7 lab 16:53:31]# cp /etc/passwd ./passwd 
[root@centos7 lab 16:53:34]# chmod o=- passwd 
[root@centos7 lab 16:53:42]# ll passwd 
-rw-r-----. 1 root root 2331 724 16:53 passwd


# 赋予laoma读取权限
[root@centos7 lab 16:53:45]# setfacl -m u:sh:rw passwd 

# 此时 group 位置对应的权限是mask权限,也就是特定用户、所有组和other用户能够获得的最大权限。
[root@centos7 lab 16:54:16]# ls -l passwd 
-rw-rw----+ 1 root root 2331 724 16:53 passwd
[root@centos7 lab 16:54:29]# getfacl passwd 
# file: passwd
# owner: root
# group: root
user::rw-
user:sh:rw-
group::r--
mask::rw-
other::---

# 验证
[sh@centos7 ~ 16:08:18]$ ll /lab/passwd 
-rw-rw----+ 1 root root 2331 724 16:53 /lab/passwd
[sh@centos7 ~ 16:55:03]$ head -n 2 /lab/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[sh@centos7 ~ 16:55:15]$ 

# 同时设置多个规则,参照如下
[root@centos7 lab 16:55:39]# setfacl -m u:yun:rwx,u:xiao:r passwd 

针对组

[root@centos7 lab 16:56:08]# setfacl -m g:wheel:rwx passwd

# 此时 mask 值变为 rwx
[root@centos7 lab 16:56:10]# setfacl -m g:wheel:rwx passwd
[root@centos7 lab 16:56:17]# ls -l passwd 
-rw-rwx---+ 1 root root 2331 724 16:53 passwd
[root@centos7 lab 16:56:22]# getfacl passwd 
# file: passwd
# owner: root
# group: root
user::rw-
user:sh:rw-
group::r--
group:wheel:rwx
mask::rwx
other::---

mask 设置

为了防止权限失控,最后一步设置相关用户的最大权限。

[root@centos7 lab 16:56:28]# setfacl -m m:- passwd
[root@centos7 lab 16:57:00]# ls -l passwd
-rw-------+ 1 root root 2331 724 16:53 passwd
[root@centos7 lab 16:57:04]#  getfacl passwd 
# file: passwd
# owner: root
# group: root
user::rw-
user:sh:rw-			#effective:---
group::r--			#effective:---
group:wheel:rwx			#effective:---
mask::---
other::---

针对目录的 acl

在具有默认acl规则的目录中创建文件,文件会继承目录的默认acl。

[root@centos7 lab 16:57:09]# mkdir test
[root@centos7 lab 16:57:31]# setfacl -m u:sh:rw test
[root@centos7 lab 16:57:54]# ls -ld test
drwxrwxr-x+ 2 root root 6 724 16:57 test
[root@centos7 lab 16:57:58]# touch test/f1
[root@centos7 lab 16:58:06]# ls -l test/f1 
-rw-r--r--. 1 root root 0 724 16:58 test/f1

# 设置目录默认 acl
[root@centos7 lab]# setfacl -m d:u:laoma:rw test
[root@centos7 lab 16:58:11]# setfacl -m d:u:sh:rw test
[root@centos7 lab 16:58:37]# getfacl test
# file: test
# owner: root
# group: root
user::rwx
user:sh:rw-
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:sh:rw-
default:group::r-x
default:mask::rwx
default:other::r-x

[root@centos7 lab 16:58:42]# touch test/f2
[root@centos7 lab 16:58:58]# ls -l test/f2
-rw-rw-r--+ 1 root root 0 724 16:58 test/f2
[root@centos7 lab 16:59:04]# getfacl test/f2
# file: test/f2
# owner: root
# group: root
user::rw-
user:sh:rw-
group::r-x			#effective:r--
mask::rw-
other::r--
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值