新建檔案時權限從哪來?答案藏在 umask 裡。理解它,才能解釋為什麼預設生出來的檔案是 644、目錄是 755。

[root@www ~]# umask
0022 ==> 與一般權限有關的是後三個數字
[root@www ~]# umask -S
u=rwx, g=rx, o=rx
建立「檔案」時預設不具執行權,最大為 666;建立「目錄」時因為 x 關係到能否進入,預設全開,最大為 777。實際權限=最大權限減去 umask。
因此 umask 為 022 時:
[root@www ~]# touch test1
[root@www ~]# mkdir test2
-rw-r--r-- 1 root root 0 Sep 27 00:25 test1
drwxr-xr-x 2 root root 4096 Sep 27 00:25 test2
把 umask 改為 002 再建立:
[root@www ~]# umask 002
-rw-rw-r-- 1 root root 0 Sep 27 00:36 test3
drwxrwxr-x 2 root root 4096 Sep 27 00:36 test4
root 的 umask 預設是 022,拿掉較多屬性,這是基於安全考量;一般使用者通常為 002,保留同群組的寫入權,方便同組協作。
umask 可用 umask 077 臨時變更,寫入 ~/.bashrc 則可永久生效。若某個檔案權限怎麼看都不對,除了 chmod,也該回頭檢查當時的 umask。至於 lsattr/chattr 管理的隱藏屬性(如 a、i),則是另一層保護,可用 lsattr 檢視。