linux下的umask命令

原址:http://hi.baidu.com/fengyun409/item/82cd158ffe7f67c8b17154e7

現在,我們知道如何建立或者是改變一個目錄或文件的屬性了,不過,你知道,當建立一個新的文件或目錄時,它的默認屬性是什麼嗎?那就與umask有關了。那麼,umask是什麼呢?基本上,umask就是指定“當前用戶在建立文件或目錄時候的屬性默認值”。那麼,如何得知或設置umask呢?它以下面的方式來指定條件: [root@linux ~]# umask
0022
[root@linux ~]# umask -S
u=rwx,g=rx,o=rx 查看方式有兩種,一種是直接輸入umask,可以看到數字類型的權限設置分數,一種是加入 -S(Symbolic)參數,就會以符號類型的方式顯示權限。奇怪的是,怎麼umask會有4組數字啊?不是隻有3組嗎?沒錯。第一組是特殊權限用的,我們先不要理它,所以先看後面3組即可。
在默認權限的屬性上,目錄與文件是不一樣的。由於我們不希望文件具有可執行的權力,默認情況中,文件是沒有可執行(x)權限的。因此:
• 若用戶建立爲”文件”則默認“沒有可執行(x)項目”,即只有rw這兩個項目,也就是最大爲666分,默認屬性如下:
-rw-rw-rw-
• 若用戶建立爲”目錄”,則由於x與是否可以進入此目錄有關,因此默認爲所有權限均開放,即爲777分,默認屬性如下:
drwxrwxrwx
umask指定的是“該默認值需要減掉的權限”。因爲r、w、x分別是4、2、1,所以。也就是說,當要去掉能寫的權限,就是輸入2,而如果要去掉能讀的權限,也就是4,那麼要去掉讀與寫的權限,也就是6,而要去掉執行與寫入的權限,也就是3。請問,5是什麼?就是讀與執行的權限。如果以上面的例子來說明的話,因爲umask爲022,所以user並沒有被去掉屬性,不過group與others的屬性被去掉了2(也就是w這個屬性),那麼當用戶:
• 建立文件時:(-rw-rw-rw-) – (-----w--w-) ==> -rw-r--r--
• 建立目錄時:(drwxrwxrwx) – (d----w--w-) ==> drwxr-xr-x
我們來測試看看。 [root@linux ~]# umask
0022
[root@linux ~]# touch test1
[root@linux ~]# mkdir test2
[root@linux ~]# ll
-rw-r--r-- 1 root root    0 Jul 20 00:36 test1
drwxr-xr-x 2 root root 4096 Jul 20 00:36 test2 看見了吧?確定屬性是沒有錯的。假如要讓與用戶同用戶組的人也可以存取文件呢?也就是說,假如dmtsai是users用戶組的人,而dmtsai做的文件希望讓users同用戶組的人也可以存取,這也是在團隊開發計劃時常常會考慮到的權限問題。在這樣的情況下,umask自然不能取消group的w權限,也就是說,我們希望製作出來的文件應該是 -rw-rw-r-- 的模樣,所以,umask應該是002纔好(僅去掉others的w權限)。如何設置umask呢?簡單得很,直接在umask後面輸入002。 [root@linux ~]# umask 002
[root@linux ~]# touch test3
[root@linux ~]# mkdir test4
[root@linux ~]# ll
-rw-rw-r-- 1 root root    0 Jul 20 00:41 test3
drwxrwxr-x 2 root root 4096 Jul 20 00:41 test4 所以,這個umask對於文件與目錄的默認權限是很有關係的。這個概念可以用在任何服務器上,尤其是將來在架設文件服務器(file server),比如SAMBA服務器或FTP服務器時,都很重要。這牽涉到用戶是否能夠將文件進一步利用的問題。不要等閒視之。
例題四:假設umask爲003,請問使用該umask,建立的文件與目錄權
限是什麼?
答:
umask爲003,所以去掉的屬性爲 --------wx,因此:
文件:(-rw-rw-rw-) - (--------wx) = -rw-rw-r--
目錄:(drwxrwxrwx) - (--------wx) = drwxrwxr--

