HFS 如何分享目錄 / 檔案?#指定特定的使用者或群組分享
UNIX/Linux 檔案權限表示方式
傳統的UNIX檔案權限分為三個部分,使用ls -l可以看到類似如下的資訊:
-rwxr--r-- 1 root root 9216 Feb 25 15:02 test.sh
各欄位意義:
第一欄(-rwxr--r--):表示檔案/目錄的權限。第一字元若為d,即表示目錄;若為-,則為檔案。
第二欄(1):表示link的數目,通常是hard link
第三欄(root):檔案/目錄的擁有者,此例為root
第四欄(root):檔案/目錄所屬群組(group),此例為root的群組
第五欄(9216):檔案大小,此例為9216 bytes
第六欄(Feb 25 15:02):檔案的建立或最後一次異動修改時間
第七欄(test.sh):檔案/目錄名稱,此例檔名為test.sh
第一欄的第一字元表示目錄或檔案,第二字元起,每三個一組,分別代表user(即owner)、group與other的權限,非屬於owner與group的成員,都歸屬於other。若為檔案,r為讀取、w為寫入、x為執行權限;若為目錄,r為讀取、w為寫入(包含建立與刪除檔案)、x為進入該目錄的權限。
---(0):無任何權限
--x(1):僅具執行或進入的權限
-w-(2):僅具寫入(刪除)權限
-wx(3):具進入與寫入或刪除權限
r--(4):僅具讀取權限
r-x(5):具進入或執行和讀取權限
rw-(6):具讀取或寫入(刪除)權限
rwx(7):具完整權限
分享檔案/目錄權限的方式有三,前二種是利用傳統UNIX/Linux的檔案權限方式就可以達成,第三種為比較精細的Access Control List方式,於Microsoft Windows中的檔案權限控制就是採用這種方式,可指定分享的檔案/目錄分享給指定的用戶或群組:
一、檔案/目錄權限全開
利用chmod 777 $file 或chmod 777 $directory,將檔案/目錄權限全部開放,也就是所有人都可以讀、寫,此一分享方式最危險,毫無任何限制,任何人都可以存取變更您的檔案資料,請避免使用此方式。
命令語法:
1.chmod 777 $file 、chmod 777 $directory
2.chmod a+rwx $file或 chmod a+rwx $directory
1與2的語法效果一樣,其中2使用的a為all的意思,其值可為u、g、o與a,分別代表user、group、other與all。
如:
chmod 777 /work/user
chmod a+rwx /work/user
二、對目錄設定setgid
設定目錄setgid bit,可讓所有在此目錄下新建立的檔案/目錄都屬於同一個群組,只要開放該檔案/目錄的群組權限,就可以分享資料
命令語法:
1.chmod 2750 $directory
2.chmod g+s $directory
如:
$ chmod 2750 /path/to/test
$ ls -ld test
drwxr-s--- 2 user nchc 1 Mar 4 15:47 /path/to/test
此一方式適用於屬於同一計畫(群組)的用戶,若有多群組成員共用資料需求,建議使用第三種方式。
三、利用Access Control List(ACL)功能
較為先進的UNIX/UNIX-like作業系統(如:AIX、HP-UX、Solaris、*BSD、Linux..)與檔案系統都有支援ACL,以下簡述其用法:
常用命令名稱:
setfacl 設定/移除檔案/目錄的ACL權限
getfacl 讀取檔案/目錄的ACL權限
1.分享檔案設定範例:
a) userA 設定fileA給userB讀取
userA]$ setfacl -m u:userB:r fileA
原來的fileA權限:
userA]$ ls -l fileA
-rw-r--r-- 1 userA groupA 1547 Sep 22 10:14 fileA
變更後的權限:
userA]$ ls -l fileA
-rw-r--r--+ 1 userA groupA 1547 Sep 22 10:14 fileA
注意:最後面多了"+"的符號,代表此目錄/檔案具ACL屬性。
確認分享後的權限
userA]$ getfacl fileA
# file: fileA
# owner: userA
# group: groupA
user::rw-
user:userB:r--
group::r--
mask::r--
other::r--
b)移除分享給UserB的讀取權限
userA]$setfacl -x u:userB fileA
userA]$ls -l fileA
-rw-r--r--+ 1 userA groupA 1547 Sep 22 10:14 fileA
確認權限是否移除
userA]$getfacl fileA
# file: fileA
# owner: userA
# group: groupA
user::rw-
group::r--
mask::r--
other::r--
c) userA 設定fileA給userB讀取、寫入
userA]$ setfacl -m u:userB:rw- fileA
d) userA 設定fileA給groupB讀取、寫入
userA]$ setfacl -m g:groupB:rw- fileA
e)若要完整移除ACL相關設定,請使用-b的參數
userA]$ setfacl -b fileA
userA]$ ls -l fileA
-rw-r--r-- 1 userA groupA 1547 Sep 22 10:14 fileA
2.目錄分享設定範例
a)userA設定目錄dirA給groupB 讀、寫與進入(rwx)權限
userA]$setfacl -m g:groupB:rwx dirA
原來的權限:
userA]$ls -ld dirA
drwxr-x--- 2 userA groupA 6 Sep 22 10:59 dirA
分享後:
userA]$ls -ld dirA
drwxrwx---+ 2 userA groupA 6 Sep 22 10:59 dirA
確認權限是否正確
userA]$# getfacl dirA
# file: dirA
# owner: userA
# group: groupA
user::rwx
group::r-x
group:groupB:rwx
mask::rwx
other::---
b) userA設定目錄dirA給userB讀、寫與進入(rwx)權限
userA]$setfacl -m u:userB:rwx dirA
c)移除ACL設定
userA]$setfacl -b dirA
userA]$ls -ld dirA
drwxr-x--- 2 userA groupA 6 Sep 22 10:59 dirA
3.更多ACL設定,請man setfacl
4. 請勿分享整個個人家目錄,除了個人資安風險外,亦會造成ssh連線問題。