普通用户新建的文件和目录的权限为什么和root创建的不一样?
一先看问题现象
[root@localhost ~]# cat /etc/redhat-release CentOS release 6.5 (Final) [root@localhost ~]# uname -rm 2.6.32-431.el6.x86_64 x86_64 [root@localhost ~]# pwd /root [root@localhost ~]# touch root_file [root@localhost ~]# mkdir root_dir [root@localhost ~]# ll -d root* drwxr-xr-x 2 root root 4096 7月 22 08:13 root_dir -rw-r--r-- 1 root root 0 7月 22 08:13 root_file [root@localhost ~]# su - tuser [tuser@localhost ~]$ id uid=1104(tuser) gid=1104(tuser) 组=1104(tuser) [tuser@localhost ~]$ pwd /home/tuser [tuser@localhost ~]$ touch tuser_file [tuser@localhost ~]$ mkdir tuser_dir [tuser@localhost ~]$ ll -d tuser* drwxrwxr-x 2 tuser tuser 4096 7月 22 08:13 tuser_dir -rw-rw-r-- 1 tuser tuser 0 7月 22 08:13 tuser_file [tuser@localhost ~]$
当我们以root用户分别创建路径和文件的时候,其权限为:
路径权限:755
文件权限:644
以普通用户tuser分别创建路径和文件的时候,其权限为:
路径权限:775
文件权限:664
为什么会这样呢?我们的第一反应肯定是root用户是超级用户,而tuser是普通用户,所以他们的权限不一样,创建的文件和路径的权限自然也就不一样了。那么,再接着向下看:
[root@localhost ~]# su - oracle localhost-> pwd /home/oracle localhost-> touch oracle_file localhost-> mkdir oracle_dir localhost-> ll -d oracle* drwxr-xr-x 2 oracle oinstall 4096 Jul 22 08:06 oracle_dir -rw-r--r-- 1 oracle oinstall 0 Jul 22 08:06 oracle_file localhost-> id uid=1101(oracle) gid=1000(oinstall) groups=1000(oinstall),1201(asmdba),1300(dba),1301(oper) localhost->
此时,我们以oracle用户来分别创建文件和路径,看到其权限:
路径权限:755
文件权限:644
明明oracle是个普通用户,其创建的文件和路径的权限却跟root用户创建的文件和路径的权限保持一致,却不像另外一个普通用户tuser创建的文件和路径的权限那样呢?
二分析问题
1关于默认权限
在Unix/Linux系统上,对于任意用户其创建的文件,其默认创建的文件都不希望有执行权限,即希望x权限位为空。为什么?这样,文件的权限位就从”-rwxrwxrwx”,变成了”-rw-rw-rw-“。即从777变成了默认的666权限位。
对于路径而言,我们执行其x权限位表示能否进入该目录有关。如果是x则表示可以查看看路径,rx则表示可以进入该路径,rwx表示可以对该路径下的文件进行读、写操作。显然,对于任意用户其创建的路径,其权限位”-rwxrwxrwx”就被保留了下来,仍然为777。
变成了”-rwxr-xr-x”。即从777变成了默认的755权限位。
2 umask
接着分析,试想,对于任意用户而言,默认情况下,其创建的文件权限为666,是不是有点儿太高了呢?对于owner而言”rw-“的权限是没有问题的当然要可读可写了,用户组group的权限是”rw-“就不太合适了,同用户组的其它用户怎么可以对我创建的文件有写权限w呢?再者,other的权限如果也保留为”rw-“,就更说不过去了。怎么办呢?我们当然是希望新建出来的文件,其权限位是”-rw-r–r–“即644这样的权限,而不是默认的666的权限。
同样,对于任意用户而言,默认情况下对于新建的路径如果权限位是777也太高了。那该怎么办呢?我们希望新建的路径的权限应该是”-rwxr-xr-x”,即owner权限是7,group权限是5,可以进入该路径,other权限也是5,同样可以进入该路径。而不能是默认的777的权限。
于是umask出现了,用于解决新创建的文件或路径的权限的问题。简单理解就是,对于任意用户其创建的文件或路径的权限,就是用默认的666或者777,减去这个umask,就是最终的权限。
我们验证一下:
[root@localhost ~]# umask 0022 [root@localhost ~]# umask -p umask 0022 [root@localhost ~]# umask -S u=rwx,g=rx,o=rx [root@localhost ~]# ll -d root* drwxr-xr-x 2 root root 4096 7月 22 08:13 root_dir -rw-r--r-- 1 root root 0 7月 22 08:13 root_file [root@localhost ~]#
tuser用户:
[tuser@localhost ~]$ id uid=1104(tuser) gid=1104(tuser) 组=1104(tuser) [tuser@localhost ~]$ umask 0002 [tuser@localhost ~]$ ll -d tuser* drwxrwxr-x 2 tuser tuser 4096 7月 22 08:13 tuser_dir -rw-rw-r-- 1 tuser tuser 0 7月 22 08:13 tuser_file [tuser@localhost ~]$
而,oracle用户新建的文件和路径的权限与root用户新建文件、路径一致的原因,是因为oracle用户的umask也是0022。
[root@localhost ~]# id uid=0(root) gid=0(root) 组=0(root) [root@localhost ~]# umask 0022 [root@localhost ~]# su - oracle localhost-> id uid=1101(oracle) gid=1000(oinstall) groups=1000(oinstall),1201(asmdba),1300(dba),1301(oper) localhost-> umask 0022 localhost->
3 为什么root和oracle用户的umask一致呢?
具体可以参考/etc/bashrc配置文件:
# By default, we want umask to get set. This sets it for non-login shell. # Current threshold for system reserved uid/gids is 200 # You could check uidgid reservation validity in # /usr/share/doc/setup-*/uidgid file if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then umask 002 else umask 022 fi
4 为什么root和普通用户的umask不一样呢?
看到root的umask是0022,普通用户的umask是0002。这样一来,root用户创建的文件或者是路径,其权限相对来说就变得更小一些,更安全一些。
5 如何查看和设置umask?
直接执行umask,就能查看。或者umask -S 以Symbol的形式查看。
[root@localhost ~]# umask 0022 [root@localhost ~]# umask -S u=rwx,g=rx,o=rx [root@localhost ~]#
如果想设置umask的话,简单的方法是umask后面跟数值,例如:
[tuser@localhost ~]$ id uid=1104(tuser) gid=1104(tuser) 组=1104(tuser) [tuser@localhost ~]$ umask 0002 [tuser@localhost ~]$ umask 0022 [tuser@localhost ~]$ umask 0022 [tuser@localhost ~]$
6 umask前面的第1个0表示什么?
这个是给文件的特殊权限位:SUID,SGID,SBIT用的。
下一篇文章里再分析和说明它。
Linux上文件特殊权限位SUID,SGID,SBIT的使用说明
三 小结和说明
- Unix、Linux系统上,文件的默认权限是666,路径的权限是777;这个默认权限太高了,不够安全;
- 搞清楚了Unix、Linux系统上,任意用户创建的文件(含路径)的权限是怎么来的;
- 学会了如何查看、修改umask;
wikipedia上关于umask的说明:
https://en.wikipedia.org/wiki/Umask
In computing,
umaskis a command that determines the settings of a mask that controls how file permissions are set for newly created files. It may also affect how the file permissions are changed explicitly.umaskis also a function that sets the mask, or it may refer to the mask itself, which is formally known as the file mode creation mask. The mask is a grouping of bits, each of which restricts how its corresponding permission is set for newly created files. The bits in the mask may be changed by invoking theumaskcommand.



一条评论
Pingback: