umask指令用來設定限制建立檔案權限的掩碼。當新檔案被建立時,其最初的權限由檔案建立掩碼決定。使用者每次注冊進入系統時,umask指令都被執行, 并自動設定掩碼mode來限制新檔案的權限。
文法
umask(選項)(參數)
選項
-p:輸出的權限掩碼可直接作為指令來執行;
-S:以符号方式輸出權限掩碼。
作用;
控制建立的檔案或目錄的權限
系統使用者:#umask 022
普通使用者:#umask 002
umask值與建立檔案、目錄權限對照表
umask值
目錄權限值
檔案權限值
7
6
1
6
6
2
5
4
3
4
4
4
3
2
5
2
2
6
1
7
例:
設定系統的權限掩碼為444[[email protected] ~]#umask 444
以符号的格式設定umask:使用者所有者讀取、組群所有者寫入、其他使用者讀取[[email protected] ~]#umask u=r,g=w,o=r
檢視系統目前的umask設定[[email protected] ~]#umask
0353以符号的格式顯示目前的umask設定
[[email protected] ~]#umask -S
u=r,g=w,o=r
計算方法:
檔案預設權限=666-umask值 666-022=644
目錄預設權限=777-umask 值 777-022=755
#這是一個好的記憶方法,但不嚴謹。
/etc/bashrc
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then # id -gn顯示組名,id -un 顯示使用者名
umask 002 #普通使用者
else
umask 022 #系統使用者
fi
特殊權限:
SUID SGID Stickybit
s對應的數值為:u 4,g 2,t 1
SUID:
限定:隻能設定在二進制可執行程式上面。對目錄文本設定無效。
功能:程式運作時的權限從執行者變更成程式所有者。
[[email protected] ~]# ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 30768 Feb 17 2012 /usr/bin/passwd
[[email protected] ~]# useradd canxue
[[email protected] ~]# su - canxue
[[email protected] ~]$ less /etc/shadow
/etc/shadow: 權限不夠
解決方法
[[email protected] ~]# ll /usr/bin/less
-rwxr-xr-x 1 root root 154416 Sep 26 2011 /usr/bin/less
[[email protected] ~]# chmod u+s /usr/bin/less
[[email protected] ~]# ll /usr/bin/less
-rwsr-xr-x 1 root root 154416 Sep 26 2011 /usr/bin/less
驗證
[[email protected] ~]$ less /etc/shadow 成功讀取
注意:
[[email protected] ~]# chmod 4755 /usr/bin/less 等同于
[[email protected] ~]# chmod u+s /usr/bin/less
SGID:
限定:既可以給二進制可執行程式設定,也可以給目錄設定。
功能:在設定了SGID權限的目錄下建立檔案時,新建立的檔案的所屬組會繼承上級目錄的所屬組
[[email protected] ~]# mkdir sws
[[email protected] ~]# chmod g+s sws/
[[email protected] ~]# cd sws/
[[email protected] sws]# touch chuanshuo
[[email protected] sws]# ll chuanshuo
-rw-r--r-- 1 root canxue 0 Oct 13 22:05 chuanshuo
[[email protected] ~]# chmod 2755 sws/
[[email protected] ~]# ll -d sws/
drwxr-sr-x 2 canxue canxue 4096 Oct 13 22:05 sws/
Stickybit
限定:隻作用于目錄
功能:目錄下建立的檔案隻有root、檔案建立者、目錄所有者才能删除
[[email protected] ~]# chmod o+t /tmp/
[[email protected] ~]# ll -d /tmp/
drwxrwxrwt. 14 root root 4096 Oct 13 21:44 /tmp/
[[email protected] tmp]$ touch canxue.txt
[[email protected] tmp]$ chmod 777 canxue.txt
[[email protected] tmp]$ rm -rf canxue.txt
rm: 無法删除"canxue.txt": 不允許的操作