全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 1346|回复: 4

[疑问] linux访问控制列表实战

[复制链接]
发表于 2015-1-30 11:56:18 | 显示全部楼层 |阅读模式

Linux访问控制列表, Access Control List ,简称ACL

作用:主要目的是针对在传统的三种身份和三种权限之外,提供更加细化的局部权限设定。官方手册来讲,它主要针对用户、用户组、以及掩码方面控制权限。
简单去理解就是,ACL 可以针对单个用户、单个用户组来进行权限细化的控制。


如何查看文件系统是否支持ACL

第一种方式:tune2fs –l/dev/sda1 | grep options
Default mount options:    user_xattr acl(表示支持ACL)

第二种方式:dumpe2fs/dev/sda1 | grep options
dumpe2fs 1.41.12(17-May-2010)
Default mount options:    user_xattr acl(表示支持ACL)


EXT2/EXT3/ext4, JFS,XFS等等都是支持ACL的

如果文件系统不支持ACL的话,可以手动给它添加ACL
tune2fs -o acl /dev/vda1
tune2fs 1.41.12 (17-May-2010)



ACL的相关的操作主要有 3 个命令,分别是 getface、setfacl和chacl,常用的主要是getfacl 和 setfacl。
  
            
getfacl    查看文件/目录的ACL设定内容
  
setfacl    设置文件/目录的ACL内容
  
chacl     查看和更改文件/目录的ACL内容,由于日常有setfacl,
  
因此chacl从来不用
  


getfacl 一般都是直接在后面跟你所要查看的文件或者目录的路径,因此掌握如何查看即可。操作如下:
[root@localhost Desktop]# getfacl /tmp
getfacl: Removing leading '/' fromabsolute path names
# file: tmp
# owner: root
# group: root
# flags: --t
user::rwx
group::rwx
other::rwx

[root@localhost Desktop]# getfacl/etc/passwd
getfacl: Removing leading '/' fromabsolute path names
# file: etc/passwd
# owner: root
# group: root
user::rw-
group::r--
other::r—



setfacl 是使用最多的,基本 ACL 方面的操作都是它,因此它的选项也是蛮多的。首先来setfacl的使用语法:
  
            
setfacl [-bkRd] [{-m|-x} acl参数] 文件/目录路径
  
选项介绍:
  
-b :删除所有的 acl 参数
  
-k :删除预设的 acl 参数
  
-R :递归设置后面的 acl 参数
  
-d :设置预设的 acl 参数(只对目录有效,在该目录新建的文件也会使用此ACL默认值)
  
-m :设置(修改)后面 acl 参数
  
-x :删除后面指定的 acl 参数
  


     ACL 参数主要由3部分组成,组成结构如下:
  
            
三种身份:对应身份名:三种权限
  
[u|g|o]:[用户名|用户组名]:[rwx]
  



实例练习
下面来看几个实例,来理解学习 ACL 的操作:
现在在/mnt目录,有文件test和目录dir,它们的权限都是600,属主和属組都是root。
  
            
[root@lh mnt]# touch test
  
[root@lh mnt]# mkdir dir
  
[root@lh mnt]# chmod 600 test
  
[root@lh mnt]# chmod 600 dir
  
[root@lh mnt]# ll
  
total 4
  
drw-------. 2 root root 4096 Jul  4 17:56 dir
  
-rw-------. 1 root root    0 Jul  4 17:56 test
  


现在要求完成如下要求:
1、为文件 test 增加 acl 权限,使 sunsky 用户可以可读可写
  
            
[root@lh mnt]# setfacl -m u:sunsky:rw test
  
[root@lh mnt]# getfacl test
  
# file: test
  
# owner: root
  
# group: root
  
user::rw-
  
user:sunsky:rw-
  
group::---
  
mask::rw-
  
other::---
  
[root@lh mnt]# su - sunsky  # 切换到sunsky用户下,进行测试
  
Wellcome to Linux World
  
[sunsky@lh ~]$ echo 1 >> /mnt/test  # 很明显能写入数据
  
[sunsky@lh ~]$ cat /mnt/test1
  


2、为文件 test 增加 acl 权限,使 sun 組的所有用户都能读该文件
  
            
[root@lh mnt]# setfacl -m g:sun:r test
  
[root@lh mnt]# getfacl test
  
# file: test
  
# owner: root
  
# group: root
  
user::rw-
  
user:sunsky:rw-
  
group::---
  
group:sun:r--
  
mask::rw-
  
other::---
  
  
[root@lh mnt]# su - sun
  
Wellcome to Linux World
  
[sun@lh ~]$ cat /mnt/test # 很明显能查看test文件的内容
  
1
  
[sun@lh ~]$ echo 2 >> /mnt/test  # 由于我们没有给sun組成员更改权限,因此不能更改
  
-bash: /mnt/test: Permission denied
  



3、为目录 dir 增加 acl 权限,使 sun 組的所有用户都能够对该目录可读可写可执行
  
            
[root@lh mnt]# setfacl -m g:sun:rw dir
  
[root@lh mnt]# getfacl dir
  
# file: dir
  
# owner: root
  
# group: root
  
user::rw-
  
group::---
  
group:sun:rwx
  
mask::rwx
  
other::---
  
  
[root@lh mnt]# su - sun  # 切换到sun用户下,进行测试
  
[sun@lh ~]$ echo "date" >> /mnt/dir/date.sh
  
[sun@lh ~]$ bash /mnt/dir/date.sh
  
Fri Jul  4 18:01:48 CST 2014
  



4、删除文件 test 上,关于 sun 組的 acl 权限
  
            
[root@lh mnt]# setfacl -x g:sun test
  
[root@lh mnt]# getfacl test
  
# file: test
  
# owner: root
  
# group: root
  
user::rw-
  
