Linux File Permission

一、Permission Mode

首先,File Permission Mode 是存在 inode 中的信息, 一共有 12 个 bit (0与1):0 是关闭、1是打开。
其中每一个 bit 所代表的意思分别如下: 
      suid, sgid, sticky_bit | user_read, user_write, user_execute | group_read, group_write, group_execute | others_read, others_write, others_execute。

举例: 100111101101 (binary mode)

但在系统应用及管理上,我们一般不是采用 binary mode(二进制),而是使用 octal mode(八进位) 或 text mode(文字) 。其中的 octal mode 就是单纯将 binary 换算过来就可以了。
你可试试 linux 的 bc 命令来算一算:

$ echo "obase=8;ibase=2;100111101101" | bc
结果:4755

 

二、ugo

利用 ls -l 命令,可获得每一个文件的如下信息:
     file_type&permission_mode, link_count, user, group, size, modification_time, file_name

我们可以从最左边的字段看到十个字母, 第一个为: file type 其余九个为 permission mode ,又分为三组:user, gorup, others 。
对每一个使用者来说,ugo 这三组只能选其一:
  * 若 uid 与 file uid 一致,则取 user 而忽略 group & others 。
  * 若 uid 与 file uid 不一致,则再看 gid(s) 若与 file gid 一致。取 group 而忽略 user & others 。
  * 若 uid 与 gid 与 file 都不一致,则取 others 而忽略 user & group。
注:你可在任何时候输入 id 这个命令得知你所使用的 uid 与 gid(s)

 

三、rwx

一旦使用者所参考的 ugo permission 能确定下来之后,接下来就来看其取的的 permission 代表的意思了。

事实上rwx 对下面两种 file type 来说是不一样的:
* 以 - 开头为 file:
        r: 可读取该档的内容
        w: 可修改该档的内容
        x: 可执行该档(executable)并产生 process
* 以 d 开头为 directory:
        r: 可读取该目录底下的内容(即列出 file 及 sub dir)
        w: 可修改该目录底下的内容(即增减 file 及 sub dir)
        x: 可进入该目录底下(即 access ,如门禁卡一般)

其中 directory 的 x 最不好理解:
就算你对 dir 有 rw ,若没 x 的话,那也跑不进去做事情,就好像你是自己套房的主人,却进不了公寓大门一样。

然而,当你对一个 directory 有了 rwx 权限之后,里面或许有些 file/dir 你是没有 rwx 权限的,就好比套房里放了一些不属于你的物品。但,你却可以将之仍进垃圾桶,因为你在套房:
* 你没办法打开一看究竟(没 r)
* 你没办法偷龙转凤动手脚(没 w)
* 你只能干瞪眼却无福消受(没 x)
* 可以看到所有物品(r)
* 可以搬进搬出或 DIY 物品(w)
* 你有钥匙入门(x)

 

四、suid, sgid, sticky bit

SUID/SGID 对 executable file 而言,会影响所产生的 process 用哪个 uid/gid:
    若有设,从 file 本身的 uid/gid 继承。
    若没设,从 parrent process 继承(通常是 shell)。

sticky_bit 在现代的 Linux 系统中,对 file 来说是没任何意意的;SUID 对 directory 没啥意义。

不过, SGID 对 directory 来说,会影响该目录下新建的 file/dir 的 gid :
    若有设,gid 从 dir 继承(若是 new dir,其 SGID 也会继承)。
    若没设,那 gid 从产生 new file/dir 的 process 继承。

至于 directory 的 sticky bit ,则起到保护文件的作用:
    若没有设,使用者只要对 dir 有 w permission ,就能删除 dir 里的文件(请参考前述)。
    若有设,使用者除了要有 w permsissin 之外,还需符合如下两个条件之一:
        * 使用者之 uid 必须与文件所在目录一致,或:
        * 使用者之 uid 必须被删文件一致。

 

五、umask

umask 的作用,就是在新建文件或目录时,需要取消的 permission 。
其运算法则为:
new dir: (NOT umask) AND 0777 (000,111,111,111)
new file: (NOT umask) AND 0666 (000,110,110,110)

 

转自:http://blog.csdn.net/pinkoo/article/details/87506

posted @ 2013-04-12 23:09  Wiliz  阅读(584)  评论(0编辑  收藏  举报