Linux 文件系统的权限

==============================================================================

概述:

==============================================================================

权限管理

 1.文件属性及权限类别

Linux 文件系统的权限_acl、SUID、SGID、chmod、

文件属性:

  文件系统上的权限是指文件系统上的文件和目录的权限,文件系统上的权限主要针对三类对象(访问者)定义:

  • owner:属主,u;

  • group:属组,g;

  • other:其他,o

rwxrwxrwx:

  • 左三位:定义user(owner)的权限;

  • 中三位:定义group的权限;

  • 右三位:定义other的权限

进程安全上下文

  进程对文件的访问权限应用模型:

  • 进程的属主与文件的属主是否相同,如果相同则应用属主权限;

  • 否则,则检查进程的属主是否属于文件的属组,如果是则应用属组权限;

  • 否则,就只能应用other权限

每个文件对每类访问者都定义了三种权限:

  • r:readable;读权限

  • w:writable;写权限

  • x:executable;执行权限

对文件来说:

  • r:可获取文件的数据,用文件查看类工具获取其内容;

  • w:可修改文件的内容和数据;(不能删除文件本身)

  • x:可将此文件运行为进程(针对二进制程序或脚本)

对目录来说:

  • r:可使用ls命令来获取目录中的文件列表;(不包括获取详细信息,ls -l)

  • w:可修改此目录下的文件列表,即:创建或删除文件;

  • x:可cd至此目录中,且可使用 ls -l 来获取所有文件的详细属性信息;

  • X:只给目录x权限,不给文件x权限 (配合 -R 选项,并且只能用模式法)

 1.权限表示方法:

权限表示方法:

权限、二进制、八进制对应的表示方法如下:

  • ---:    000     0

  • --x:    001     1

  • -w-:      010     2

  • -wx:   011     3

  • r--:     100     4

  • r-x:     101     5

  • rw-:    110     6

  • rwx:    111     7

附图:

Linux 文件系统的权限_用户组_02

示例:

   640 表示:rw-r-----,664表示:rw-rw-r--,755表示:rwxr-xr-x

chmod:权限管理命令

chmod:文件权限修改

用法:

  • chmod [OPTION]... MODE[,MODE]... FILE...   (模式法)

  • chmod [OPTION]... OCTAL-MODE FILE...       (八进制模式)

  • chmod [OPTION]... --reference=RFILE FILE... (参考其他文件文件权限修改文件权限)

三类用户:

  • u:属主;

  • g:属组;

  • o:其他;

  • a:所有;

chmod [OPTION]... MODE[,MODE]... FILE...   (模式法)

 MODE表示法:

  • 赋权表示法:直接操作一类用户的所有权限位rwx

        u=

        g=

        o=

        a=

  • 授权表示法:直接操作一类用户的一个权限位r,w,x

        u+,u-

        g+,g-

        o+,o-

        a+,a-

chmod [OPTION]... OCTAL-MODE FILE...   (八进制模式)

  • 八进制表示法:

   如:640 表示:rw-r-----,664表示:rw-rw-r--,755表示:rwxr-xr-x

chmod [OPTION]... --reference=RFILE FILE... (参考其他文件文件权限修改文件权限)

  • 参考RFILE文件的权限,将FILE修改为同RFILE的权限

选项:

  • -R:递归修改权限

注意:

  • 使用 -R 递归修改权限时,建议使用赋权表示法和授权表示法,不建议用八进制表示法,因为八进制表示法不清楚目录下的文件权限,可能会带来危险操作

演示:

[root@centos7 ~]# cp /etc/fstab /tmp/
 [root@centos7 ~]# ll /tmp/fstab 
-rw-r--r-- 1 root root 595 2月  10 20:32 /tmp/fstab

# MODE赋权表示法
[root@centos7 ~]# chmod g=rw /tmp/fstab
[root@centos7 ~]# ll /tmp/fstab 
-rw-rw-r-- 1 root root 595 2月  10 20:32 /tmp/fstab
[root@centos7 ~]# chmod ug=r /tmp/fstab
[root@centos7 ~]# ll /tmp/fstab 
-r--r--r-- 1 root root 595 2月  10 20:32 /tmp/fstab
[root@centos7 ~]# chmod u=rwx,g=rw,o= /tmp/fstab
[root@centos7 ~]# ll /tmp/fstab 
-rwxrw---- 1 root root 595 2月  10 20:32 /tmp/fstab