提示:有關umask與權限的計算方式中,教科書喜歡使用二進制方式來進行AND與NOT計算,不過,筆者比較喜歡使用符號方式計算,聯想比較容易。但是,有的書籍或BBS上,有人喜歡使用文件默認屬性666與目錄默認屬性777來與umask進行相減計算。這是不好的。以例題4來看,如果使用默認屬性相加減,則文件變成:666-003=663,即 -rw-rw--wx,這是完全不對的。想想看,原本文件就已經去除x的默認屬性了,怎麼可能突然間冒出來了?所以,這個地方要特別小心。

在默認的情況中,root的umask會去掉比較多的屬性,root的umask默認是022,這是基於安全的考慮。一般身份用戶通常的umask爲002,即保留同用戶組的寫入權力。其實,關於默認umask的設置可以參考 /etc/bashrc文件的內容,不過,建議不要修改該文件,可以參考第11章提到的環境參數設置文件(~/.bashrc)的說明。


原址:http://1123697506.blog.51cto.com/3783048/882064

一 權限掩碼umask


umask是chmod配套的,總共爲4位(gid/uid,屬主,組權,其它用戶的權限),不過通常用到的是後3個,例如你用chmod 755 file(此時這文件的權限是屬主讀(4)+寫(2)+執行(1),同組的和其它用戶有讀寫權限)

二 umask的作用

默認情況下的umask值是022(可以用umask命令查看),此時你建立的文件默認權限是644(6-0,6-2,6-2),建立的目錄的默認權限是755(7-0,7-2,7-2),可以用ls -l驗證一下哦 現在應該知道umask的用途了吧,它是爲了控制默認權限,不要使默認的文件和目錄具有全權而設的
三 修改umask值

知道了umask的作用後,你可以修改umask的值了,例如:umask 024則以後建立的文件和目錄的默認權限就爲642,753了

四 將umask值保存到環境文件

若要長期修改umask的值,可以把它寫進/etc/profile或~/.profile或~/.bash_profile中,大家應該知道這幾個文件是幹什麼用的吧。

您知道當你建立一個新的檔案或目錄時,他的預設屬性會是什麼嗎?呵呵!那就與 umask 有關了!那麼 umask 是在搞什麼呢?基本上, umask 就是指定『目前使用者在建立檔案或目錄時候的屬性默認值』,那麼如何得知或設定 umask 呢?他的指定條件以底下的方式來指定:語法:

[root @test root]# umask
0022
[root@vbird test]# umask 002 <==後面接 3 個數字!
[root@vbird test]# umask
0002

說明:查看 umask 數值爲直接輸入 umask 即可,而設定呢?沒錯!就是 umask 之後接三個數字!那麼如何來指定呢?主要還是跟 Linux 的檔案屬性(那九個屬性, r, w, x )有關的,而且是以分數的那一個關係爲例的,而有底下的規則爲輔:

若使用者建立爲『檔案』則預設『沒有可執行 ( x ) 項目』,亦即只有 rw 這兩個項目,也就是最大爲 666 分

–rw-rw-rw-

若使用者建立爲『目錄』,則由於 x 與是否可以進入此目錄有關,因此預設爲所有權限均開放,亦即爲 777 分

drwxrwxrwx
 
那麼 umask 指定的是『該默認值需要減掉的權限!』因爲 r、w、x 分別是 4、2、1 分,所以囉!也就是說,當要拿掉能寫的權限,就是輸入 2 分,而如果要拿掉能讀的權限,也就是 4 分,那麼要拿掉讀與寫的權限,也就是 6 分,而要拿掉執行與寫入的權限,也就是 3 分,這樣瞭解嗎?請問您, 5 分是什麼?呵呵!就是讀與執行的權限啦!如果以上面的例子來說明的話,因爲 umask 爲 002 ,所以 user, group 並沒有被拿掉屬性,不過 others 的屬性被拿掉了 2 ( 也就是 w 這個屬性 ),那麼由於當使用者:
 
建立檔案時:(-rw-rw-rw-) – (——–w-) ==> -rw-rw-r–
建立目錄時:(drwxrwxrwx) – (——–w-) ==> drwxrwxr-x
 
