7 文件系统权限(二):权限

rwx, chmod, chgrp, chown, SUID, SGID, Sticky, chattr, lsattr, umask, ACL, setfacl, getfacl

权限

假设这样几个场景:    
1) A用户在/testdir目录中创建了A.txt文件,B用户是否可以删除、修改、移动、重命名该文件呢?    
2) 公司有一个项目组,成员是A,B,C,D,对于/testdir/project目录下的文件,用户均可查看、修改、创建,
   但是不可删除其他人创建的文件,该项目下新建的文件或目录均属于该项目组,组外成员对该项目不能访问。
   该如何实现该要求?
3) 有一个可执行文件f1,如何在不改变属主和属组及其他用户权限设定的情况下,使A用户只能查看和执行f1,
   B用户仅具有执行权限,组G1的所有成员仅能查看。要达成该要求,又该如何完成相关设定?

为了解决上述类似的情况,Linux多采用如下三种权限模式:

一、读写执行 rwx(read,write,execute)

[root@centos7 testdir]# ll
total 4drwxr-xr-x. 2 root it      6 Aug  7 15:29 dir
-rw-r--r--. 1 root root 2338 Aug  5 10:43 passwd
#----------------------------------------------------------------------------------------------
# 以第一行为例:
# 第一列:drwxr-xr-x.
#   i)r=read,w=write,x=execute,-=nothing
#  ii)其中 rwxr-xr-x 为文件/目录的权限设定,其中:
#       1)前三位 rwx :代表文件拥有者owner的权限为“可读、可写  、可执行”
#       2)中三位 r-x :代表文件所属组group的权限为“可读、不可写、可执行”
#       3)后三位 r-x :代表 非owner和group的权限为“可读、不可写、可执行”
# iii)权限格式固定为:
#       rwx为固定顺序,owner group other 的顺序也是固定的
#           1)若将“权限位”中有权限设为1,无权限设为0,则rwxr-xr-x = 111101101(二进制)
#           2)由于每三位的rwx设定情况决定了其对应用户的权限,将rwx转换为 “八进制” 数字标识为:
#               * --x = 1(001)
#               * -w- = 2(010)
#               * -wx = 3(011)
#               * r-- = 4(100)
#               * r-x = 5(101)
#               * rw- = 6(110)
#               * rwx = 7(111)
# 第三列:root 为文件/目录的拥有者owner
# 第四列:it   为文件/目录的所属组group
#----------------------------------------------------------------------------------------------

通过上面的示例,可以初步了解Linux的权限设定的含义。 那么有 三个问题

1)如何设置权限?

chmod 修改文件/目录权限

#----------------------------------------------------------------------------------------------
# 1)chmod [OPTION]... MODE[,MODE]...     FILE... 使用字符模式修改权限
#       a. 修改一类用户的所有权限:      u=  g= o= ug=  a= u=,g=
#       b. 修改一类用户某位或某些位权限:u+  u- g+ g- o+ o- a+ a- + -
#----------------------------------------------------------------------------------------------
[root@centos7 testdir]# ll passwd
-rw-r--rw-. 1 root root 2352 Aug  7 20:54 passwd
[root@centos7 testdir]# chmod u=rwx,g+x,o-w passwd
[root@centos7 testdir]# ll passwd-rwxr-xr--. 1 root root 2352 Aug  7 20:54 passwd
[root@centos7 testdir]# chmod a=rwx passwd
[root@centos7 testdir]# ll passwd-rwxrwxrwx. 1 root root 2352 Aug  7 20:54 passwd
[root@centos7 testdir]# chmod u=rwx,go=rw passwd
[root@centos7 testdir]# ll passwd -rwxrw-rw-. 1 root root 2352 Aug  7 20:54 passwd

#----------------------------------------------------------------------------------------------
# 2)chmod [OPTION]... OCTAL-MODE         FILE... 使用8进制模式修改权限 
#----------------------------------------------------------------------------------------------
[root@centos7 testdir]# chmod -R 777 dir
[root@centos7 testdir]# tree -p .
├── [drwxrwxrwx]  dir
│   └── [-rwxrwxrwx]  f1
└── [-rwxrw-rw-]  passwd

#----------------------------------------------------------------------------------------------
# 3)chmod [OPTION]... --reference=RFILE  FILE... 参考RFILE文件的权限,将FILE的修改为同RFILE
#----------------------------------------------------------------------------------------------
[root@centos7 testdir]# lltotal 4drwxrwxrwx. 2 root root   15 Aug  8 11:23 dir
-rwxrw-rw-. 1 root root 2352 Aug  7 20:54 passwd
[root@centos7 testdir]# chmod --reference=passwd dir    ### 将dir目录的权限设为与passwd一样的权限
[root@centos7 testdir]# lltotal 4drwxrw-rw-. 2 root root   15 Aug  8 11:23 dir
-rwxrw-rw-. 1 root root 2352 Aug  7 20:54 passwd