user:sunsky:rw-
  
group::---
  
mask::rw-
  
other::---
  



5、删除目录 dir 的所有 ACL 权限
  
            
[root@lh mnt]# setfacl -b dir
  
[root@lh mnt]# getfacl dir
  
# file: dir
  
# owner: root
  
# group: root
  
user::rw-
  
group::---
  
other::---
  



6、为目录 dir 增加默认ACL权限,使 dir 目录下新创建的文件或目录,都默认拥有 sunsky 用户可读可写可执行
  
            
[root@lh mnt]# setfacl -m d:u:sunsky:rwx dir
  
[root@lh mnt]# getfacl dir
  
# file: dir
  
# owner: root
  
# group: root
  
user::rw-
  
group::---
  
other::---
  
default:user::rw-
  
default:user:sunsky:rwx
  
default:group::---
  
default:mask::rwx
  
defaultther::---
  
[root@lh mnt]# touch /mnt/dir/sunsky
  
[root@lh mnt]# getfacl /mnt/dir/sunsky
  
getfacl: Removing leading '/' from absolute path names
  
# file: mnt/dir/sunsky
  
# owner: root
  
# group: root
  
user::rw-
  
user:sunsky:rwx                 #effective:rw-
  
group::---
  
mask::rw-
  
other::---
  



   在第六题中,我们发现,在user:sunsky:rwx后面多了一个 #effective:rw-,这是为什么呢?我们在切换到sunsky用户下,看看它是否有执行该文件的权限!
  
            
[root@lh mnt]# su - sunsky
  
Wellcome to Linux World
  
[sunsky@lh ~]$ bash /mnt/dir/sunsky
  
bash: /mnt/dir/sunsky: Permission denied
  
    很明显,尽管我们使用setfacl给了sunsky对dir目录下默认新生成的文件可读可写可执行的权限,但是依旧是没有执行权限的。这是为什么呢?
       我们发现多了输出#effective:rw-,它是由于什么出来的呢?
       effective生效的为rw,他是受我们输出中的mask影响的。但是我们发现,我们并没有设置过mask啊,为啥他默认变成rw了。这里我就来介绍一下mask!
       mask 的作用是为了用来限制除了属主和其他人以外的所有用户或组的权限,mask 权限为这些用户他们可能拥有的最高权限。
       如果遇到设置的用户权限与 mask 权限冲突,则用户的权限为
       # effective 权限
       那么,一旦一个文件被设置了 ACL,其文件原属组部分的权限将变为 MASK 权限,而并非原来的属组权限。如果其文件原先属組权限为空,那么当你设置了mask权限之后,你的属組权限也相应改变为其mask对应的权限。


      下面,我们就继续进行第六题的实验!
  
            
[root@lh mnt]# setfacl -m m::rwx /mnt/dir/
  
[root@lh mnt]# getfacl /mnt/dir/
  
getfacl: Removing leading '/' from absolute path names
  
# file: mnt/dir/# owner: root
  
# group: root
  
user::rw-
  
group::---
  
mask::rwx
  
other::---
  
default:user::rw-
  
default:user:sunsky:rwx
  
default:group::---
  
default:mask::rwx
  
defaultther::---
  
  
[root@lh mnt]# su - sunsky
  
Wellcome to Linux World
  
[sunsky@lh ~]$ bash /mnt/dir/sunsky
  
bash: /mnt/dir/sunsky: Permission denied
  



    奇怪了,为什么我已经更改了mask为rwx了,并且effective也不再出现了,为何现在sunsky依旧拿不到执行权限呢?
    我们现在去看下/mnt/dir/sunsky文件的ACL权限吧。
  
            
[root@lh mnt]# getfacl /mnt/dir/sunsky
  
getfacl: Removing leading '/' from absolute path names
  
# file: mnt/dir/sunsky
  
# owner: root
  
# group: root
  
user::rw-
  
user:sunsky:rwx                 #effective:rw-
  
group::---
  
mask::rw-
  
other::---
  
    通过查看我们发现,/mnt/dir/sunsky文件中,对于 sunsky的acl权限设置竟然还是 # effective:rw- ,这是为什么呢?
       原来,我们刚才修改 /mnt/dir 的 mask 仅仅只针对/mnt/dir目录下新生成的文件有效,并且由于文件在创建时,受到传统权限的 umask 值的影响,已经拥有了属組的权限,所以就使得 ACL 的mask设置失效。因此,此时我们通过使用上面提到的-R 递归选项,将/mnt/dir目录下的所有文件重新修改一次 ACL 的mask权限,就能解决该问题!
  
            
[root@lh mnt]# su - sunsky
  
Wellcome to Linux World
  
[sunsky@lh ~]$ echo date > /mnt/dir/sunsky
  
[sunsky@lh ~]$ bash /mnt/dir/sunsky
  
Fri Jul  4 18:32:11 CST 2014
  
    以上就是 setfacl 的日常管理的所有操作了!相信只要大家将以上的操作掌握,以后只要用到ACL的地方就不会窘迫了。
发表于 2015-1-30 12:00:31 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
 楼主| 发表于 2015-1-30 12:03:19 | 显示全部楼层
this100 发表于 2015-1-30 12:00
你不是来刷贴的吧?

这比发广告有用多了吧!!这个贴对一些人有用多了吧!!
发表于 2015-1-30 12:04:41 | 显示全部楼层
this100 发表于 2015-1-30 12:00
你不是来刷贴的吧?

唔。。。

http://nolinux.blog.51cto.com/4824967/1434617
发表于 2015-1-30 12:14:51 | 显示全部楼层
我是人 发表于 2015-1-30 12:04
唔。。。

http://nolinux.blog.51cto.com/4824967/1434617

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-5-19 22:35 , Processed in 0.061506 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表