不相信嗎?你只要使用 touch test 然後看看這個 test 的檔案屬性,就可以知道了!那麼如何看你這個使用者目前的 umask呢?直接下達 umask 即可!實作看看先:
 
[root@vbird test]# umask
0002
[root@vbird test]# touch test1
[root@vbird test]# mkdir test2
[root@vbird test]# ls -l
-rw-rw-r– 1 root root 0 Oct 22 00:00 test1
drwxrwxr-x 2 root root 4096 Oct 22 00:00 test2/

發現了什麼?呵呵! Test1 的屬性爲 666-002 = 664 !正確嗎?是的!正確!而 test2 這個目錄呢?就是 777-002 = 775 !也正確!

[root@vbird test]# umask 003
[root@vbird test]# touch test3
[root@vbird test]# mkdir test4
[root@vbird test]# ll
-rw-rw-r– 1 root root 0 Oct 22 00:03 test3
drwxrwxr– 2 root root 4096 Oct 22 00:03 test4/

嘿!屬性又跟剛剛的不一樣囉!仔細推敲一下爲什麼呦!test3 666-003 =663,這是怎麼一回事?! 663 應該是 -rw-rw–wx 纔對啊!怎麼會是上面的屬性!呵呵!這裏就要特別的給他強調了!『儘量不要以數字相加減啦!』容易造成類似上面的問題!你應該要這樣想(-rw-rw- rw-) – (——–wx)=-rw-rw-r–這樣就對啦!瞭解了嗎?不要用十進制的數字喔!夠能力的話,用二進制來算,不曉得的話,用 rwx 來算喔!

由上面的例子您應該很輕易的就可以發現 umask 的用途!而這個 umask 可以在 /etc/bashrc 裏面進行修改喔!預設的情況之下, root 的 umask 爲 022 而一般使用者則爲 002 ,因爲可寫的權限蠻嚴重的,因此預設都會拿掉這個權限!此外,因爲 root 比較重要!所以爲了安全的需求,其同羣組的寫入屬性就被拿掉了!這東西對於安全性也有一定程度的貢獻呦!

例如,對於u m a s k值0 0 2,相應的文件和目錄缺省創建權限是什麼呢?
第一步,我們首先寫下具有全部權限的模式,即7 7 7 (所有用戶都具有讀、寫和執行權限)。
第二步,在下面一行按照u m a s k值寫下相應的位,在本例中是0 0 2。
第三步,在接下來的一行中記下上面兩行中沒有匹配的位。這就是目錄的缺省創建權限。
稍加練習就能夠記住這種方法。
第四步,對於文件來說,在創建時不能具有文件權限,只要拿掉相應的執行權限比特即
可。
這就是上面的例子,其中u m a s k值爲0 0 2:
1) 文件的最大權限rwx rwx rwx (777)
2) umask值爲0 0 2 – - – - – - -w-
3) 目錄權限rwx rwx r-x (775) 這就是目錄創建缺省權限
4) 文件權限rw- rw- r– (664) 這就是文件創建缺省權限
下面是另外一個例子,假設這次u m a s k值爲0 2 2:
1) 文件的最大權限rwx rwx rwx (777)
2 ) u m a s k值爲0 2 2 – - – -w- -w-
3) 目錄權限rwx r-x r-x (755) 這就是目錄創建缺省權限
4) 文件權限rw- r– r– (644) 這就是文件創建缺省權限
 


原址:http://www.cyberciti.biz/tips/understanding-linux-unix-umask-value-usage.html

Anil ask a question (via email):

What is umask and how is it determined on a Linux system?


When user create a file or directory under Linux or UNIX, she create it with a default set of permissions. In most case the system defaults may be open or relaxed for file sharing purpose. For example, if a text file has 666 permissions, it grants read and write permission to everyone. Similarly a directory with 777 permissions, grants read, write, and execute permission to everyone.

Default umask Value

The user file-creation mode mask (umask) is use to determine the file permission for newly created files. It can be used to control thedefault file permission for new files. It is a four-digit octal number. A umask can be set or expressed using:

  • Symbolic values
  • Octal values

Procedure To Setup Default umask