#----------------------------------------------------------------------------------------------
# 4)大写X:给目录设定X权限,即对目录下现有内容,凡是目录都给予x权限,凡是文件都不增加x权限,不影响文件已有设定
#----------------------------------------------------------------------------------------------
[root@centos7 testdir]# tree -p /testdir/testdir
├── [drwxrw-rw-]  dir
│   ├── [-rw-rw-rw-]  cat2.sh
│   ├── [-rwxrwxrwx]  f1
│   └── [drwxrw-rw-]  subdir
└── [-rwxrw-rw-]  passwd

[root@centos7 testdir]# chmod -R g+rwX dir
[root@centos7 testdir]# tree -p /testdir/testdir
├── [drwxrwxrw-]  dir
│   ├── [-rw-rw-rw-]  cat2.sh
│   ├── [-rwxrwxrwx]  f1
│   └── [drwxrwxrw-]  subdir
└── [-rwxrw-rw-]  passwd
#----------------------------------------------------------------------------------------------
# 5)修改文件的属主、属组
#       chgrp  sales        testfile    ### 修改testfile的属组为sales
#       chown  root:admins  testfile    ### 将testfile的属主设为root,属组设为admins
#       chown  liang:       testfile    ### 将testfile的属组和属主均设为liang
#       chown  :liang       testfile    ### 将testfile的属组设为liang
#----------------------------------------------------------------------------------------------

2)三种权限rwx对文件和目录而言,含义有何差异?

读权限r

#----------------------------------------------------------------------------------------------
# 1)对文件而言,仅具备读权限,用户可查看当前workdir的文件内容
# 2)对目录而言,仅具备读权限,用户可查看目录内的文件列表,不能进入目录而无法查看目录下文件的元数据,
#    即使用户对目录下的文件也具有读权限
# 3)若用户需要查看、执行某个目录下的文件,需要具备如下条件(得一即可):
#   a. 用户想要查看或执行的文件 在用户当前的workdir下
#   b. 用户具有对目录的执行权限(目录的读权限可以没有,但建议设置,否则用户无法得知目录内文件列表的信息,
#      也不能使用tab键补全),具有对文件的读权限
# 4)对于文件/目录的元数据,用户需具有该文件或目录的上层目录的执行权限,至于对文件或目录本身是否拥有权限并不影响
#----------------------------------------------------------------------------------------------
[root@centos7 testdir]# chmod 777 /testdir          ### 使所有人都具有读写执行的权限
[root@centos7 testdir]# ll -d /testdirdrwxrwxrwx. 3 root root 42 Aug  7 15:29 /testdir

[root@centos7 testdir]# mkdir dir                   ### root用户创建/testdir/dir目录
[root@centos7 testdir]# chmod 754 dir               ### 权限设为754(owner读写执行,group读执行,other读)
[root@centos7 testdir]# cd dir
[root@centos7 dir]# touch f1 f2 f3                  ### root用户在/testdir/dir目录下创建文件f1,f2,f3
[root@centos7 dir]# tree -p /testdir                ### root用户查看/testdir的目录树及对应的权限/testdir
├── [drwxr-xr--]  dir
│   ├── [-rw-r--r--]  f1
│   ├── [-rw-r--r--]  f2
│   └── [-rw-r--r--]  f3
└── [-rw-r--r--]  passwd1 directory, 4 files

[root@centos7 testdir]# su liang               ### 临时切换到 liang 用户 
[liang@centos7 testdir]$ groups liang          ### 查看liang的组(对于dir及其下文件来说,liang属于other)
liang : liang wangcai it
[liang@centos7 testdir]$ pwd                   ### 查看liang用户当前所在的workdir/testdir

[liang@centos7 testdir]$ cat passwd            ### liang用户查看passwd文件内容
root:x:0:0:root:/root:/bin/bashliang:x:1000:1000:liang:/home/liang:/bin/bash
[liang@centos7 testdir]$ cd dir                ### (1)liang用户对于dir目录没有执行权限,不能进入dir目录
bash: cd: dir: Permission denied

