Chinaunix首页 | 论坛 | 博客
  • 博客访问: 353805
  • 博文数量: 83
  • 博客积分: 5322
  • 博客等级: 中校
  • 技术积分: 1057
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-11 11:27
个人简介

爱生活,爱阅读

文章分类

全部博文(83)

文章存档

2015年(1)

2013年(1)

2012年(80)

2011年(1)

分类: LINUX

2012-03-31 22:41:01

主要内容:
     1、文件/目录权限
     2、特殊文件权限
 
    之前总结中关于Fedora下用户用户组管理写了几篇总结,对于文件/目录类型、权限类型简单提及,一些特殊权限没有总结。而其中的SUID、SGID等与进程等Linux下核心概念紧密联系,这里再把文件权限给翻弄出来,就当是提前过“节了吧! 

一、文件/目录权限

     Linux下对文件、目录的访问时基于用户、用户组权限的。建立一个文件的同时已分别指定:文件拥有者(USER)、文件组(GROUP)、其他组(OTHERS)三类用户的操作权限。这三者涉及到用户管理内容,可以参考blog中,这里不再赘述。
    在Linux下,可通过ls -l命令查询文件的属性。如下:

点击(此处)折叠或打开

  1. [root@localhost admin]# ll /usr/bin | egrep "^-..s......"
  2. -rwsr-xr-x 1 root root 45380 2007-03-27 21:49 at
  3. -rwsr-xr-x 1 root root 46844 2007-04-10 18:17 chage
  4. -rws--x--x 1 root root 17964 2007-10-08 21:49 chfn
  5. -rws--x--x 1 root root 19224 2007-10-08 21:49 chsh
  6. -rwsr-sr-x 1 root root 315384 2007-04-13 00:50 crontab
  7. -rwsr-xr-- 1 root fuse 22720 2007-05-12 22:05 fusermount
  8. -rwsr-xr-x 1 root root 47352 2007-04-10 18:17 gpasswd
  9. -rwsr-xr-x 1 root root 24556 2007-04-10 18:17 newgrp
  10. -rwsr-xr-x 1 root root 25604 2007-04-05 16:54 passwd
  11. -rwsr-xr-x 1 root root 18640 2007-04-12 01:02 rcp
  12. -rwsr-xr-x 1 root root 14388 2007-04-12 01:02 rlogin
  13. -rwsr-xr-x 1 root root 8940 2007-04-12 01:02 rsh
  14. ---s--x--x 2 root root 162012 2007-04-12 16:34 sudo
  15. ---s--x--x 2 root root 162012 2007-04-12 16:34 sudoedit
  16. -rws--x--x 1 root root 1902244 2007-05-12 04:17 Xorg
  17. [root@localhost admin]#

点击(此处)折叠或打开

  1. [admin@localhost test]$ ll /usr/bin/ | egrep "^-.....s..."
  2. -rwsr-sr-x 1 root root 315384 2007-04-13 00:50 crontab
  3. -rwx--s--x 1 root slocate 34552 2007-03-06 10:01 locate
  4. -rwxr-sr-x 1 root mail 16140 2007-03-27 20:34 lockfile
  5. -rwxr-sr-x 1 root nobody 85032 2007-03-20 17:17 ssh-agent
  6. -r-xr-sr-x 1 root tty 10548 2007-05-04 12:37 wall
  7. -rwxr-sr-x 1 root tty 11084 2007-10-08 21:49 write
  8. -rwxr-sr-x 1 root utempter 331900 2007-04-12 19:13 xterm
  9. [admin@localhost test]$ ll /usr/bin/ | egrep "^-..s......"
  10. -rwsr-xr-x 1 root root 45380 2007-03-27 21:49 at
  11. -rwsr-xr-x 1 root root 46844 2007-04-10 18:17 chage
  12. -rws--x--x 1 root root 17964 2007-10-08 21:49 chfn
  13. -rws--x--x 1 root root 19224 2007-10-08 21:49 chsh
  14. -rwsr-sr-x 1 root root 315384 2007-04-13 00:50 crontab
  15. -rwsr-xr-- 1 root fuse 22720 2007-05-12 22:05 fusermount
  16. -rwsr-xr-x 1 root root 47352 2007-04-10 18:17 gpasswd
  17. -rwsr-xr-x 1 root root 24556 2007-04-10 18:17 newgrp
  18. -rwsr-xr-x 1 root root 25604 2007-04-05 16:54 passwd
  19. -rwsr-xr-x 1 root root 18640 2007-04-12 01:02 rcp
  20. -rwsr-xr-x 1 root root 14388 2007-04-12 01:02 rlogin
  21. -rwsr-xr-x 1 root root 8940 2007-04-12 01:02 rsh
  22. ---s--x--x 2 root root 162012 2007-04-12 16:34 sudo
  23. ---s--x--x 2 root root 162012 2007-04-12 16:34 sudoedit
  24. -rws--x--x 1 root root 1902244 2007-05-12 04:17 Xorg
  25. [admin@localhost test]$
 
     上表中注意列出文件\目录属性的前十个字符,该字符序列指出了文件类型及用户、用户组、其他组的权限。
 
    从左至右将其编号如下:9 8 7 6 5 4 3 2 1 0,每一个编号对应一个字符。对于上述的admin.txt文件其属性为:

点击(此处)折叠或打开

  1. 9 8 7 6 5 4 3 2 1 0
  2. - r w - r w - r - -
    其中位9代表文件类型,常见如下:
---------------------------------------------------------------------------------
|符号|    d    |     p    |     l     |  s    |    b     |  c       |  -    |
---------------------------------------------------------------------------------
|意义| 目录文件| 管道文件| 符号链接 | socket|块设备文件 |字符设备文件|普通文件
---------------------------------------------------------------------------------
     8 7 6位代表文件属主权限;
     5 4 3 位代表文件用户组权限;
     2 1 0位代表其他组权限。
     且三位分别代表读r、写w、执行x(搜索),如果用-占用该位,则该用户不具备对应的权限。
     实际上,在上面的例子中也存在以下几种类型,如s\S\t\T,且s\S仅可能出现在6或者3位置;而t\T仅可能出现在0位置。
     In fact,文件权限属性有12标识(bit),如下所示:

 

点击(此处)折叠或打开

  1. b a 9 8 7 6 5 4 3 2 1 0
     其中:
     第12bit(上图中b)标识是否设置SUID:1、设置SUID;0、未设置SUID;
     第11bit(上图中a)标识是否设置SGID:1、设置SGID;0、未设置SGID;
     第10bit(上图中9)标识是否设置Strict bit:1、设置Stickybit;0、未设置Stickybit;
 
     几种特殊标识的意义如下:
     s:文件用户(组)可执行,且设置了SUID(SGID);
     S:文件用户(组)不可执行,且设置了SUID(SGID);
     t:文件其他组可执行,且设置了Stickybit;
     T:文件其他组不可执行,且设置了Stickybit;
 
    结合man chmod,可以得出修改文件访问权限属性可以采用如下方式:
     1、chmod augo +=- rwxsStT filename(directoryname)
     2、chmod xxxx  filename(directoryname)
     上述xxxx为八进制对应的数字,不难得知:SUID:对应4、SGID对应2、Stickybit对应1。
 
 
二、特殊文件权限
 
     SUID,即所谓 SET UER ID,主用功能在于:某个可执行的文件具备SUID时,在执行该文件(程序)对应的进程具备文件属主的访问权限。实际上Linux下此类文件也比较常见,具体可在/usr/bin下查看(/usr/bin/passwd最为常用的一个)。该标记常用于可执行文件。在文件夹上通常没有什么意义。

点击(此处)折叠或打开

  1. [root@localhost testt]# ll /usr/bin/ | egrep "^-..s......"
  2. -rwsr-xr-x 1 root root 45380 2007-03-27 21:49 at
  3. -rwsr-xr-x 1 root root 46844 2007-04-10 18:17 chage
  4. -rws--x--x 1 root root 17964 2007-10-08 21:49 chfn
  5. -rws--x--x 1 root root 19224 2007-10-08 21:49 chsh
  6. -rwsr-sr-x 1 root root 315384 2007-04-13 00:50 crontab
  7. -rwsr-xr-- 1 root fuse 22720 2007-05-12 22:05 fusermount
  8. -rwsr-xr-x 1 root root 47352 2007-04-10 18:17 gpasswd
  9. -rwsr-xr-x 1 root root 24556 2007-04-10 18:17 newgrp
  10. -rwsr-xr-x 1 root root 25604 2007-04-05 16:54 passwd
  11. -rwsr-xr-x 1 root root 18640 2007-04-12 01:02 rcp
  12. -rwsr-xr-x 1 root root 14388 2007-04-12 01:02 rlogin
  13. -rwsr-xr-x 1 root root 8940 2007-04-12 01:02 rsh
  14. ---s--x--x 2 root root 162012 2007-04-12 16:34 sudo
  15. ---s--x--x 2 root root 162012 2007-04-12 16:34 sudoedit
  16. -rws--x--x 1 root root 1902244 2007-05-12 04:17 Xorg
  17. [root@localhost testt]#
      SGID,即SET GROUP ID:
      1、如果该位设置在可执行的文件上,则该文件执行后的有效用户组为文件的主用户组。
      2、如果该位设置在文件夹A上,则在A目录下新建的文件B或者目录C,新建文件B的用户组为目录A的主用户组,且新建目录C的SGID位也被设置。
 
      Stricybit,即粘着位:
      该位在目录下作用如下:
      假设1:目录test属于用户admin,且设置了Stricybit位,其他普通用户(hello,hw等)拥有对目录test的RWE权限;
      假设2:在目录test内其他用户所建立的文件及目录(例如:hello建立目录hellodir,hellofile; hw建立目录hwdir,hwfile);
        则:除了文件属主(hellofile/hellodir的属主为hello,hwfile/hwdir的属主为hw)及root外,只有test目录的属主(admin)可以删除其他用户(hello,hw)的文件(hellofile,hwfile)或目录(hwdir,hellodir),其他用户(hw,hello)仅能删除自己新建的目录或文件,而不能删除其他用户创建的用户或目录(hello无法删除hwfile,hwdir;且hw无法删除hellofile,hellodir)。

 

