Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux umask 詳細解析:預設權限計算與修改方法

編輯:Linux 教學

umask(user file-creation mode mask)決定建立新檔案/目錄時的**預設權限**。理解其計算邏輯,能避免權限過鬆導致安全風險。

查看目前 umask

umask
# 輸出範例:0022

四位數字含義

位置代表範例值說明
第 1 位特殊權限0SUID/SGID/Sticky bit,通常 0
第 2 位擁有者0捨棄權限:0=全保留(檔案不含 x)
第 3 位群組2捨棄權限:2=寫入權限(檔案同時捨棄 x)
第 4 位其他人2捨棄權限:2=寫入權限(檔案同時捨棄 x)

預設權限計算公式

  • 目錄基礎權限:777(rwxrwxrwx)
  • 檔案基礎權限:666(rw-rw-rw-),**檔案預設無執行權限**
  • 最終權限 = 基礎權限 - umask(按位減法,非數值減法)

實例:umask 0022

類型基礎權限umask結果權限符號表示
目錄777022755rwxr-xr-x
檔案666022644rw-r--r--

修改 umask

暫時生效(當前 Shell)

umask 002   # 目錄 775、檔案 664,群組可寫

永久生效

  • 全系統:編輯 /etc/profile 或 /etc/bashrc 加入 umask 002
  • 單用戶:編輯 ~/.bashrc 或 ~/.bash_profile 加入

安全建議

  • 預設 022 較安全(他人不可寫)。
  • 團隊協作目錄可設 002(群組可寫),並配合 SGID:chmod g+s dir。
  • 嚴格環境可設 077(僅擁有者可存取)。
Copyright © Windows教程網 All Rights Reserved