[liang@centos7 testdir]$ ll -d dir             ### (2)liang用户查看 dir 目录本身
drwxr-xr--. 2 root root 33 Aug  7 17:09 dir    ###      
 [liang@centos7 testdir]$ cat /testdir/dir/f1  ### (3)liang用户查看/testdir/dir/f1的内容,但权限受限。
 cat: /testdir/dir/f1: Permission denied       ###  【问题1】为什么liang对f1有读权限,却发生访问被拒绝呢?
 [liang@centos7 testdir]$ ll dir               ### (4)liang用户 查看dir信息
 ls: cannot access dir/f1: Permission denied   ###     结果“Permission denied”及“???”,因为:
 ls: cannot access dir/f2: Permission denied   ###     1)liang对 dir 只有读权限,没有写与执行权限
 ls: cannot access dir/f3: Permission denied   ###     2)liang用 ll,能看dir下都有哪些文件
 total 0                                       ###     3)对无执行权限的dir下的文件,没获取文件元数据
 ?????????? ? ? ? ?            ? f1
 ?????????? ? ? ? ?            ? f2
 ?????????? ? ? ? ?            ? f3

[liang@centos7 testdir]$ exit
exit
[root@centos7 testdir]# chmod 755 dir          ### 切换至root,对dir设权限,使other用户具有r-x权限
[root@centos7 testdir]# tree -p /testdir/testdir
├── [drwxr-xr-x]  dir
│   ├── [-rw-r--r--]  f1
│   ├── [-rw-r--r--]  f2
│   └── [-rw-r--r--]  f3
└── [-rw-r--r--]  passwd

[root@centos7 testdir]# su liang                    ### 临时切换至liang用户
[liang@centos7 testdir]$ cat /testdir/dir/f1        ### (3’)获取dir执行权限后,查看f1 >>>参考(3)
this is file1 in /testdir/dir

[liang@centos7 testdir]$ ll dir                     ### (4’)获取dir执行权限后,查看dir>>>参考(4)
total 4-rw-r--r--. 1 root root 30 Aug  7 17:14 f1
-rw-r--r--. 1 root root  0 Aug  7 17:09 f2
-rw-r--r--. 1 root root  0 Aug  7 17:09 f3

[liang@centos7 testdir]$ cd dir                     ### (1’)获取dir执行权限后,进入dir>>>参考(1)
[liang@centos7 dir]$ 
[root@centos7 testdir]# chmod 751 dir               ### dir权限751,liang对dir目录仅有执行权限
[root@centos7 testdir]# ll
total 4drwxr-x--x. 2 root root   33 Aug  7 17:09 dir
-rw-r--r--. 1 root root 2338 Aug  5 10:43 passwd

[root@centos7 testdir]# su liang                    
[liang@centos7 testdir]$ ll dir                     ### (5)切至liang查看dir,无读权限,访问被拒绝
ls: cannot open directory dir: Permission denied
[liang@centos7 testdir]$ ll -d dir                  ### (5’)使用 ll -d 命令查看dir本身的信息
drwxr-x--x. 2 root root 33 Aug  7 17:09 dir

[liang@centos7 testdir]$ cat /testdir/dir/f1        ### (3’)liang对dir可执行,f1可读,可看f1的内容
this is file1 in /testdir/dir

#----------------------------------------------------------------------------------------------
# 对于文件/目录的元数据,用户需具有该文件或目录的上层目录的执行权限,至于对文件或目录本身是否拥有权限并不影响
#----------------------------------------------------------------------------------------------
[root@centos7 testdir]# chmod 754 dir               ### 修改dir权限,liang仅可读,对/testdir可读写执行
[root@centos7 testdir]# tree -p /testdir            ### 查看/testdir的目录树/testdir
├── [drwxr-xr--]  dir
│   ├── [-rw-r--r--]  f1
│   ├── [-rw-r--r--]  f2
│   ├── [-rw-r--r--]  f3
│   └── [-rw-rw-r--]  f4
└── [-rw-r--r--]  passwd1 directory, 5 files
[root@centos7 testdir]# su liang -c 'stat dir'      ### liang对dir仅可读,对/testdir可读写执行
  File: ‘dir’  Size: 42        	Blocks: 0          IO Block: 4096   
  directoryDevice: 805h/2053d	Inode: 33609792    Links: 2
  Access: (0754/drwxr-xr--)  Uid: (    0/    root)   Gid: (    0/    root)
  Context: unconfined_u:object_r:etc_runtime_t:s0
  Access: 2016-08-07 20:35:41.645416968 +0800
  Modify: 2016-08-07 20:19:11.577370211 +0800
  Change: 2016-08-07 20:35:33.845416599 +0800
 Birth: -
[root@centos7 testdir]# su liang -c 'stat dir/f1'   ### liang对dir、f1仅可读
stat: cannot stat ‘dir/f1’: Permission denied
[root@centos7 testdir]# chmod 751 dir               ### 修改dir权限,liang仅可执行,/testdir可读写执行
[root@centos7 testdir]# chmod 750 dir/f1            ### 修改dir/f1权限,使liang用户无权限
[root@centos7 testdir]# tree -p /testdir/testdir
├── [drwxr-x--x]  dir
│   ├── [-rwxr-x---]  f1
│   ├── [-rw-r--r--]  f2
│   ├── [-rw-r--r--]  f3
│   └── [-rw-rw-r--]  f4
└── [-rw-r--r--]  passwd

