原址: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:
- The default umask 002 used for normal user. With this mask default directory permissions are 775 and default file permissions are 664.
- The default umask for the root user is 022 result into default directory permissions are 755 and default file permissions are 644.
- For directories, the base permissions are (rwxrwxrwx) 0777 and for files they are 0666 (rw-rw-rw).
In short,
- A umask of 022 allows only you to write data, but anyone can read data.
- 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.
- 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:
- r : read
- w : write
- x : execute
- u : User ownership (user who owns the file)
- g : group ownership (the permissions granted to other users who are members of the file's group)
- 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
- The umask command can restricts permissions.
- 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.youkuaiyun.com/wallwind/article/details/6891618