預設權限與隱藏權限


		umask 		 查詢及設定預設權限。

chattr 改變檔案的隱藏屬性。
lsattr 顯示檔案的隱藏屬性。

  • 檔案的預設權限
    1. 若 umask 為 022,則所屬群組及其他使用者皆須扣除『寫』的權限。
      1. 建立檔案時:(-rw-rw-rw-) – (-----w--w-) ==> -rw-r--r--
      2. 建立目錄時:(drwxrwxrwx) – (-----w--w-) ==> drwxr-xr-x
    2. umask:查詢及設定預設權限。
      [root@dywHome2 test]# umask [-S] [mode]
      參數說明:
      -S(Symbolic): 以符號類型顯示權限 
      #範例: 
      [root@dywHome2 test]# umask
      0022
      [root@dywHome2 test]# umask -S
      u=rwx,g=rx,o=rx
      [root@dywHome2 test]# touch foo1
      [root@dywHome2 test]# mkdir test1
      [root@dywHome2 test]# ll
      total 1
      -rw-r--r-- 1 root root    0 Sep  4 10:53 foo1
      drwxr-xr-x 2 root root 1024 Sep  4 10:54 test1/
      [root@dywHome2 test]# umask 002
      [root@dywHome2 test]# umask u=rwx.g=rwx,o=rx
      [root@dywHome2 test]# touch foo2
      [root@dywHome2 test]# mkdir test2
      [root@dywHome2 test]# ll
      total 2
      -rw-r--r-- 1 root root    0 Sep  4 10:53 foo1
      -rw-rw-r-- 1 root root    0 Sep  4 10:54 foo2
      drwxr-xr-x 2 root root 1024 Sep  4 10:54 test1/
      drwxrwxr-x 2 root root 1024 Sep  4 10:54 test2/
      
  • 檔案的隱藏屬性
    1. chattr:改變檔案的隱藏屬性。
      [root@dywHome2 test]# chattr +-=[ASacDdIijsTtu] [檔案或目錄名稱] 
      參數說明: 
      +-= :分別為 [+ 增加] [- 減少] [= 設定] 屬性。 
      A  :存取時間 atime (access) 不可被修改。(驗證未成功)
      S  :類似 sync 的功能,會將資料同步寫入磁碟中。 
      a  :檔案只能增加資料,而不能刪除,只有 root 才能設定這個屬性。 ### 
      c  :自動將檔案『壓縮』,讀取時會自動解壓縮。 
      d  :當 dump (備份)程序被執行時,設定 d 屬性將可使該檔案(或目錄)具有 dump 功效。 
      i  :檔案不能被刪除、改名、設定連結也無法寫入或新增資料。 ###
      j  :檔案在寫入時先記錄在 journal 中。
      s  :檔案會被完全的移除出硬碟空間。 
      u  :與 s 相反,資料內容還存在磁碟中,可以 undeletion。
      
      The letters 'acdijsuADST' select the  new  attributes  for  the  files:
      append only (a), compressed (c), no dump (d), immutable (i), data jour-
      nalling (j), secure deletion (s), no tail-merging (t), undeletable (u),
      no  atime  updates  (A), synchronous directory updates (D), synchronous
      updates (S), and top of directory hierarchy (T).
       
      #範例: 
      [root@dywHome2 test]# chattr +a foo
      [root@dywHome2 test]# echo append > foo
      -bash: foo: Operation not permitted
      [root@dywHome2 test]# echo append >> foo
      [root@dywHome2 test]# cat foo
      append
      [root@dywHome2 test]# chattr +i foo
      [root@dywHome2 test]# mv foo foo1
      mv: cannot move `foo' to `foo1': Operation not permitted
      [root@dywHome2 test]# chattr -i foo
      [root@dywHome2 test]# mv foo foo1
      
    2. lsattr:顯示檔案的隱藏屬性。
      [root@dywHome2 test]# lsattr [-adR] 
      參數說明: 
      -a :列出所有檔案,含隱藏檔; 
      -d :只列出目錄,而不列目錄內容; 
      -R :連同子目錄一併列出。 
      #範例: 
      [root@dywHome2 test]# lsattr
      ----ia------- ./foo
      ------------- ./test1
      ------------- ./foo2
      ------------- ./test2
      

練習題

  1. 如何查詢檔案產生的預設權限?
    Sol. umask 或 umask -S
  2. 如何設定檔案產生的預設權限為 -rw-r—–?
    Sol. umask 026 或 umask u=rwx,g=rx,o=x 或 umask u=rw,g=r,o=
  3. 如何設定目錄產生的預設權限為 drwxr-x—?
    Sol. umask 026 或 umask u=rwx,g=rx,o=r
  4. 執行 umask 033 ,請問新建的目錄與檔案的權限為何?
    Sol. 檔案為 -rw-r–r– ,目錄為: drwxr–r–
  5. 執行 umask 026 ,請問新建的目錄與檔案的權限為何?
    Sol. 檔案為 -rw-r—– ,目錄為: drwxr-x–x
  6. 執行 umask 0022,則一般檔案產生的預設權限為何?
    Sol. -rw-r–r–
  7. 執行 umask 0022,則目錄產生的預設權限為何?
    Sol. drwxr-xr-x
  8. 執行 umask 0034,則一般檔案產生的預設權限為何?
    Sol. -rw-r—w-
  9. 執行 umask 0034,則目錄產生的預設權限為何?
    Sol. drwxr—wx
  10. 如何設定檔案 foo,只能加資料而不能刪除資料?
    Sol. chattr +a foo 或 chattr =a foo
  11. 如何去除檔案 foo,只能加資料而不能刪除資料的屬性?
    Sol. chattr -a foo
  12. 如何設定檔案 foo,不能被刪除、改名、設定連結也無法寫入或新增資料?
    Sol. chattr +i foo 或 chattr =i foo
  13. 如何去除檔案 foo,不能被刪除、改名、設定連結也無法寫入或新增資料的屬性?
    Sol. chattr -i foo
  14. 如何顯示檔案 foo 的隱藏屬性,例如:只能加資料而不能刪除資料的屬性?
    Sol. lsattr foo
  15. 如何顯示工作目錄下所有檔案的隱藏屬性,包含隱藏檔?
    Sol. lsattr -a
  16. 如何顯示工作目錄下所有檔案的隱藏屬性,連同子目錄?
    Sol. lsattr -R
  17. 如何顯示工作目錄下所有檔案的隱藏屬性,目錄只列出目錄本身,而不列目錄內容?
    Sol. lsattr -d

  DYWANG_HOME