[root@centos7 testdir]# su liang -c 'stat dir'      ### liang对dir仅可执行,对/testdir可读写执行
  File: ‘dir’  Size: 42        	Blocks: 0          IO Block: 4096   
  directoryDevice: 805h/2053d	Inode: 33609792    Links: 2
  Access: (0751/drwxr-x--x)  Uid: (    0/    root)   Gid: (    0/    root)
  Context: unconfined_u:object_r:etc_runtime_t:s0
  Access: 2016-08-07 20:36:34.889419482 +0800
  Modify: 2016-08-07 20:19:11.577370211 +0800
  Change: 2016-08-07 20:36:20.875418820 +0800
 Birth: -
[root@centos7 testdir]# su liang -c 'stat dir/f1'   ### liang用户查看dir/f1的元数据(liang用户对dir仅有执行权限,对dir/f1无任何权限)
  File: ‘dir/f1’  Size: 0         	Blocks: 0          IO Block: 4096   
  regular empty fileDevice: 805h/2053d	Inode: 33609793    Links: 1
  Access: (0750/-rwxr-x---)  Uid: (    0/    root)   Gid: (    0/    root)
  Context: unconfined_u:object_r:etc_runtime_t:s0
  Access: 2016-08-07 20:12:25.865351051 +0800
  Modify: 2016-08-07 20:12:25.865351051 +0800
  Change: 2016-08-07 20:45:49.182445659 +0800
 Birth: -

写权限w

#----------------------------------------------------------------------------------------------
# 1)对于文件而言,拥有写权限,意味着用户可以修改文件内容,但是不能删除文件本身
# 2)对于目录而言,写权限以为着可以在目录中创建或删除文件,但是必须拥有执行权限才可以
#----------------------------------------------------------------------------------------------
[root@centos7 testdir]# ll
total 4
drwxr-xr-x. 2 root root   42 Aug  7 20:19 dir
-rw-r--r--. 1 root root 2338 Aug  5 10:43 passwd
[root@centos7 testdir]# su liang
[liang@centos7 testdir]$ echo "god is a girl" >> passwd ### liang对passwd无写权限,修改失败
bash: passwd: Permission denied
[liang@centos7 testdir]$ rm -f dir/f1                   ### liang用户对dir没有写权限,不能删除文件
rm: cannot remove ‘dir/f1’: Permission denied
[root@centos7 testdir]# chmod 753 dir                   ### 修改dir权限
[root@centos7 testdir]# chmod 646 passwd                ### 修改passwd权限
[root@centos7 testdir]# su liang 
[liang@centos7 testdir]$ echo "god is a girl" >> passwd ### 修改后,liang对passwd有写权限,修改成功
[liang@centos7 testdir]$ rm -f dir/f1                   ### 修改后,liang对dir有写权限,能删除文件
[liang@centos7 testdir]$ 
[root@centos7 testdir]# mkdir dir                       ### 创建dir目录
[root@centos7 testdir]# touch dir/f1 dir/f2 dir/f3      ### 在dir目录下创建f1,f2,f3文件
[root@centos7 testdir]# chmod 754 dir                   ### 设dir权限为:754,liang用户只有读权限
[root@centos7 testdir]# tree -p /testdir                ### 查看/testdir的目录树/testdir
├── [drwxr-xr--]  dir
│   ├── [-rw-r--r--]  f1
│   ├── [-rw-r--r--]  f2
│   └── [-rw-r--r--]  f3
└── [-rw-r--r--]  passwd1 directory, 4 files

[root@centos7 testdir]# su liang                        ### 切换至liang用户
[liang@centos7 testdir]$ tree -p /testdir               ### 查看/testdir的目录树/testdir
├── [drwxr-xr--]  dir
└── [-rw-r--r--]  passwd1 directory, 1 file

[liang@centos7 testdir]$ touch dir/f4                   ### liang对dir仅可读,不能在目录下创建文件
touch: cannot touch ‘dir/f4’: Permission denied

[root@centos7 testdir]# chmod 751 dir                   ### 更改dir权限设定,other权限设为仅可执行
[root@centos7 testdir]# ll -d dir
drwxr-x--x. 2 root root 42 Aug  7 20:19 dir
[root@centos7 testdir]# su liang
[liang@centos7 testdir]$ touch dir/f5                   ### liang对dir仅可执行,不能创建文件
touch: cannot touch ‘dir/f5’: Permission denied


[root@centos7 testdir]# chmod 753 dir                   ### 更改dir的权限设定,为other增加w权限
[root@centos7 testdir]# tree -p /testdir/testdir
├── [drwxr-x-wx]  dir
│   ├── [-rw-r--r--]  f1
│   ├── [-rw-r--r--]  f2
│   └── [-rw-r--r--]  f3
└── [-rw-r--r--]  passwd1 directory, 4 files

[root@centos7 testdir]# su liang                        
[liang@centos7 testdir]$ tree -p /testdir               
/testdir
├── [drwxr-x-wx]  dir [error opening dir] ###liang看/testdir目录树,对dir不可读,不能列内部文件信息
└── [-rw-r--r--]  passwd
[liang@centos7 testdir]$ touch dir/f4       ###liang对dir可写执行,可在其中建/删文件(删除示例略)
[liang@centos7 testdir]$

执行权限x

#----------------------------------------------------------------------------------------------
# 1)对于二进制程序或脚本而言,可以执行文件
# 2)对于目录而言,意味着用户可以进入目录,访问目录中的文件的元数据,若要查看文件内容或修改文件,则需拥有读写权限
#----------------------------------------------------------------------------------------------
[root@centos7 testdir]# chmod 754 /bin/cat                   ### 修改/bin/cat权限,使liang不可执行
[root@centos7 testdir]# ll /bin/cat /testdir/dir/f2             
-rwxr-xr--. 1 root    root    54048 Oct 10  2016 /bin/cat
-rw-r--r--. 1 wangcai wangcai    13 Aug  7 21:12 /testdir/dir/f2
[root@centos7 testdir]# su liang -c 'cat /testdir/dir/f2' ### 用/bin/cat看f2,提示/usr/bin/cat权限受限
bash: /usr/bin/cat: Permission denied                     ### 该提示为v7版本,v6:/bin/cat: Permission denied.

3)为什么不同的用户创建的文件和目录默认的权限不一致呢?

umask

  • 说明

    用户登录系统后,在创建文件和目录后会发现其都有了默认的权限设定,该默认设定即为umask。umask采用补码的形式,与chmod相反。

  • 语法说明

#----------------------------------------------------------------------------------------------
# 1) umask      : 查看用户默认的权限设定补码(数字形式)
#       a. root用户    :默认022
#       b. 非特权用户   :默认002
# 2) umask -S   :查看用户默认的权限设定补码(字符形式)
# 3) umask 补码 :修改当前用户的umask值
# 4) 全局设置: /etc/bashrc 
#    用户设置:~/.bashrc
#----------------------------------------------------------------------------------------------[root@centos7 ~]# umask             ### 查看root用户的umask0022[root@centos7 ~]# umask -S          ### 查看root用户的umasku=rwx,g=rx,o=rx
[root@centos7 ~]# umask 033         ### 修改root用户的umask
[root@centos7 ~]# umask0033
[liang@centos7 root]$ umask         ### 查看liang用户的umask
0002
[liang@centos7 root]$ umask -S      ### 查看liang用户的umask
u=rwx,g=rwx,o=rx

[root@centos7 ~]# umask -p
umask 0022
  • 注意点

#----------------------------------------------------------------------------------------------# 1) 对于目录而言,目录的权限 = 777 - umask# 2) 对于文件而言,文件的权限 = 666 - umask 的结果的“奇数位”+1#----------------------------------------------------------------------------------------------[root@centos7 dir]# lltotal 0-rw-r--r--. 1 root root 0 Aug  8 10:07 f1       ### 文件f1的权限为  :644 = 666-022(root的umask=022)-rw-rw-rw-. 1 root root 0 Aug  8 10:10 f2       ### 文件f2的权限为  :666 = 666-011 = 655,655的奇数位+1 = 666(root的umask=011)drwxr-xr-x. 2 root root 6 Aug  8 10:07 test     ### 目录test的权限为:755 = 777-022(root的umask=022)drwxrw-rw-. 2 root root 6 Aug  8 10:10 test2    ### 目录test的权限为:766 = 777-011(root的umask=011)

特殊权限SUID/SGID/Sticky

本文最开始,场景2 中描述的情况,使用上述的权限并没有办法解决,这就引入了特权体系。

            前提内容:文件有属主和属组,同样一个程序被启动为进程后也有属主和属组 

            (1)程序能发启动为进程,取决于发起者是否对程序文件有执行权限(不管是owner、group、other都可以) 

            (2)程序启动为进程后,进程的属主为发起者,属组为发起者所属的组 

            (3)进程若要访问文件,要看进程的发起者的权限,其顺序为: 

                    (a)判断 文件属主 进程发起者 的关系,相同则应用属主权限,不再向后看属组和其他 

                    (b)判断 文件属组 进程发起者 所属组的关系,相同则应用属组权限,不再向后看其他 

                    (c)应用其他权限

    SUID

  • 应用场景

    普通用户需要在执行某些操作时,拥有root用户的权限(如修改密码),SUID可以使用户临时以root用户或属主的身份来执行二进制程序。

    注意:

    • 1)SUID只能应用于二进制可执行程序,对目录无效

    • 2)用户将程序启动为进程之后,其进程的属主为原程序的属主

    • 3)s: 属主拥有x权限; S:属主没有x权限

  • 示例说明

