Linux 檔案umask預設許可權 012

2022-07-07 16:12:17 字數 3981 閱讀 7421

一、umask介紹

linux 系統使用者建立乙個新的目錄或檔案時,系統會缺省會分配相應的許可權。目錄或檔案的許可權是如何產生的呢?

1、這就是umask的功能,umask設定了使用者建立檔案或目錄的預設許可權。linux 系統umask設定的預設許可權屬於安全許可權的臨界點,如果高於這個臨界點,檔案的許可權就太過危險,如果低於這個臨界點,檔案的許可權太過苛刻,操作麻煩。

2、linux系統root系統管理員使用者預設umask值為0022,對應建立目錄預設許可權為755,對應建立檔案預設許可權為644.linux系統其它使用者預設uamsk值為0002, 對應建立目錄預設許可權為775, 對應建立檔案預設許可權為664.

root系統管理員使用者建立檔案和目錄:

[oldgirl@oldboy ~]$ whoami

oldgirl

[root@oldboy ~]# umask

[root@oldboy ~]# mkdir test

[root@oldboy ~]# ls -ld /test/

drwxr-xr-x 2 root root 4096 dec  1 11:47 /test/

[root@oldboy ~]# touch /test/file.txt

[root@oldboy ~]# ls -l /test/file.txt

-rw-r--r-- 2 root root 0 dec  5 09:47 /test/file.txt

test普通使用者建立檔案和目錄:

[test@oldboy ~]$ whoami

test

[test@oldboy ~]$ umask

[test@oldboy ~]$ mkdir hello

[test@oldboy ~]$ ls -ld hello/

drwxrwxr-x 2 test test 4096 dec  5 10:03 hello/

[test@oldboy ~]$ touch testfile.txt

[test@oldboy ~]$ ls -l testfile.txt

-rw-rw-r-- 1 test test 0 dec  5 10:03 testfile.txt

linux 系統umask預設值存在於bashrc系統檔案:

[test@oldboy ~]$ sed -n '65,69p' /etc/bashrc

if [ $uid -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then

umask 002

else

umask 022

filinux 臨時設定umask預設值:(umask預設是四位,第一位是檔案特殊許可權位,可以暫時忽略):

[test@oldboy ~]$ umask 033

[test@oldboy ~]$ umask

二、umask值對應檔案許可權計算方法(以系統管理員root使用者操作)

檔案許可權計算方法

檔案的起始許可權值

umask值

操作

計算後檔案許可權

舉例

666

022  (每位如果都是偶數)

相減範例1

666

033  (每位如果有奇數或偶數)

相減(奇數字相減後在其原奇數字加1)

範例2666

325(每位如果有奇數或偶數)

相減(奇數字相減後在其原奇數字加1)

範例3目錄許可權計算方法

檔案的起始許可權值

umask值

操作

計算後檔案許可權

舉例

777

相減範例1

777

相減範例2

777

相減範例3

範例1:

[root@oldboy ~]# umask

[root@oldboy ~]# mkdir /oldboy

[root@oldboy ~]# touch /oldboy/test.sh

[root@oldboy ~]# ls -ld /oldboy/

drwxr-xr-x 2 root root 4096 dec  5 11:16 /oldboy/

[root@oldboy ~]# ls -l /oldboy/test.sh

-rw-r--r-- 1 root root 0 dec  5 11:16 /oldboy/test.sh

[root@oldboy ~]# stat /oldboy/ | awk -f '[: ]+' 'nr==4 '

(0755/drwxr-xr-x)

[root@oldboy ~]# stat /oldboy/test.sh | awk -f '[: ]+' 'nr==4 '

(0644/-rw-r--r--)

範例2:

[root@oldboy ~]# umask 033

[root@oldboy ~]# umask

[root@oldboy ~]# mkdir /oldboy

[root@oldboy ~]# touch /oldboy/test.sh

[root@oldboy ~]# ls -ld /oldboy/

drwxr--r-- 2 root root 4096 dec  5 11:26 /oldboy/

[root@oldboy ~]# ls -l /oldboy/test.sh

-rw-r--r-- 1 root root 0 dec  5 11:26 /oldboy/test.sh

[root@oldboy ~]# stat /oldboy/ | awk -f '[: ]+' 'nr==4 '

(0744/drwxr--r--)

[root@oldboy ~]# stat /oldboy/test.sh | awk -f '[: ]+' 'nr==4 '

(0644/-rw-r--r--)

範例3:

[root@oldboy ~]# umask 325

[root@oldboy ~]# umask

[root@oldboy ~]# mkdir /oldboy

[root@oldboy ~]# touch /oldboy/test.sh

[root@oldboy ~]# ls -ld /oldboy/

dr--r-x-w- 2 root root 4096 dec  5 11:34 /oldboy/

[root@oldboy ~]# ls -l /oldboy/test.sh

-r--r---w- 1 root root 0 dec  5 11:34 /oldboy/test.sh

[root@oldboy ~]# stat /oldboy/ | awk -f '[: ]+' 'nr==4 '

(0452/dr--r-x-w-)

[root@oldboy ~]# stat /oldboy/test.sh | awk -f '[: ]+' 'nr==4 '

(0442/-r--r---w-)

Linux 檔案預設許可權與umask

大家都知道檔案有屬主屬組和其他許可權rwx,每乙個檔案剛建立的時候都有乙個預設許可權。那麼這個預設許可權是怎麼規定的呢。這就要用到 umask 許可權掩碼。可以用 umask 檢視當前umask值 我們假設是 0022 其中第乙個0表示特殊許可權。022 表示屬主屬組和其他許可權。在linux中檔案...

umask 檔案預設許可權

命令解釋 當你新建乙個檔案或目錄時,會有乙個預設的許可權。umask命令就是 當前使用者在新建檔案或目錄的預設許可權 如下面的例子 wenjie localhost ceshi umask 0022 與一般許可權有關是後面三個數字 wenjie localhost ceshi umask s u r...

檔案預設許可權 umask

umask 目前使用者在新建檔案或目錄時候的許可權預設值,預設為0022 使用 1 nianxiongdi com etc umask 0022 2 nianxiongdi com etc umask s u rwx,g rx,o rx 3 修改umask的值 umask 你所修改的許可權 umas...