# MODE授权表示法:
[root@centos7 ~]# chmod u-x /tmp/fstab
[root@centos7 ~]# ll /tmp/fstab 
-rw-rw---- 1 root root 595 2月  10 20:32 /tmp/fstab
[root@centos7 ~]# chmod o+r /tmp/fstab
[root@centos7 ~]# ll /tmp/fstab 
-rw-rw-r-- 1 root root 595 2月  10 20:32 /tmp/fstab
[root@centos7 ~]# chmod ug+x /tmp/fstab
[root@centos7 ~]# ll /tmp/fstab 
-rwxrwxr-- 1 root root 595 2月  10 20:32 /tmp/fstab
[root@centos7 ~]# chmod g-wx /tmp/fstab
[root@centos7 ~]# ll /tmp/fstab 
-rwxr--r-- 1 root root 595 2月  10 20:32 /tmp/fstab
[root@centos7 ~]# chmod u=r /tmp/fstab
[root@centos7 ~]# ll /tmp/fstab 
-r--r--r-- 1 root root 595 2月  10 20:32 /tmp/fstab
[root@centos7 ~]# chmod +x /tmp/fstab
[root@centos7 ~]# ll /tmp/fstab 
-r-xr-xr-x 1 root root 595 2月  10 20:32 /tmp/fstab
[root@centos7 ~]# chmod -x /tmp/fstab
[root@centos7 ~]# ll /tmp/fstab 
-r--r--r-- 1 root root 595 2月  10 20:32 /tmp/fstab
[root@centos7 ~]# chmod +w /tmp/fstab   # 不允许全局都有写权限,太危险
[root@centos7 ~]# ll /tmp/fstab 
-rw-r--r-- 1 root root 595 2月  10 20:32 /tmp/fstab
[root@centos7 ~]# chmod u+x,g+w /tmp/fstab  # 多类用户权限不同时,用逗号可开
[root@centos7 ~]# ll /tmp/fstab 
-rwxrw-r-- 1 root root 595 2月  10 20:32 /tmp/fstab

#===================================================================================
# 八进制表示法:
[root@centos7 ~]# chmod 660 /tmp/fstab
[root@centos7 ~]# ll /tmp/fstab 
-rw-rw---- 1 root root 595 2月  10 20:32 /tmp/fstab

# 参考RFILE文件的权限,将FILE修改为同RFILE的权限
[root@centos7 ~]# ll /var/log/messages
-rw------- 1 root root 644634 2月  10 21:01 /var/log/messages
[root@centos7 ~]# ll /tmp/fstab 
-rw-rw---- 1 root root 595 2月  10 20:32 /tmp/fstab
[root@centos7 ~]# chmod --reference=/var/log/messages /tmp/fstab 
[root@centos7 ~]# ll /tmp/fstab 
-rw------- 1 root root 595 2月  10 20:32 /tmp/fstab

 2.-R递归修改权限

[root@centos7 ~]# cp -r /etc/skel/ /tmp
[root@centos7 ~]# ll -d /tmp/skel/
drwxr-xr-x 3 root root 74 2月  10 21:25 /tmp/skel/
[root@centos7 ~]# ll -a /tmp/skel/
总用量 16
drwxr-xr-x   3 root root   74 2月  10 21:25 .
drwxrwxrwt. 12 root root 4096 2月  10 21:25 ..
-rw-r--r--   1 root root   18 2月  10 21:25 .bash_logout
-rw-r--r--   1 root root  193 2月  10 21:25 .bash_profile
-rw-r--r--   1 root root  231 2月  10 21:25 .bashrc
drwxr-xr-x   4 root root   37 2月  10 21:25 .mozilla
[root@centos7 ~]# ll -a /tmp/skel/.mozilla/
总用量 0
drwxr-xr-x 4 root root 37 2月  10 21:25 .
drwxr-xr-x 3 root root 74 2月  10 21:25 ..
drwxr-xr-x 2 root root  6 2月  10 21:25 extensions
drwxr-xr-x 2 root root  6 2月  10 21:25 plugins

# -R级联修改权限 属组和其他用户权限为空
[root@centos7 ~]# chmod -R go= /tmp/skel/
[root@centos7 ~]# ll -d /tmp/skel/
drwx------ 3 root root 74 2月  10 21:25 /tmp/skel/
[root@centos7 ~]# ll -a /tmp/skel/
总用量 16
drwx------   3 root root   74 2月  10 21:25 .
drwxrwxrwt. 12 root root 4096 2月  10 21:28 ..
-rw-------   1 root root   18 2月  10 21:25 .bash_logout
-rw-------   1 root root  193 2月  10 21:25 .bash_profile
-rw-------   1 root root  231 2月  10 21:25 .bashrc
drwx------   4 root root   37 2月  10 21:25 .mozilla
[root@centos7 ~]# ll -a /tmp/skel/.mozilla/
总用量 0
drwx------ 4 root root 37 2月  10 21:25 .
drwx------ 3 root root 74 2月  10 21:25 ..
drwx------ 2 root root  6 2月  10 21:25 extensions
drwx------ 2 root root  6 2月  10 21:25 plugins

