Detailed explanation of LINUX UMASK

A permission mask umask

umask is matched with chmod, with a total of 4 bits (gid/uid, owner, group authority, permissions of other users), but usually the last 3 are used, for example, you use chmod 755 file (the permissions of this file at this time) Is the owner read (4) + write (2) + execute (1), the same group and other users have read and write permissions)

The role of two umask

The default umask value is 022 (you can use the umask command to view it), the default permission of the file you create is 644 (6-0, 6-2, 6-2), and the default permission of the created directory is 755 (7 -0,7-2,7-2), you can use ls -l to verify it. Now you should know the purpose of umask. It is to control the default permissions, not to make the default files and directories have full rights.

Three modify the umask value

After knowing the function of umask, you can modify the value of umask, for example: umask 024, the default permissions of files and directories created later are 642,753

4. Save the umask value to the environment file

If you want to modify the value of umask for a long time, you can write it into /etc/profile or ~/.profile or ~/.bash_profile. You should know what these files are used for.

Do you know what the default properties will be when you create a new file or directory? Ha ha! Then it has something to do with umask! So what is umask doing? Basically, umask is to specify "the current user's attribute default value when creating a file or directory", so how to know or set umask? His specified conditions are specified in the following way: Syntax:

[root @test root]# umask
0022
[root@vbird test]# umask 002 <== followed by 3 numbers!
[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) 这就是文件创建缺省权限

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326285477&siteId=291194637