page counter next up previous contents
Next: 檔案的搜尋 Up: Linux 檔案權限與管理 Previous: 改變檔案權限   Contents

Access Control Lists


		getfacl		取得 ACL 的狀態。

setfacl 設定 ACL 的狀態。

  1. 什麼是 ACL (Access Control List)?
    1. 提供傳統的owner,group,others的read,write,execute權限之外的細部權限設定。
    2. ACL 可以針對單一使用者,單一檔案或目錄來進行 r, w, x 的權限規範。
    3. ACL 主要可以針對以下項目來控制權限:
      1. 使用者 (user):可以針對使用者來設定權限;
      2. 群組 (group):針對群組為對象來設定其權限;
      3. 預設屬性 (mask):針對在該目錄下在建立新檔案/目錄時,規範新資料的預設權限。
  2. 啟動及觀察 ACL
    [root@demo ~]# mount -o remount,acl /
    [root@demo ~]# mount
    /dev/mapper/vgsrv-root on / type ext4 (rw,acl)
    

  3. 開機即啟動 ACL
    [root@demo ~]# vim /etc/fstab 
    /dev/mapper/vgsrv-root  /         ext4    defaults,acl    1 1
    

  4. setfacl:設定 ACL 的狀態。
    [root@demo ~]# setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ...
    選項:
    -m :設定後續的 acl 參數給檔案使用,不可與 -x 合用;
    -x :刪除後續的 acl 參數,不可與 -m 合用;
    -b :移除所有的 ACL 設定參數;
    -k :移除預設的 ACL 參數;
    -R :遞迴設定 acl,即包括次目錄都會被設定;
    -d :設定『預設 acl 參數』,只對目錄有效,在該目錄新建的資料會引用此預設值
    acl_spec:
    [d[efault]:] [u[ser]:]uid [:perms]
    [d[efault]:] g[roup]:gid [:perms]
    [d[efault]:] m[ask][:] [:perms]
    [d[efault]:] o[ther][:] [:perms]
    # d 選項,表示該目錄預設有設定的 acl 規範,類似 umask 的功能。
    
    [root@demo ~]# cd /tmp
    [root@demo tmp]# touch acl_test
    [root@demo tmp]# ll acl_test 
    -rw-r--r--. 1 root root 0 Jul 18 17:35 acl_test
    ## 針對使用者 aclan
    [root@demo tmp]# setfacl -m u:aclan:rwx acl_test 
    [root@demo tmp]# ll acl_test 
    -rw-rwxr--+ 1 root root 0 Jul 18 17:35 acl_test
    # 權限部分多了個 +,且權限不是原本的“644”?
    

  5. getfacl:取得 ACL 狀態。
    [root@demo ~]# getfacl filename
    選項與參數:
    getfacl 的選項幾乎與 setfacl 相同
    
    [root@demo tmp]# getfacl acl_test 
    # file: acl_test
    # owner: root
    # group: root
    user::rw-
    user:aclan:rwx  <== 多出使用者 aclan 獨立權限
    group::r--
    mask::rwx
    other::r--
    

  6. ACL 設定與觀察
    [root@demo tmp]# setfacl -m u::rwx acl_test 
    [root@demo tmp]# ll acl_test 
    -rwxrwxr--+ 1 root root 0 Jul 18 17:35 acl_test
    [root@demo tmp]# getfacl acl_test 
    # file: acl_test
    # owner: root
    # group: root
    user::rwx         <== 無使用者列表,代表設定該檔案擁有者
    user:aclan:rwx
    group::r--
    mask::rwx
    other::r--
    
    [root@demo tmp]# setfacl -m g:aclan:rx acl_test 
    [root@demo tmp]# getfacl acl_test 
    # file: acl_test
    # owner: root
    # group: root
    user::rwx
    user:aclan:rwx
    group::r--
    group:aclan:r-x  <== 針對群組 aclan 設定
    mask::rwx
    other::r--
    
    #使用者或群組所設定的權限必須存在於 mask 的權限設定範圍內才會生效
    [root@demo tmp]# setfacl -m m:r acl_test 
    [root@demo tmp]# getfacl acl_test 
    # file: acl_test
    # owner: root
    # group: root
    user::rwx
    user:aclan:rwx		#effective:r--  
    group::r--
    group:aclan:r-x		#effective:r--
    mask::r--
    other::r--
    ##effective:r--: 有效權限只有r,則user alcan與group aclan的有效權限會縮減為r。
    
    [root@demo tmp]# setfacl -m m:rx acl_test 
    [root@demo tmp]# man setfacl
    [root@demo tmp]# getfacl acl_test 
    # file: acl_test
    # owner: root
    # group: root
    user::rwx
    user:aclan:rwx			#effective:r-x
    group::r--
    group:aclan:r-x
    mask::r-x
    other::r--
    ##effective:r-x: 有效權限只有r-x,則user alcan的有效權限縮減為r-x。
    
    [root@demo tmp]# setfacl -x u:aclan: acl_test 
    [root@demo tmp]# getfacl acl_test 
    # file: acl_test
    # owner: root
    # group: root
    user::rwx
    group::r--
    group:aclan:r-x
    mask::r-x
    other::r--
    

  7. 例題:將目錄 /tmp/acl_dir,設定為讓 aclan 可以進入查閱,但不具有修改的權力。
    # 1. 先產生測試目錄
    [root@demo tmp]# mkdir acl_dir
    [root@demo tmp]# ll -d acl_*
    drwxr-xr-x. 2 root root 4096 Jul 18 18:10 acl_dir
    -rwxr-xr--+ 1 root root    0 Jul 18 17:35 acl_test
    [root@demo tmp]# chmod 700 acl_dir/
    [root@demo tmp]# ll -d acl_*
    drwx------. 2 root root 4096 Jul 18 18:10 acl_dir
    -rwxr-xr--+ 1 root root    0 Jul 18 17:35 acl_test
    
    # 2. 切換使用者 aclan 測試
    [root@demo tmp]# su - aclan
    [aclan@demo ~]$ cd /tmp/acl_dir/
    -bash: cd: /tmp/acl_dir/: Permission denied
    
    # 3. 用 root 設定 aclan 對目錄 acl_dir 的權限
    [root@demo tmp]# setfacl -m u:aclan:rx acl_dir 
    [root@demo tmp]# getfacl acl_dir
    # file: acl_dir
    # owner: root
    # group: root
    user::rwx
    user:aclan:r-x
    group::---
    mask::r-x
    other::---
    
    #4. 再切換使用者 aclan 測試
    [root@demo tmp]# su - aclan
    [aclan@demo ~]$ cd /tmp/acl_dir/
    [aclan@demo acl_dir]$ touch aaa
    touch: cannot touch `aaa': Permission denied
    

  8. 設定目錄的預設 ACL 的權限
    #1. 產生測試目錄及檔案並觀察
    [root@demo tmp]# cd acl_dir/
    [root@demo acl_dir]# touch file1
    [root@demo acl_dir]# mkdir dir1
    [root@demo acl_dir]# ll
    total 4
    drwxr-xr-x. 2 root root 4096 Jul 18 18:28 dir1
    -rw-r--r--. 1 root root    0 Jul 18 18:28 file1
    #權限後面都沒有 + ,代表這個 acl 屬性並沒有繼承
    
    #2. 讓 aclan 在 /tmp/acl_dir 底下一直具有 rx 的預設權限
    [root@demo acl_dir]# setfacl -m d:u:aclan:rx /tmp/acl_dir
    [root@demo acl_dir]# getfacl /tmp/acl_dir
    getfacl: Removing leading '/' from absolute path names
    # file: tmp/acl_dir
    # owner: root
    # group: root
    user::rwx
    user:aclan:r-x
    group::---
    mask::r-x
    other::---
    default:user::rwx
    default:user:aclan:r-x
    default:group::---
    default:mask::r-x
    default:other::---
    
    [root@demo acl_dir]# touch file2
    [root@demo acl_dir]# mkdir dir2
    [root@demo acl_dir]# ll
    total 16
    drwxr-xr-x. 2 root root 4096 Jul 18 18:28 dir1
    drwxr-x---+ 2 root root 4096 Jul 18 18:48 dir2
    -rw-r--r--. 1 root root    0 Jul 18 18:28 file1
    -rw-r-----+ 1 root root    0 Jul 18 18:48 file2
    # 權限後面有 +,表示確實有繼承。
    

  9. 將一檔案的 ACL 設定 copy 到另一檔案
    [root@demo acl_dir]# getfacl file2
    # file: file2
    # owner: root
    # group: root
    user::rw-
    user:aclan:r-x			#effective:r--
    group::---
    mask::r--
    other::---
    
    [root@demo acl_dir]# getfacl file2 | setfacl --set-file=- file1
    [root@demo acl_dir]# getfacl file1
    # file: file1
    # owner: root
    # group: root
    user::rw-
    user:aclan:r-x			#effective:r--
    group::---
    mask::r--
    other::---
    

  10. 例題:假設 /depts 與 根目錄 / 為同一個 partition。如何針對這個目錄下的資料進行 ACL 設定?
    1. 如何讓 /depts 目錄具有可以使用 ACL 功能?
    2. 請在 /depts 底下新增一個名為 tech 的目錄,這個目錄的使用者、群組為:root, hr;
    3. 使用 ACL,讓 web 這個群組在 /depts/tech 具有完整的權限:
    4. 使用 ACL 讓 alex 具有讀、執行但不可寫入,且 alex 的預設權限為 read/write?
    5. 請問 alex, Joshua, manager 在該目錄下的 rwx 為何?
  11. 例題:讓檔案系統可以支援 ACL 的作法。
    大前提:請建立一個新的 partition,且此 partition 掛載到 /opt/ 這個目錄,使用預設掛載不要加入參數!
    1. 建立使用者:新增群組名為 acct,且新增四個使用者,分別是:amy, arthur, ann, austin,這四個人的次要群組(seconday)為 acct。這四個使用者不需要密碼。
    2. 建立共用目錄,請用 root 建立 /acct 目錄,此目錄屬於 amy 且屬於 acct 群組,且權限請設定為 755。
    3. /opt 請設定為 1777 權限;
    4. 請切換身份成為 amy,利用 ACL 的控制,讓 arthur 與 ann 對 /acct 可讀可寫可執行。
    5. 請分別切換身份為為其他三人,測試看看能否寫入該目錄。
    6. 使用 amy 身份到 /acct 建立兩個檔案,檔名為 cayman, swiss,讓 arthur 可寫入 cayman,讓 ann 可寫入 swiss。
    7. 建立一個次目錄( banks ),讓所有在 banks 所建立的檔案都能讓 amy, arthur, ann 三人寫入,但 austin 則否;
    8. 讓 amy 建立檔案 /acct/banks/deposits,並觀察 ACL。請用 amy 的身份將 deposits 移動到 /tmp,並查閱 ACL,再將檔案移動到 /opt,查閱 ACL。
    9. ACL 在不同的檔案系統間移動,可能會遺失 ACL 的屬性。請使用 mount 檢查不同的檔案系統是否具有 ACL 的支援。


page counter next up previous contents
Next: 檔案的搜尋 Up: Linux 檔案權限與管理 Previous: 改變檔案權限   Contents
2015-04-13