# -R级联修改权限 属组增加读权限
[root@centos7 ~]# chmod -R g+r /tmp/skel/
[root@centos7 ~]# ll -d /tmp/skel/
drwxr----- 3 root root 74 2月  10 21:25 /tmp/skel/
[root@centos7 ~]# ll -a /tmp/skel/
总用量 16
drwxr-----   3 root root   74 2月  10 21:25 .
drwxrwxrwt. 12 root root 4096 2月  10 21:28 ..
-rw-r-----   1 root root   18 2月  10 21:25 .bash_logout
-rw-r-----   1 root root  193 2月  10 21:25 .bash_profile
-rw-r-----   1 root root  231 2月  10 21:25 .bashrc
drwxr-----   4 root root   37 2月  10 21:25 .mozilla

从属关系管理命令:chown、chgrp

chown命令:

格式:

  • chown [OPTION]... [OWNER][:[GROUP]] FILE... (冒号也可写为"."号)

  • chown [OPTION]... --reference=RFILE FILE...

[OWNER][:[GROUP]]有三种表示方法:

  • OWNER:只修改属主;

  • OWNER:GROUP 或OWNER.GROUP:修改属主和属组;

  • :GROUP或 .GROUP:只修改属组

选项:

  • -R:递归修改

chgrp命令:

用法:

  • chgrp [OPTION]... GROUP FILE...

  • chgrp [OPTION]... --reference=RFILE FILE...

选项:

  • -R:递归修改

注意:

  • 用户对目录有写权限,但对目录下的文件没有写权限时,是不能修改此文件的内容(目录下的文件没有写权限),但是可以删除此文件(因为对目录有写权限);


演示:

# chown修改属主
[root@centos7 ~]# chown -R centos /tmp/skel/
[root@centos7 ~]# ll -d /tmp/skel/
drwxr----- 3 centos root 74 2月  10 21:25 /tmp/skel/
[root@centos7 ~]# ll -a /tmp/skel/
总用量 16
drwxr-----   3 centos root   74 2月  10 21:25 .
drwxrwxrwt. 12 root   root 4096 2月  10 22:53 ..
-rw-r-----   1 centos root   18 2月  10 21:25 .bash_logout
-rw-r-----   1 centos root  193 2月  10 21:25 .bash_profile
-rw-r-----   1 centos root  231 2月  10 21:25 .bashrc
drwxr-----   4 centos root   37 2月  10 21:25 .mozilla

# chown递归修改属主和属组
[root@centos7 ~]# chown -R arclinux:mygrp /tmp/skel/
[root@centos7 ~]# ll -d /tmp/skel/
drwxr----- 3 arclinux mygrp 74 2月  10 21:25 /tmp/skel/
[root@centos7 ~]# ll -a /tmp/skel/
总用量 16
drwxr-----   3 arclinux mygrp   74 2月  10 21:25 .
drwxrwxrwt. 12 root     root  4096 2月  10 22:55 ..
-rw-r-----   1 arclinux mygrp   18 2月  10 21:25 .bash_logout
-rw-r-----   1 arclinux mygrp  193 2月  10 21:25 .bash_profile
-rw-r-----   1 arclinux mygrp  231 2月  10 21:25 .bashrc
drwxr-----   4 arclinux mygrp   37 2月  10 21:25 .mozilla

[root@centos7 ~]# chown -R root.root /tmp/skel/
[root@centos7 ~]# ll -d /tmp/skel/
drwxr----- 3 root root 74 2月  10 21:25 /tmp/skel/
[root@centos7 ~]# ll -a /tmp/skel/
总用量 16
drwxr-----   3 root root   74 2月  10 21:25 .
drwxrwxrwt. 12 root root 4096 2月  10 22:55 ..
-rw-r-----   1 root root   18 2月  10 21:25 .bash_logout
-rw-r-----   1 root root  193 2月  10 21:25 .bash_profile
-rw-r-----   1 root root  231 2月  10 21:25 .bashrc
drwxr-----   4 root root   37 2月  10 21:25 .mozilla

文件或目录创建时的遮罩码:umask

umask:文件权限的反向掩码,遮罩码

  • 所谓遮掩码是指在创建一个文件时或目录时的默认权限

文件:

  • 666-umask (之所以用666去减,表示文件默认不能拥有执行权限)

  • 如果某类用户的权限减得的结果中存在执行权限(奇数),则将其权限+1,偶数保留;

        eg: umask=023, 666-023=643+1=644