#----------------------------------------------------------------------------------------------
# 修改SUID的设定:chmod u+s/u-s/4XXX FILE... (前提:必须有x权限)
#----------------------------------------------------------------------------------------------
[root@centos7 dir]# ll /bin/passwd              ### /bin/passwd有SUID
-rwsr-xr-x. 1 root root 27832 Jun 10  2014 /bin/passwd      
[root@centos7 dir]# su liang                    ### liang执行时继承属主root权限,以属主身份发起进程       
[liang@centos7 dir]$ passwd                     ### 切换至liang用户,执行/bin/passwd,挂起程序
Changing password for user liang.
Changing password for liang.
(current) UNIX password: 

[root@centos7 ~]# ps aux | grep passwd          ### 查看进程发现,passwd进程的发起人为root
root       4107  0.0  0.1 185304  2336 pts/1    S+   10:43   0:00 passwd

SGID

1)文件:SGID 使用户在执行某程序时,以该程序所属组的身份运行该程序,程序启动为进程后的属组为原程序的属组
        s: group拥有x权限 
        S:group没有x权限
2)目录:        
        a. 默认情况下,用户创建的文件/目录的属组为用户的主组;        
        b. 一旦某目录被设定了SGID,则对此目录有写权限的用户 在此目录中创建的文件 所属的组为此目录的属组
        c. 通常用于创建一个协作目录
#----------------------------------------------------------------------------------------------
# 修改SGID的设定:chmod g+s/g-s/2xxx FILE/DIR... (前提:必须有x权限)
#----------------------------------------------------------------------------------------------
[root@centos7 testdir]# groups liang                            ### liang用户的所属组
liang : liang wangcai it
[root@centos7 testdir]# tree -pgu /testdir                      ### 查看目录树、权限、属主、属组
/testdir
├── [drwxrwxrwx root     it      ]  dir
│   ├── [-rw-rw-rw- root     root    ]  f2
│   └── [drwxr-xr-x root     root    ]  test
└── [-rw-r--rw- root     root    ]  passwd

[root@centos7 testdir]# chmod g+s dir                           ### 修改dir的权限,增加SGID
[root@centos7 testdir]# su liang -c "touch /testdir/dir/f3"     ### liang用户创建f3
[root@centos7 testdir]# tree -pgu /testdir            ### 查看发现f3属组为dir属组,其他原有文件不改变
/testdir
├── [drwxrwsrwx root     it      ]  dir
│   ├── [-rw-rw-rw- root     root    ]  f2
│   ├── [-rw-rw-rw- liang    it      ]  f3
│   └── [drwxr-xr-x root     root    ]  test
└── [-rw-r--rw- root     root    ]  passwd

Sticky

1. 对于拥有写权限的目录,用户可以删除目录中的任何文件,无论用户对该文件是否有任何权限或拥有权
2. 对目录设置Sticy权限,用户可以修改其下的文件,但只有root或owner才可以删除文件
3. sticky 设置在文件上无意义
4. t: other拥有x权限; 
   T:other没有x权限
#----------------------------------------------------------------------------------------------
# 修改SGID的设定:chmod o+t/o-t/1xxx DIR... 
#----------------------------------------------------------------------------------------------
[root@centos7 testdir]# tree -pgu /testdir                      ### 当前dir目录设定/testdir
├── [drwxrwxrwx root     root    ]  dir
└── [-rw-r--rw- root     root    ]  passwd
[root@centos7 testdir]# su liang -c "touch /testdir/dir/f1"     ### liang用户创建f1
[root@centos7 testdir]# chmod o+t dir                           ### 为dir增加Sticy权限
[root@centos7 testdir]# tree -pgu /testdir/testdir
├── [drwxrwxrwt root     root    ]  dir
│   └── [-rw-rw-rw- liang    liang   ]  f1
└── [-rw-r--rw- root     root    ]  passwd

[root@centos7 testdir]# su wangcai -c "rm -rf /testdir/dir/f1"  ### wangcai用户无法删除f1
rm: cannot remove ‘/testdir/dir/f1’: Operation not permitted

[root@centos7 testdir]# su wangcai -c "echo "wangcai" >> /testdir/dir/f1" ### wangcai可以修改f1
[root@centos7 testdir]#

chattr 设定 / lsattr查看 文件特殊属性

  • chattr语法说明:

    chattr [ -RVf ] [ -v version ] [ mode ] files...
    其中mode的格式为:+-=[aAcCdDeijsStTu]
  • 示例

