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.www58058.com/31280

(0)
taobaibaitaobaibai
上一篇 2016-08-19
下一篇 2016-08-19

相關推薦

  • 第四周:/etc/passwd、/etc/group文件熟悉及配合grep使用正則表達式

    1、復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其它用戶均沒有任何訪問權限。 [root@wlm ~]# useradd tuser1 [root@wlm ~]# cp -r etc/skel/ /home/tuser1/…

    Linux干貨 2016-10-13
  • HA之corosync+pacemaker+crmsh

    高可用集群框架 圖片轉載之http://www.www58058.com/16656 實驗拓撲: 兩臺節點服務器: node1     192.168.150.137     node1.com node2     192.168.150.138     node2.com nf…

    Linux干貨 2017-01-18
  • 命令read

    read命令是一個bash命令,它用于從鍵盤或標準輸入中讀取文本.我們可以使用read以交互的形式讀取來自用戶的輸入.并且read還提供一種不需要按回車就能夠輸入參數的方法. read可以從標準輸入中讀取單獨的一行,或者使用-u選項,從文件描述符FD中讀取.并且這單獨的行被分隔成多個域,第一個詞被賦值給第一個變量,第一個賦值給第二個變量,以此類推,直到剩下的…

    Linux干貨 2016-08-12
  • Linux之網絡管理

    Linux之網絡管理     在學習linux的過程中,Linux網絡的管理和配置中是很重要的,幾乎學習的后期都離不開網絡的概念和配置,如集群中的使用,學習好Linux網絡至關重要。     Linux網絡IP地址有兩種配置方式:靜態指定和動態分配 動態分配就是利用DHCP服務器,動態的給linux主機分配IP地址。靜…

    Linux干貨 2016-09-05
  • Linux不同發行版之間的聯系與區別

    Linux發行版有商業版、社區版兩類。兩者的共同點都是GNU/Linux發行版,區別在于商業版是由商業公司維護,社區版是社區組織維護。由于linux發行版都要遵循GPL協定,任何人都有使用、共享、修改軟件源代碼的自由,所以商業版的收入主要來自向企業提供支持服務,它的軟件基本都是免費的。

    Linux干貨 2018-03-04
  • 馬哥教育網絡班20期+第二周課程練習

    linux 中一切皆文件,我們所做的一切都是和文件打交道。   文件分為兩部分:元數據和數據           元數據: 即真實數據的屬性??捎?stat  命令查看       &nbs…

    Linux干貨 2016-06-23

評論列表(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

欧美性久久久久