目录:

  • 777-umask

umask 命令:

  • umask:查看当前的umask

  • umask MASK:设置umask (次类设定只对当前shell进程有效)

非特权用户umask的值是002,root的umask是022

配置文件:

  • 全局设置:/etc/bashrc

  • 用户设置:~/.bashrc 或者 ~/.bash_profile

Linux系统上的特殊权限(SUID,SGID,Sticky)

Linux权限模型是由mode和owner组成:

  • mode有:r,w,x

  • owner有:user, group, other

安全上下文

   前提:进程有属主和属组;文件有属主和属组

任何一个可执行程序文件能不能启动为进程:取决发起者对程序文件是否拥有执行权限;

启动为进程之后,其进程的属主为发起者;进程的属组为发起者所属的组;

进程访问文件时的权限,取决于进程的发起者

  • 进程的发起者,同文件的属主:则应用文件属主权限

  • 进程的发起者,属于文件属组;则应用文件属组权限

  • 否则,应用文件“其它”权限

 1.可执行文件上的SUID权限

SUID

前提:此类文件有可执行权限的命令

任何一个可执行程序文件能不能启动为进程:取决发起者对程序文件是否拥有执行权限

启动为进程之后,其进程的属主为原程序文件的属主;

注意:

  • SUID只对二进制可执行程序才有意义,才有效

  • SUID设置在目录上无意义

  • 权限设定: 4代表suid

       chmod u+s FILE...

       chmod u-s FILE...

  • 使用 ls -l 查看时,此 s 可能显示为大写或小写两种形式之一

       属主原有执行权限时,显示为小写;

       属主原无执行权限时,显示为大写

演示:

Linux 文件系统的权限_acl、SUID、SGID、chmod、_03

Linux 文件系统的权限_acl、SUID、SGID、chmod、_04

  2.权限设定 4代表suid

Linux 文件系统的权限_用户组_05


 2.目录上的SGID权限

SGID

默认情况下,用户创建文件时,其属组为此用户所属的基本组;

一旦某目录被设定了SGID,则对此目录有写权限的用户在此目录中创建的文件所属的组为此目录的属组;

通常用于创建一个协作目录;

权限设定:2 代表 SGID

  • chmod g+s DIR...

  • chmod g-s DIR...

 3.Sticky粘滞位

Sticky

具有写权限的目录通常用户可以删除该目录中的任何文件,无论是否拥有对该文件的权限;

对于一个多人可写的牡蛎,如果设置了sticky位,则每个用户仅能删除自己的文件

在目录设置Sticky 位,只有文件的所有者或root可以删除该文件

sticky 设置在文件上无意义

权限设定:1 代表 sticky

  • chmod o+t DIR...

  • chmod o-t DIR...

演示:

[root@centos7 ~]# mkdir /testdir
[root@centos7 ~]# ll -d /testdir
drwxr-xr-x 2 root root 6 2月  11 10:47 /testdir

# 授权其他用户对此目录的写权限(在此目录中可创建和删除文件)和粘滞位权限(每个用户仅能删除自己的文件)
[root@centos7 ~]# chmod o+w /testdir/
[root@centos7 ~]# chmod o+t /testdir
[root@centos7 ~]# ll -d /testdir/
drwxr-xrwt 2 root root 15 2月  11 10:48 /testdir/

# root 用户在目录中创建文件
[root@centos7 ~]# echo "taotaoxiuxiu" > /testdir/f1

# 切换到centos用户
[root@centos7 ~]# su - centos
上一次登录:六 2月 11 10:52:49 CST 2017pts/0 上

# centos在目录中创建文件f2
[centos@centos7 ~]$ echo "zaiyiqi" > /testdir/f2
[centos@centos7 ~]$ ll /testdir/
总用量 8
-rw-r--r-- 1 root   root   13 2月  11 10:58 f1
-rw-rw-r-- 1 centos centos  8 2月  11 10:59 f2

# 可以发现centos用户仅能删除自己的文件,而不能删除别人的文件
[centos@centos7 ~]$ rm -f /testdir/f1
rm: 无法删除"/testdir/f1": 不允许的操作
[centos@centos7 ~]$ rm -f /testdir/f2
[centos@centos7 ~]$ ll /testdir/
总用量 4
-rw-r--r-- 1 root root 13 2月  11 10:58 f1

# 如果把目录的粘滞位t去除,则centos用户就可以删除其他用户的文件,如下:
[root@centos7 ~]# chmod o-t /testdir/
[root@centos7 ~]# ll -d  /testdir/
drwxr-xrwx 2 root root 15 2月  11 11:05 /testdir/
[root@centos7 ~]# su - centos