#----------------------------------------------------------------------------------------------
# 1) chattr +i :不能删除,改名,更改 
#----------------------------------------------------------------------------------------------
[root@centos7 testdir]# chattr +i passwd.txt        ### 为文件增加 i 属性
[root@centos7 testdir]# lsattr passwd.txt 
----i----------- passwd.txt

[root@centos7 testdir]# echo xx >> passwd.txt 
-bash: passwd.txt: Permission denied

[root@centos7 testdir]# mv passwd.txt user.txt
mv: cannot move ‘passwd.txt’ to ‘user.txt’: Operation not permitted

[root@centos7 testdir]# rm passwd.txt 
rm: cannot remove ‘passwd.txt’: Operation not permitted

[root@centos7 testdir]# chattr -i passwd.txt 
[root@centos7 testdir]# lsattr passwd.txt 
---------------- passwd.txt

[root@centos7 testdir]# echo test >> passwd.txt 
[root@centos7 testdir]#

#----------------------------------------------------------------------------------------------
# 2) chattr +A :不能更改atime 
#----------------------------------------------------------------------------------------------
[root@centos7 testdir]# stat passwd.txt 
……
Access: 2016-08-02 20:37:10.497505648 +0800
……  

[root@centos7 testdir]# chattr +A passwd.txt            ### 增加A属性,防止更改atime
[root@centos7 testdir]# cat passwd.txt > /dev/null
[root@centos7 testdir]# stat passwd.txt 
……
Access: 2016-08-02 20:37:10.497505648 +0800             ### 读后,atime没有改变   
……

[root@centos7 testdir]# ll passwd.txt 
-rwxrwxrwx. 1 root it 64 Aug  1 16:56 passwd.txt
[root@centos7 testdir]# lsattr passwd.txt               ### 查看passwd.txt的特殊属性
-------A-------- passwd.txt
[root@centos7 testdir]# chattr -A passwd.txt            ### 去掉A属性,允许更改atime
[root@centos7 testdir]# cat passwd.txt > /dev/null
[root@centos7 testdir]# lsattr passwd.txt 
---------------- passwd.txt
[root@centos7 testdir]# stat passwd.txt 
……
Access: 2016-08-03 16:47:23.768832575 +0800             ### 读后,atime发生改变
……

#----------------------------------------------------------------------------------------------
# 3) chattr +a :只能追加文件内容,不能覆盖、删除
#----------------------------------------------------------------------------------------------
[root@centos7 testdir]# chattr +a testfile

[root@centos7 testdir]# echo "haha" > testfile
-bash: testfile: Operation not permitted

[root@centos7 testdir]# echo "haha" >> testfile
[root@centos7 testdir]# cat testfile haha

[root@centos7 testdir]# rm -f testfile 
rm: cannot remove ‘testfile’: Operation not permitted

[root@centos7 testdir]# chattr -a testfile 
[root@centos7 testdir]# rm -f testfile

ACL(Access Control List)访问控制列表

  • 说明

    1. ACL 可以实现在除了文件的所有者、所属组、其他人之外,对更多的场景做出对应的权限设置。

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

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

                 tune2fs –o acl /dev/sdb1 mount –o acl /dev/sdb1 /mnt

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

setfacl

1. ACL文件上的group权限是mask 值(自定义用户,自定义组,拥有组的最大权限),而非传统的组权限
2. 默认ACL权限给了x,文件也不会继承x权限
3. base ACL 不能删除
4. mask只影响除所有者和other的之外的人和组的最大权限
5. Mask需要与用户的权限进行逻辑与运算后,才能变成有限的权限(Effective  Permission)
6. 用户或组的设置必须存在于mask权限设定范围内才会生效。
7. --set选项会把原有的ACL项都删除,用新的替代,需要注意的是一定要包含UGO的设置,不能象-m一样只是添加ACL就可以.
    如:setfacl --set u::rw,u:wang:rw,g::r,o::- file1
# setfacl  -m  u:wang:rwx       file|directory      ### 修改文件或目录的ACL设定,wang权限为rwx
# setfacl  -m  g:salesgroup:rw  file|directory      ### 修改文件或目录ACL设定,salesgroup组权限为rw
# setfacl  -m  d:u:wang:rx      directory           ### 修改目录的ACL的默认设定(只能针对于目录)
# setfacl  -m  mask::rx         file                ### 修改文件的mask值
# setfacl  -Rm g:sales:rwX      directory           ### 修改目录及下层内容的ACL的默认设定(只能对目录)
# setfacl  -M  file.acl         file|directory      ### 按照file.acl的指定要求,设定文件或目录的权限
# setfacl  -x  u:wang           file |directory     ### 清除wang用户的权限设定
# setfacl  -X  file.acl         directory           ### 按照file.acl的指定要求,清除目录的权限设定
# setfacl  -k                   directory           ### 删除目录默认ACL权限
# setfacl  -b                   file1               ### 清除所有ACL权限
# getfacl  file1 | setfacl --set-file=- file2       ### 复制file1的acl权限给file2
# setfacl --set u::rw,u:wang:rw,g::r,o::- file1     ### 将原ACL项删除,用新的代替,要包含UGO的设置,
                                                    ###   不能像-m一样只是添加ACL即可

