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 Setuid/Setgid/Sticky Bit 實戰演練:團隊共享目錄權限控制

編輯:Linux 教學

實驗目標

建立 family 群組與 shuai、hongyan 兩用戶,透過 SGID + Sticky Bit 實現:共享目錄下新檔自動屬群組、用戶只能刪除自己檔案。

完整操作腳本


# 1. 建立群組與用戶
groupadd family
useradd shuai; useradd hongyan
echo "password" | passwd --stdin shuai
echo "password" | passwd --stdin hongyan
usermod -aG family shuai
usermod -aG family hongyan

# 2. 建立共享目錄並設定權限
mkdir /share
chown :family /share
chmod 2775 /share      # SGID (2) + rwxrwxr-x
chmod o+t /share       # Sticky Bit → 最終 3775 (drwxrwsrwt)

# 3. 驗證 SGID 效果
su - shuai
cd /share
touch shuai.txt
ls -l shuai.txt        # 群組應為 family ✅

su - hongyan
cd /share
touch hongyan.txt
ls -l hongyan.txt      # 群組應為 family ✅

# 4. 驗證 Sticky Bit 保護
su - hongyan
rm shuai.txt           # 失敗:Operation not permitted ✅

su - shuai
rm hongyan.txt         # 失敗 ✅

# 5. root 可刪除任意檔案
su - root
rm /share/*.txt        # 成功 ✅

權限位演變過程圖解

步驟chmod八進位ls -ld 顯示關鍵效果
初始-0775drwxrwxr-x群組可寫,新檔群組 = 用戶主群組
加 SGIDg+s2775drwxrwsr-x新檔群組 = 目錄群組 (family)
加 Stickyo+t3775drwxrwsrwt非擁有者/root 不可刪除他人檔案

核心知識點總結

  • SGID on 目錄:強制繼承目錄群組,解決「umask/主群組導致共享檔案群組不一致」問題。
  • Sticky Bit:保護公共寫入目錄 (如 /tmp, /var/spool/mail) 防止誤刪/惡意刪除。
  • 組合威力:2775 + o+t = 3775 是團隊專案目錄、FTP 上傳目錄、共享開發環境的黃金權限模板。
  • root 例外:Sticky Bit 不限制 root,root 可刪除任意檔案。

擴展:ACL 精細控制

若需「用戶 A 可讀寫、用戶 B 唯讀、用戶 C 無權限」,超越傳統 UGO 模型,請使用 ACL:


setfacl -m u:alice:rwx /share
setfacl -m u:bob:r-x /share
setfacl -m u:charlie:--- /share
setfacl -m g:devteam:rwx /share
getfacl /share

總結

SGID + Sticky Bit 以極簡配置解決 90% 團隊共享目錄權限痛點。理解 2775 → 3775 演變邏輯,配合 ls -ld 驗證 s/t 位,即可輕鬆打造安全共享空間。

Copyright © Windows教程網 All Rights Reserved