建立 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 顯示 | 關鍵效果 |
|---|---|---|---|---|
| 初始 | - | 0775 | drwxrwxr-x | 群組可寫,新檔群組 = 用戶主群組 |
| 加 SGID | g+s | 2775 | drwxrwsr-x | 新檔群組 = 目錄群組 (family) |
| 加 Sticky | o+t | 3775 | drwxrwsrwt | 非擁有者/root 不可刪除他人檔案 |
/tmp, /var/spool/mail) 防止誤刪/惡意刪除。2775 + o+t = 3775 是團隊專案目錄、FTP 上傳目錄、共享開發環境的黃金權限模板。若需「用戶 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 位,即可輕鬆打造安全共享空間。