getfacl

#----------------------------------------------------------------------------------------------
# 1. 查看文件或目录的ACL设置: getfacl file|directory 
# 2. getfacl 可看到特殊权限 :flags
#----------------------------------------------------------------------------------------------
[root@centos7 testdir]# getfacl dir
# file: dir
# owner: root
# group: root
# flags: --t
user::rwx
group::rwx
other::rwx

ACL 备份与恢复

1. 文件操作命令cp和mv都支持ACL,只是cp命令需要加上-p 参数。但tar等常见备份工具不会保留目录和文件的ACL信息
# getfacl -R     /tmp/dir1 > acl.txt         ### 将dir1目录及其下内容的ACL设置备份到文件acl.txt中
# setfacl -R -b  /tmp/dir1                   ### 清除dir目录及其下内容的ACL设置
# setfacl -R --set-file=acl.txt  /tmp/dir1   ### 用acl.txt的设置恢复dir1的ACL权限设置
# getfacl -R     /tmp/dir1                   ### 查看dir1目录及其下内容的ACL设置

原创文章,作者:taobaibai,如若转载,请注明出处:http://www.178linux.com/31280

(0)
上一篇 2016-08-19 08:36
下一篇 2016-08-19 08:36

相关推荐

  • linux入门第二课

    **linux入门基础** linux 命令类别 内部命令和外部命令 首先我们可以用echo $SHELL 来查询shell 里面会显示我们当前在用的shell 上图是echo $SHELL 也可以用cat /etc/shells来查询系统里面的shell bash其实是一个文件是一个程序 存在于 /bin/bash 目录,这是真是存在的文件,是一个可以执行…

    Linux干货 2017-05-20
  • 马哥教育网络班21期+第6周课程练习

    请详细总结vim编辑器的使用并完成以下练习题 1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; [root@localhost ~]# cp /etc/rc.d/rc.sysinit /tmp/ [root@localho…

    Linux干货 2016-08-15
  • useradd命令

    useradd创建用户 使用格式: useradd[options] LOGIN -c:备注信息保存在passwd的备注栏中。 -d:用户登入时的主目录 -e:账号的失效日期,缺省表示永久有效。 -f:密码过期后多少天关闭该账号。 -g:用户所属组。 -G:用户所属的附加群组。 -m:自动创建用户的登入目录。 -M:不要自动创建用户的登入目录。 -n:取消创…

    Linux干货 2017-12-20
  • 内核编译安装 (用NTFS模块)

    内核编译安装 (用NTFS模块) 1 rz 下载的 的内核最新文件 在这 https://www.kernel.org/ 2 tar xvf linux-4.12.10.tar.xz 解压文件 内核文件一般都放在 /usr/src/ 3 cd linux-4.12.10/ 4 [root@god linux-4.12.10]#cp /boot/config-…

    2017-09-04
  • Linux系统上的文件处理工具

    Linux系统上的文件处理工具 我们都知道在计算机中,存在大量的文件,这些文件中包含大量的信息。可是有些时候,为了工作的效率,我们在茫茫信息中提取出我们所需要的信息,这样的技能就显得尤为重要。Linux系统上为我我们提供了多种多样的文本处理工具,下面我们来简单说一下。 查看文件内容我们可以使用less命令,cat命令,more命令等等 cat cat [OP…

    Linux干货 2016-08-05
  • 脚本又见脚本,作业又是作业_第七周

    1、创建一个10G分区,并格式为ext4文件系统; (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl; (2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳; [root@centos ~]# fdisk -l &n…

    Linux干货 2016-12-30

评论列表(4条)

  • 护栏网
    护栏网 2016-11-21 10:15

    不错的文章,内容博学多才.禁止此消息:nolinkok@163.com

  • 钢筋网
    钢筋网 2016-11-28 09:40

    好文章,内容欢风华丽.禁止此消息:nolinkok@163.com

  • 外墙保温网格布
    外墙保温网格布 2016-12-05 09:46

    好文章,内容一针见血.禁止此消息:nolinkok@163.com

  • 丝网
    丝网 2016-12-10 18:27

    好文章,内容惊心动魄.禁止此消息:nolinkok@163.com