關于Linux文件默認權限的問題,可以實際先嘗試一下如下命令:
root用戶登錄
[root@localhost test]# touch file1
[root@localhost test]# ls-l file1
-rw-r--r-- 1 root root 0 May? 5 08:28 file1 #輸出結果 對應的數字權限 644
[root@localhost test]# touch file2
[root@localhost test]# ls-l file2
-rw-r--r-- 1 root root 0 May? 5 08:29 file2 #輸出結果 對應的數字權限 644
[root@localhost test]# mkdir dir1
[root@localhost test]# ls-ld dir1
drwxr-xr-x 2 root root 4096 May? 5 08:29 dir1 #輸出結果 對應的數字權限 755
[root@localhost test]# mkdir dir2
[root@localhost tmp]# ls-ld dir2
drwxr-xr-x 2 root root 4096 May? 5 08:29 dir2 #輸出結果 對應的數字權限 755
user1用戶登錄
[user1@localhost test]# touch file1
[user1@localhost test]# ls-l file1
-rw-rw-r-- 1 root root 0 May? 5 08:28 file1 #輸出結果 對應的數字權限 664
[user1@localhost test]# touch file2
[user1@localhost test]# ls-l file2
-rw-rw-r-- 1 root root 0 May? 5 08:29 file2 #輸出結果 對應的數字權限 664
[user1@localhost test]# mkdir dir1
[user1@localhost test]# ls-ld dir1
drwxrwxr-x 2 root root 4096 May? 5 08:29 dir1 #輸出結果 對應的數字權限 775
[user1@localhost test]# mkdir dir2
[user1@localhost tmp]# ls-ld dir2
drwxrwxr-x 2 root root 4096 May? 5 08:29 dir2 #輸出結果 對應的數字權限 775
通過上面的執行結果可以得出以下結論:如果是root用戶創建的文件默認權限是644,目錄默認權限是755;普通用戶創建的文件默認權限是664,目錄默認權限是775.兩者的默認權限是不同的,造成兩者用戶權限不同的原因就是Linux針對不同的用戶創建文件和創建目錄默認的權限不同,Linux系統通過umask(遮罩)的概念來控制相應的權限。可以在/etc/profile 文件中進行查看。
內容如下(51-55行):
if [ $UID-gt 99 ] && [ "`id-gn`" = "`id-un`" ]; then
? ? umask 002
else
? ? umask 022
fi
通過上面的文件內容可以看出:如果UID>99 設置的umask值為002,如果UID不大于99則umask值為022.關于遮罩計算權限的方式如下:比如 777 用字符串表示 rxwrwxrwx,如果遮罩值是022 對于的字符串是 ----w--w-,計算方法是如果遮著包含字母的,計算出真正的權限就不包含該位置的字母用-代替即可,個人理解公式:遮罩值+計算的真正權限=rxwrwxrwx
上面的權限可以這樣理解: ----w--w-+rxwr-xr-x=rxwrwxrwx