[centos@centos7 ~]$ rm -f /testdir/f1  # 成功删除root的文件f1
[centos@centos7 ~]$ ll /testdir
总用量 0

 4.权限位映射

三个特殊位也可组成一组权限,即:suidsgidsticky

几个权限位映射:

SUID: user,占据属主的执行权限位

  • s:属主拥有x权限

  • S:属主没有x权限

SGID: group,占据属组的执行权限位

  • s:group拥有x权限

  • S:group没有x权限

Sticky: other,占据other的执行权限位

  • t:other拥有x权限

  • T:other没有x权限

示例:

Linux 文件系统的权限_acl、SUID、SGID、chmod、_06

演示:

# 添加SGID权限
[root@centos7 ~]# chmod 2755 /testdir
[root@centos7 ~]# ll -d  /testdir/
drwxr-sr-x 2 root root 6 2月  11 11:11 /testdir/

# 添加SGID和Sticky权限
[root@centos7 ~]# chmod 3755 /testdir
[root@centos7 ~]# ll -d  /testdir/
drwxr-sr-t 2 root root 6 2月  11 11:11 /testdir/

# 添加SUID,SGID和Sticky权限
[root@centos7 ~]# chmod 7755 /testdir
[root@centos7 ~]# ll -d  /testdir/
drwsr-sr-t 2 root root 6 2月  11 11:11 /testdir/

[root@centos7 ~]# chmod u-s,g-s,o-t /testdir
[root@centos7 ~]# ll -d  /testdir/
drwxr-xr-x 2 root root 6 2月  11 11:11 /testdir/

chattr:设定文件特定属性

chattr:设定文件特定属性

用法:

  • chattr +i  FILE...:不能删除,移动,更改 ;

    chattr -i  FILE...:取消如上特权;

  • chattr +a FILE...:不可移动删除,只能追加;

    chattr -a FILE...:取消如上特权;

  • lsattr: 显示特定属性

演示:

# +i 和 -i 选项
[root@centos7 ~]# ll -d  /testdir/
drwxr-xr-x 2 root root 6 2月  11 11:11 /testdir/
[root@centos7 ~]# echo "aaaaaaaa" > /testdir/f1
[root@centos7 ~]# ll /testdir
总用量 4
-rw-r--r-- 1 root root 9 2月  11 11:38 f1

[root@centos7 ~]# chattr +i /testdir/f1  # 设定文件属性不可删除,移动,更改
[root@centos7 ~]# ll /testdir
总用量 4
-rw-r--r-- 1 root root 9 2月  11 11:38 f1
[root@centos7 ~]# rm -f /testdir/f1 
rm: 无法删除"/testdir/f1": 不允许的操作
[root@centos7 ~]# echo "dddddd" >> /testdir/f1
-bash: /testdir/f1: 权限不够
[root@centos7 ~]# mv /testdir/f1 /testdir/f2
mv: 无法将"/testdir/f1" 移动至"/testdir/f2": 不允许的操作

[root@centos7 ~]# chattr -i /testdir/f1 # 取消特权
[root@centos7 ~]# ll /testdir
总用量 4
-rw-r--r-- 1 root root 9 2月  11 11:38 f1
[root@centos7 ~]# echo "dddddd" >> /testdir/f1
[root@centos7 ~]# mv /testdir/f1 /testdir/f2
[root@centos7 ~]# ll /testdir/
总用量 4
-rw-r--r-- 1 root root 16 2月  11 11:41 f2
[root@centos7 ~]# rm -f /testdir/f2

#==============================================================================
# +a 选项
[root@centos7 ~]# ll /testdir/
总用量 4
-rw-r--r-- 1 root root 7 2月  11 11:47 f1
[root@centos7 ~]# cat /testdir/f1
dddddd
[root@centos7 ~]# lsattr /testdir/
---------------- /testdir/f1

[root@centos7 ~]# chattr +a  /testdir/f1
[root@centos7 ~]# lsattr /testdir/
-----a---------- /testdir/f1
[root@centos7 ~]# lsattr /testdir/f1 
-----a---------- /testdir/f1

[root@centos7 ~]# rm -f /testdir/f1
rm: 无法删除"/testdir/f1": 不允许的操作
[root@centos7 ~]# mv /testdir/f1 /testdir/f2
mv: 无法将"/testdir/f1" 移动至"/testdir/f2": 不允许的操作
[root@centos7 ~]# echo "aaaaaa" > /testdir/f1
-bash: /testdir/f1: 不允许的操作