You can setup umask in /etc/bashrc or/etc/profile file for all users. By default most Linux distro set it to 0022 (022) or 0002 (002). Open /etc/profile or ~/.bashrc file, enter:
# vi /etc/profile
OR
$ vi ~/.bashrc
Append/modify following line to setup a new umask:
umask 022
Save and close the file. Changes will take effect after next login. All UNIX users can override the system umask defaults in their /etc/profile file, ~/.profile (Korn / Bourne shell) ~/.cshrc file (C shells), ~/.bash_profile (Bash shell) or ~/.login file (defines the user's environment at login).

Explain Octal umask Mode 022 And 002

As I said earlier, if the default settings are not changed, files are created with the access mode 666 and directories with 777. In this example:

  1. The default umask 002 used for normal user. With this mask default directory permissions are 775 and default file permissions are 664.
  2. The default umask for the root user is 022 result into default directory permissions are 755 and default file permissions are 644.
  3. For directories, the base permissions are (rwxrwxrwx) 0777 and for files they are 0666 (rw-rw-rw).

In short,

  1. A umask of 022 allows only you to write data, but anyone can read data.
  2. A umask of 077 is good for a completely private system. No other user can read or write your data if umask is set to 077.
  3. A umask of 002 is good when you share data with other users in the same group. Members of your group can create and modify data files; those outside your group can read data file, but cannot modify it. Set your umask to007 to completely exclude users who are not group members.

But, How Do I Calculate umasks?

The octal umasks are calculated via the bitwise AND of the unary complement of the argument using bitwise NOT. The octal notations are as follows:

  • Octal value : Permission
  • 0 : read, write and execute
  • 1 : read and write
  • 2 : read and execute
  • 3 : read only
  • 4 : write and execute
  • 5 : write only
  • 6 : execute only
  • 7 : no permissions

Now, you can use above table to calculate file permission. For example, if umask is set to 077, the permission can be calculated as follows:

Bit Targeted at File permission
0 Owner read, write and execute
7 Group No permissions
7 Others No permissions

To set the umask 077 type the following command at shell prompt:
$ umask 077
$ mkdir dir1
$ touch file
$ ls -ld dir1 file

Sample outputs:

drwx------ 2 vivek vivek 4096 2011-03-04 02:05 dir1
-rw------- 1 vivek vivek    0 2011-03-04 02:05 file

Task: Calculating The Final Permission For FILES

You can simply subtract the umask from the base permissions to determine the final permission for file as follows:
666 - 022 = 644

  • File base permissions : 666
  • umask value : 022
  • subtract to get permissions of new file (666-022) : 644 (rw-r--r--)

Task: Calculating The Final Permission For DIRECTORIES

You can simply subtract the umask from the base permissions to determine the final permission for directory as follows:
777 - 022 = 755

  • Directory base permissions : 777
  • umask value : 022
  • Subtract to get permissions of new directory (777-022) : 755 (rwxr-xr-x)

How Do I Set umask Using Symbolic Values?

The following symbolic values are used:

  1. r : read
  2. w : write
  3. x : execute
  4. u : User ownership (user who owns the file)
  5. g : group ownership (the permissions granted to other users who are members of the file's group)
  6. o : other ownership (the permissions granted to users that are in neither of the two preceding categories)

The following command will set umask to 077 i.e. a umask set to u=rwx,g=,o= will result in new files having the modes -rw-------, and new directories having the modes drwx------:
$ umask u=rwx,g=,o=
$ mkdir dir2
$ touch file2
$ ls -ld dir2 file2

Sample umask Values and File Creation Permissions

If umask value set to User permission Group permission Others permission
000 all all all
007 all all none
027 all read / execute none

all = read, write and executable file permission

Limitations of the umask

  1. The umask command can restricts permissions.
  2. The umask command cannot grant extra permissions beyond what is specified by the program that creates the file or directory. If you need to make permission changes to existing file use the chmod command.

umask and level of security

The umask command be used for setting different security levels as follows:

umask value Security level Effective permission (directory)
022 Permissive 755
026 Moderate 751
027 Moderate 750
077 Severe 700
For more information about the umask read the man page of bash or ksh or tcsh shell:
man bash
help umask
man chmod



還有一個可以參考的網址:http://blog.csdn.net/wallwind/article/details/6891618

發佈了15 篇原創文章 · 獲贊 3 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章