1. 程式人生 > >linux許可權及ntfs檔案系統許可權的知識

linux許可權及ntfs檔案系統許可權的知識

關於ntfs許可權的問題

檔案的許可權:

[-dcbps][u:rwx][g:rwx][a:rwx] 
其中: r=4, w=2, x=1,  u=owner, g=group, a=all user
           d=dir, -=file, l=symbolic link, p=pipe, 

           c=character device, b=block device,  d=door, s = socket

linux下,目錄的r——可列目錄,w——可寫/刪/改名,x——可進入訪問;

                     檔案r——可讀,w——可寫/刪/改名,x——執行

許可權的組合可匯合成一個數字: rwx = 4+2+1 = 7

因此:-rwxrwxrwx = 777, -rw-rw-rw=666, -rwx-r-x-r-x=755

一般通過chmod的引數進行設定:

chmod 777 /dir/file 設定檔案為讀寫執行
chmod -x /dir/file 刪除檔案uga的可執行
chmod ga-w /dir/file 刪除檔案ga的可寫許可權
chmod u=rx /dir/file 重設定檔案u為讀和執行
chmod +x /dir/file 增加檔案uga為可執行

umask & fmask & dmask的使用

umask —— 設定目錄和檔案的許可權過濾
fmask —— 設定檔案的許可權過濾
dmask —— 設定目錄的許可權過濾
dmask和fmask是mount的選項,針對fat/ntfs檔案系統,適用於fstab配置
不同於chmod/chown的許可權值,它們三個是有mask——過濾的意思 ,以下是它們的對檔案的讀寫許可權:
    0   1   2   3   4   5   6   7
r   +   +   +   +   -   -   -   -
w   +   +   -   -   +   +   -   -
x   +   -   +   -   +   -   +   -

其實這個結果是通過 mask = rwx - 檔案許可權

如設定檔案為0755許可權,那麼mask值則需為0022,即:0755=0777-0022

fstab例項

<file system> <mount point> <type>         <options>       <dump> <pass>
/dev/hda1      /media/win    ntfs  defaults,utf8,umask=111   0      0

其中:umask=111==>(777-111)=666=rw-rw-rw, 即檔案擁有讀寫許可權

可以重新設計更更嚴格的許可權關係:

           dmask=022,fmask=133 即:f=755=rwxr-xr-x, d=644=rw-r--r--

注意:其實umask可理解為關閉某些許可權。可以使用umask命令改變一個檔案的許可權:
umask 檢視當前目錄的許可權mask
umask <mask> 設定當前

檔案許可權進階—— 檔案的組和使用者繼承關係suid和sgid

當檔案設定了suid後,該檔案執行時以擁有者身份執行
chmod 755 file (owner)
chmod u+s file ==> -rwsr-xr-x (user)
(即當使用user執行時,它以owner的身份執行)
(suid常用於檔案上,目錄一般沒有執行許可權)

當目錄設定了sgid後,其他人要是有r/x/w許可權時,其他人建立的子目錄的組為當前擁有的組

chmod 757 dir (owner)
chmod g+s dir ==> drwxr-srwx (ower)
mkdir dir/newidr (user)
(即當user建立子目錄時,它的組是owner,它的擁有者則是user)
(sgid常用於目錄上)

當目錄設定了sticky後,防止別人刪除目錄的資料

chmod 757 dir (owner組)
chmod o+t dir ==> drwxr-srwt (owner)
rm -r dir (user) ==> error
(user無法刪除,雖然開放了刪除許可權,但還是隻有owner可刪除)

例子:

chmod  u=rwxs,o=rx   file
chmod  g+s,o=wrx    test/
chmod  o=rwxt   test/ 
chmod 1775 test/

0755也就是755, 而1755前面的1則與suid/sgid/sticky相關,看下錶:

(可以理解為suid=4,sgid=2,sticky=1)
	suid	sgid	sticky	模式數字
	on	on	on	7
	on	on	off	6
	on	off	on	5
	on	off	off	4
	off	on	on	3
	off	on	off	2
	off	off	on	1
	off	off	off	0

檔案的擁有者

一般通過chown進行設定
檢視當前登入	w或者who
檢視當前使用者名稱 	whoami
檢視當前使用者組id  id <u>  或者 finger <u>
檢視使用者登入記錄  last
		lastb
檢視所有使用者	cut -d : -f 1 /etc/passwd
		cat /etc/passwd |awk -F \: '{print $1}'
檢視當前組   	groups
檢視指定組   	groups 
改變擁有者   	chown  /dir/file
改變組		chgrp  /dir/file
改變組及擁有者	chown : /dir/file
其他		groupadd/groupmod/groupdel
		useradd/usermod/userdel

最後進階理解fstab配置

<file system> <mount point> <type>                    <options>               <dump> <pass>
/dev/hda1 /media/win ntfs defaults,utf8,uid=1000,gid=1000,fmask=133,dmask=022 0 0

參考文章