[root@centos7 ~]# echo "aaaaaa" >> /testdir/f1
[root@centos7 ~]# cat /testdir/f1
dddddd
aaaaaa
[root@centos7 ~]# chattr -a  /testdir/f1
[root@centos7 ~]# lsattr /testdir/f1 
---------------- /testdir/f1

ACL:访问控制列表

ACLAccess Control List

实现灵活的权限管理;

除了文件的所有者,所属组和其它人,可以对更多的用户设置权限;

CentOS7.0默认创建的xfs和ext4文件系统有ACL功能;

CentOS7.X之前版本,默认手工创建的ext4文件系统无ACL功能。需手动增加:

  • tune2fs –o acl/dev/sdb1

  • mount –o acl/dev/sdb1 /mnt

ACL生效顺序:所有者,自定义用户,自定义组,其他人;

为多用户或者组的文件和目录赋予访问权限rwx

用法:

  • setfacl [-bkRd] [{-m|-x} acl参数]  目标文件名

选项:

  • -m:设置后续的acl参数给文件使用,不可以与-x合用。

  • -x:删除后续的acl参数,不可与-m合用。

  • -b:删除所有的acl设置参数。

  • -k:删除默认的acl参数。

  • -R:递归设置acl。

  • -d:设置默认的acl参数,对目录无效。只对在该目录下新建的文件有效,会引用其默认权限

示例:

  • mount -o acl /directory

  • getfacl file |directory

  • setfacl -m u:wang:rwx file|directory

  • setfacl -Rm g:sales:rwX directory(对sales目录设置rwx权限,但对目录下的子文件设置rw权限,无x权限,注意X的作用)

  • setfacl -M file.acl file|directory(系统调用文件里的权限设置)

  • setfacl -m g:salesgroup:rw file| directory

  • setfacl -m d:u:wang:rx directory

  • setfacl -x u:wang file |directory:删除权限

  • setfacl -X file.acl directory(删除系统调用里的权限设置,和上面的 -M 用法相反。)

演示:

 1.setfacl -m u:salesuser:rw file| directory

[root@centos7 ~]# ll /testdir/f1 
-rw-r--r-- 1 root root 14 2月  11 11:56 /testdir/f1  # 对其他用户仅有读权限
[root@centos7 ~]# cat /testdir/f1
dddddd
aaaaaa

# 为/testdir/f1设定acl权限,对centos没有任何权限,对arclinux用户可读可写
[root@centos7 ~]# setfacl -m u:centos:0 /testdir/f1
[root@centos7 ~]# setfacl -m u:arclinux:rw /testdir/f1
[root@centos7 ~]# ll /testdir/f1 
-rw-rw-r--+ 1 root root 14 2月  11 11:56 /testdir/f1  # 可以看到权限最后多了一个“+”

# 切换到centos用户,因为设定其对/testdir/f1没有任何权限,所以原来的读权限也不允许
[root@centos7 ~]# su - centos
上一次登录:六 2月 11 11:11:40 CST 2017pts/0 上
[centos@centos7 ~]$ cat /testdir/f1 
cat: /testdir/f1: 权限不够
[centos@centos7 ~]$ exit
登出

# 切换到arclinux用户,因为设定其对/testdir/f1有rw全新,所以原来的没有写权限也可以允许写操作
[root@centos7 ~]# su - arclinux
上一次登录:五 2月 10 23:29:17 CST 2017pts/1 上
[arclinux@centos7 ~]$ cat /testdir/f1 
dddddd
aaaaaa
[arclinux@centos7 ~]$ echo "cccccc" >> /testdir/f1 
[arclinux@centos7 ~]$ cat /testdir/f1 
dddddd
aaaaaa
cccccc
[arclinux@centos7 ~]$ exit
登出

# 查看该文件的acl权限
[root@centos7 ~]# getfacl /testdir/f1 
getfacl: Removing leading '/' from absolute path names
# file: testdir/f1
# owner: root
# group: root
user::rw-
user:centos:---
user:arclinux:rw-
group::r--
mask::rw-
other::r--

# 删除文件对指定用户的acl权限
[root@centos7 ~]# setfacl -x u:centos /testdir/f1
[root@centos7 ~]# getfacl /testdir/f1 
getfacl: Removing leading '/' from absolute path names
# file: testdir/f1
# owner: root
# group: root
user::rw-
user:arclinux:rw-
group::r--
mask::rw-
other::r--

[root@centos7 ~]# setfacl -x u:centos /testdir/f1
[root@centos7 ~]# setfacl -x u:arclinux /testdir/f1
[root@centos7 ~]# getfacl /testdir/f1 
getfacl: Removing leading '/' from absolute path names
# file: testdir/f1
# owner: root
# group: root
user::rw-
group::r--
mask::r--  
other::r--

