手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux ACL权限规划:getfacl,setfacl使用

[日期:2013-07-30] 来源:Linux社区  作者:guodong810 [字体: ]

ACL的使用

    ACL即Access Control List 主要的目的是提供传统的owner,group,others的read,write,execute权限之外的具体权限设置,ACL可以针对单一用户、单一文件或目录来进行r,w,x的权限控制,对于需要特殊权限的使用状况有一定帮助。如,某一个文件,不让单一的某个用户访问。

  ACL使用两个命令来对其进行控制
    getfacl:取得某个文件/目录的ACL设置项目
    setfacl:设置某个文件/目录的ACL设置项目
 
 setfacl 参数
  -m:设置后续acl参数
  -x:删除后续acl参数 
  -b:删除全部的acl参数
  -k:删除默认的acl参数
  -R:递归设置acl,包括子目录
  -d:设置默认acl

例:创建一文件test,将其权限修改为777,并查看其默认ACL权限配置
    [root@ www.linuxidc.com ~]# touch /test
    [root@ www.linuxidc.com ~]# chmod 777 /test
    [root@ www.linuxidc.com ~]# getfacl /test            //获得文件的ACL权限
    getfacl: Removing leading '/' from absolute path names
    # file: test                                //文件名
    # owner: root                            //文件所属者
    # group: root                            //文件所属组
    user::rwx                                //文件所属者权限
    group::rwx                              //同组用户权限
    other::rwx                              //其它者权限

[root@ www.linuxidc.com ~]#

可以看到其它者的权限也是可读可写可执行,可以自行测试,现在我们修改其ACL策略,使用用户code只有读取的权限
 [root@ www.linuxidc.com ~]# setfacl -m u:code:r /test
[root@ www.linuxidc.com ~]# ll /test
-rwxrwxrwx+ 1 root root 1 Apr 11 07:25 /test            //可以看到权限的最后多了一个”+”号
[root@ www.linuxidc.com ~]#
现在再次查看一下此文件的ACL属性
[root@ www.linuxidc.com ~]# getfacl /test
getfacl: Removing leading '/' from absolute path names
# file: test
# owner: root
# group: root
user::rwx
user:code:r--                          //可以看到code单独的权限为r--
group::rwx
mask::rwx
other::rwx

 [root@ www.linuxidc.com ~]#
注:code的权限并不是只根据ACL配置来决定的,它是由code用户基本权限与配置的ACL权限的“与”运算决定的,即other:rwx 与 code:r--  = code:r--
现在使用code用户,测试是否可写
 在写文件时,会出现-- INSERT -- W10: Warning: Changing a readonly file提示。

除了对单个用户进行设置外,还可以对用户组、有效权限(mask)进行设置如对用户组设置:    g:[用户组]:[rwx]
注:有效权限(mask) 即用户或组所设置的权限必须要存在于mask的权限设置范围内才会生效
 如上面的/test文件,已经有了可读权限,如果我们把它的有效权限修改为只有写权限,则设置的acl权限不在有效权限之内,则用户code就不可能再查看/test文件中的内容了
 [root@ www.linuxidc.com ~]# setfacl -m m:w /test              //设置有效权限为只写
可以查看/test acl属性
[root@ www.linuxidc.com ~]# getfacl /test
getfacl: Removing leading '/' from absolute path names
# file: test
# owner: root
# group: root
user::rwx
user:code:r-- #effective:---
group::rwx #effective:-w-
mask::-w-                      //可以看到有效权限已经修改成功
other::rwx

[root@ www.linuxidc.com ~]#

使用code用户查看文件内容,首先使用root用户写入一些内容,会使测试更加直观
[root@ www.linuxidc.com ~]# echo "this is a test getfacl " >/test
[code@ www.linuxidc.com ~]$ vim /test
 "/test" [Permission Denied]                    //可以在最下面看到不允许访问的提示,并且看不到任何内容

取消acl权限
[root@ www.linuxidc.com ~]# setfacl -x u:code /test            //取消/test对用户code的权限
    [root@ www.linuxidc.com ~]# setfacl -x m /test                //恢复有效权限
    [root@ www.linuxidc.com ~]# getfacl /test             
    getfacl: Removing leading '/' from absolute path names
    # file: test
    # owner: root
    # group: root
    user::rwx
    group::rwx
    other::rwx

    [root@ www.linuxidc.com ~]# ll /test
    -rwxrwxrwx 1 root root 24 Apr 11 08:01 /test              //已经可以正常使用
    [root@ www.linuxidc.com ~]#

    至于另外的一些参数,自己尝试使用!!

相关阅读:

Linux权限的认识 http://www.linuxidc.com/Linux/2007-07/5902.htm

Linux权限补充:rwt rwT rws rwS 特殊权限 http://www.linuxidc.com/Linux/2013-07/87537.htm

linux
相关资讯       ACL  ACL权限  Linux ACL 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款