点击(此处)折叠或打开

  1. [root@localhost test]# ll / | egrep "test$"
    drwxrwxrwt   5 admin admin   4096 2012-04-01 00:08 test
  2. [hw@localhost test]$ ll
    total 16
    drwxrwxr-x 2 admin admin 4096 2012-04-01 00:05 admindir
    -rwxrwxrwx 1 admin admin    0 2012-04-01 00:05 adminfile
    drwxrwxr-x 2 hello hello 4096 2012-04-01 00:05 hellodir
    -rwxrwxrwx 1 hello hello    0 2012-04-01 00:07 hellofile
    drwxrwxr-x 2 hw    hw    4096 2012-04-01 00:16 hwdir
    -rwxrwxrwx 1 hw    hw       0 2012-04-01 00:16 hwfile
    drwxr-xr-x 2 root  root  4096 2012-04-01 00:04 rootdir
    -rwxrwxrwx 1 root  root     0 2012-04-01 00:08 rootfile
    尽管各用户相互拥有操作操作的访问权限,但由于其父目录设置了Stricybit,因而用户间不能相互删除(hw用户无法删除hello用户创建的文件,例如hellofile,尽管其拥有操作权限),规则如上所述。
 

点击(此处)折叠或打开

  1. [hello@localhost test]$ ll
  2. total 20
  3. drwxrwxr-x 2 admin admin 4096 2012-04-01 00:05 admindir
  4. -rwxrwxrwx 1 admin admin 0 2012-04-01 00:05 adminfile
  5. drwxrwxr-x 2 hello hello 4096 2012-04-01 00:05 hellodir
  6. -rwxrwxrwx 1 hello hello 0 2012-04-01 00:07 hellofile
  7. drwxrwxr-x 2 hw hw 4096 2012-04-01 00:16 hwdir
  8. -rwxrwxrwx 1 hw hw 34 2012-04-01 00:19 hwfile
  9. drwxr-xr-x 2 root root 4096 2012-04-01 00:04 rootdir
  10. -rwxrwxrwx 1 root root 0 2012-04-01 00:08 rootfile
  11. [hello@localhost test]$ rm -rf hwdir
  12. rm: cannot remove directory `hwdir': Operation not permitted
  13. [hello@localhost test]$ rm -rf hwfile
  14. rm: cannot remove `hwfile': Operation not permitted
  15. [hello@localhost test]$ su hw
  16. Password:
  17. [hw@localhost test]$ ll
  18. total 20
  19. drwxrwxr-x 2 admin admin 4096 2012-04-01 00:05 admindir
  20. -rwxrwxrwx 1 admin admin 0 2012-04-01 00:05 adminfile
  21. drwxrwxr-x 2 hello hello 4096 2012-04-01 00:05 hellodir
  22. -rwxrwxrwx 1 hello hello 0 2012-04-01 00:07 hellofile
  23. drwxrwxr-x 2 hw hw 4096 2012-04-01 00:16 hwdir
  24. -rwxrwxrwx 1 hw hw 34 2012-04-01 00:19 hwfile
  25. drwxr-xr-x 2 root root 4096 2012-04-01 00:04 rootdir
  26. -rwxrwxrwx 1 root root 0 2012-04-01 00:08 rootfile
  27. [hw@localhost test]$ rm -rf hellofile
  28. rm: cannot remove `hellofile': Operation not permitted
  29. [hw@localhost test]$ rm -rf admindir
  30. rm: cannot remove directory `admindir': Operation not permitted
  31. [hw@localhost test]$ su admin
  32. Password:
  33. [admin@localhost test]$ rm -rf hellodir
  34. [admin@localhost test]$ rm hwfile
  35. [admin@localhost test]$ ll
  36. total 12
  37. drwxrwxr-x 2 admin admin 4096 2012-04-01 00:05 admindir
  38. -rwxrwxrwx 1 admin admin 0 2012-04-01 00:05 adminfile
  39. -rwxrwxrwx 1 hello hello 0 2012-04-01 00:07 hellofile
  40. drwxrwxr-x 2 hw hw 4096 2012-04-01 00:16 hwdir
  41. drwxr-xr-x 2 root root 4096 2012-04-01 00:04 rootdir
  42. -rwxrwxrwx 1 root root 0 2012-04-01 00:08 rootfile
  43. [admin@localhost test]$

    关于上述权限位与进程的关系以及通过系统调用进行的修改影响,下节内容再做总结吧。
 
 
 
阅读(2421) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~