# 清除所有的ACL权限
[root@centos7 ~]# setfacl -b /testdir/f1
[root@centos7 ~]# ll  /testdir/f1 
-rw-r--r-- 1 root root 21 2月  11 12:24 /testdir/f1

#========================================================================
                   # 对目录设置acl权限
# 原来目录对其他用户没有写权限,即不允许创建和删除目录中的文件
[root@centos7 ~]# ll -d /testdir
drwxr-xr-x 2 root root 15 2月  11 15:10 /testdir

# 设置目录对centos有rwx权限
[root@centos7 ~]# setfacl -m u:centos:rwx /testdir/
[root@centos7 ~]# ll -d /testdir
drwxrwxr-x+ 2 root root 15 2月  11 15:10 /testdir

[root@centos7 ~]# getfacl /testdir/
getfacl: Removing leading '/' from absolute path names
# file: testdir/
# owner: root
# group: root
user::rwx
user:centos:rwx  # 设置成功
group::r-x
mask::rwx
other::r-x

# 切换到centos用户,可以在目录中创建新文件
[root@centos7 ~]# su - centos
[centos@centos7 ~]$ echo "dddddd" > /testdir/f3
[centos@centos7 ~]$ ll /testdir/
总用量 12
-rw-r--r-- 1 root   root   21 2月  11 12:24 f1
-rw-rw-r-- 1 centos centos  7 2月  11 15:18 f3

# 取消该目录对centos用户的rwx权限
[root@centos7 ~]# setfacl -x u:centos /testdir/
[root@centos7 ~]# getfacl /testdir/
getfacl: Removing leading '/' from absolute path names
# file: testdir/
# owner: root
# group: root
user::rwx
group::r-x
mask::r-x
other::r-x

# 再次创建目录,提示权限不够
[root@centos7 ~]# su - centos
[centos@centos7 ~]$ echo "ddddwd" > /testdir/f4
-bash: /testdir/f4: 权限不够

  2.setfacl -m g:salesgroup:rw file| directory

# 1.开始状态
[root@centos7 ~]# ll /testdir/f1 
-rw-r--r-- 1 root root 21 2月  11 12:24 /testdir/f1
[root@centos7 ~]# id arclinux
uid=1002(arclinux) gid=1002(arclinux) 组=1002(arclinux),1003(mygrp)
[root@centos7 ~]# getfacl /testdir/f1 
getfacl: Removing leading '/' from absolute path names
# file: testdir/f1
# owner: root
# group: root
user::rw-
group::r--
other::r--

# 2.对文件设定针对组mygrp的acl权限,禁止读写执行
[root@centos7 ~]# setfacl -m g:mygrp:0 /testdir/f1

[root@centos7 ~]# ll /testdir/f1 
-rw-r--r--+ 1 root root 21 2月  11 12:24 /testdir/f1
[root@centos7 ~]# getfacl /testdir/f1 
getfacl: Removing leading '/' from absolute path names
# file: testdir/f1
# owner: root
# group: root
user::rw-
group::r--
group:mygrp:--- # 设定成功
mask::r--
other::r--

# 3.验证,arclinux属于mygrp组,访问文件发现没有读权限
[root@centos7 ~]# su - arclinux
[arclinux@centos7 ~]$ cat /testdir/f1 
cat: /testdir/f1: 权限不够
[arclinux@centos7 ~]$ exit
登出

# 对其他用户有读权限
[root@centos7 ~]# su - centos
[centos@centos7 ~]$ cat /testdir/f1 
dddddd
aaaaaa
cccccc

# 4.取消对组mygrp的acl权限,
[root@centos7 ~]# setfacl -x g:mygrp /testdir/f1

[root@centos7 ~]# getfacl /testdir/f1 
getfacl: Removing leading '/' from absolute path names
# file: testdir/f1
# owner: root
# group: root
user::rw-
group::r--
mask::r--
other::r--

# 没有清除干净,还有mask
[root@centos7 ~]# ll /testdir/f1 
-rw-r--r--+ 1 root root 21 2月  11 12:24 /testdir/f1

# 彻底清除所有的ACL权限
[root@centos7 ~]# setfacl -b /testdir/f1
[root@centos7 ~]# ll /testdir/f1 
-rw-r--r-- 1 root root 21 2月  11 12:24 /testdir/f1
[root@centos7 ~]# getfacl /testdir/f1 
getfacl: Removing leading '/' from absolute path names
# file: testdir/f1
# owner: root
# group: root
user::rw-
group::r--
other::r--

  3.setfacl -m d:u:wang:rx directory (设置默认权限在目录上,目录本身不会有acl权限,只对新建的子文件产生影响)

# 1.初始文件状态
[root@centos7 ~]# ll -d /testdir/
drwxr-xr-x 2 root root 15 2月  11 15:39 /testdir/
[root@centos7 ~]# ll /testdir/
总用量 4
-rw-r--r-- 1 root root 21 2月  11 12:24 f1

# 1.设置目录/testdir的默认权限为,对centos用户有rwx权限,
[root@centos7 ~]# setfacl -m d:u:centos:rwx /testdir/
[root@centos7 ~]# ll -d /testdir/
drwxr-xr-x+ 2 root root 15 2月  11 15:39 /testdir/

[root@centos7 ~]# getfacl /testdir/
getfacl: Removing leading '/' from absolute path names
# file: testdir/
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
default:user::rwx
default:user:centos:rwx
default:group::r-x
default:mask::rwx
default:other::r-x

#2.root用户创建f2文件,可以看到继承了默认权限,但没有执行权限x
[root@centos7 ~]# touch /testdir/f2
[root@centos7 ~]# getfacl /testdir/f2
getfacl: Removing leading '/' from absolute path names
# file: testdir/f2
# owner: root
# group: root
user::rw-
user:centos:rwx			#effective:rw- #继承默认权限
group::r-x			#effective:r--
mask::rw-
other::r--

# 3.切换到centos用户
[root@centos7 ~]# su - centos

# 创建文件,提示权限不够,说明acl权限不对目录生效,因为目录/testdir对普通用户没有写权限
[centos@centos7 ~]$ touch /testdir/f3
touch: 无法创建"/testdir/f3": 权限不够

# 4.但是root新建的子文件却继承了默认权限,对centos用户可读,可写(原来对其他用户不可写)
[centos@centos7 ~]$ ll /testdir/f2
-rw-rw-r--+ 1 root root 7 2月  11 15:50 /testdir/f2

[centos@centos7 ~]$ echo "ssssss" > /testdir/f2

# 删除默认的acl权限
[root@centos7 ~]# setfacl -k /testdir/

[root@centos7 ~]# getfacl /testdir/
getfacl: Removing leading '/' from absolute path names
# file: testdir/
# owner: root
# group: root
user::rwx
group::r-x
other::r-x

[root@centos7 ~]# ll -d /testdir/
drwxr-xr-x 2 root root 24 2月  11 15:45 /testdir/

 

注意:

  • ACL文件上的group权限是mask 值(自定义用户,自定义组,拥有组的最大权限),而非传统的组权限

  • getfacl可看到特殊权限:flags

  • 默认ACL权限给了x,文件也不会继承x权限。

  • base ACL 不能删除

  • setfacl-k dir:删除默认ACL权限

mask只影响除所有者和other的之外的人和组的最大权限

  • Mask需要与用户的权限进行逻辑与运算后,才能变成有限的权限(Effective Permission)

  • 用户或组的设置必须存在于mask权限设定范围内才会生效。

设置方法:

  • setfacl -m m:: FILE|DIR

演示:            

Linux 文件系统的权限_用户组_07

 

备份和恢复ACL

  • 主要的文件操作命令cp和mv都支持ACL,只是cp命令需要加上 -a,-p 参数。但是tar等常见的备份工具是不会保留目录和文件的ACL信息。

其他方法:

  • # getfacl -R /tmp/dir1 > acl.txt

  • # setfacl -R -b /tmp/dir1

  • # setfacl -R --set-file=acl.txt /tmp/dir1

  • # getfacl -R /tmp/dir1

演示:

Linux 文件系统的权限_用户组_08




练习:

   1.在/data/testdir里创建的新文件自动属于g1组,组g2的成员如:alice能对这些新文件有读写权限,组g3的成员如:tom只能对新文件有读权限,其它用户(不属于g1,g2,g3)不能访问这个文件夹。

Linux 文件系统的权限_Linux_09

Linux 文件系统的权限_用户组_10

Linux 文件系统的权限_Linux_11

Linux 文件系统的权限_用户组_12

Linux 文件系统的权限_acl、SUID、SGID、chmod、_13

         

练习2:Linux 文件系统的权限_用户组_14

 



注意:

  1.对目录下的文件进行操作(新建文件或删除),一定要首先看文件目录的权限,是否具备操作权限。(如果目录other没有w权限,那么other用户就不能在目录下创建和删除文件,即使是文件的属主other也不行),然后再看该文件的权限,是否匹配,允许操作。  权限查看顺序:目录-->子目录或文件

Linux 文件系统的权限_用户组_15