Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2339810
  • 博文数量: 2110
  • 博客积分: 18861
  • 博客等级: 上将
  • 技术积分: 24420
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-05 18:23
文章分类

全部博文(2110)

文章存档

2011年(139)

2010年(1971)

我的朋友

分类: LINUX

2011-02-10 00:02:31

什么是ACL

ACL是Access Control List的缩写,主要的目的是在提供传统的owner,group,others的 read,write,execute权限之外的局部权限设定。ACL可以针对单个用户,单个文件或目录来进行r,w,x的权限设定,特别适用于需要特殊权限的使用情况。
ACL主要针对用户(user)、用户组(group)、以及掩码(mask)方面来控制权限。
简单地来说,ACL就是可以设置特定用户或用户组对于一个文件/目录的操作权限。
而在windows系统上,没有这个ACL,ACL是类Unix(Unix-like)操作系统权限的额外支持项目,因此要使用ACL必须要有文件系统的支持才行。主要包括ReiserFS, EXT2/EXT3/ext4, JFS, XFS等文件系统。

查看系统是否支持ACL

要查看你的系统是不是支持ACL,我们可以通过下面的方法来查看。
[root@tonyzhang ~]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1             15118728   2442140  11908588  18% /
[root@tonyzhang ~]# dumpe2fs /dev/sda1 |grep acl
dumpe2fs 1.41.12 (17-May-2010)+`-
Default mount options:    user_xattr acl

我们看到,默认的挂载选项就已经有了ACL了,如果你的系统挂载时候没有这个选项,你可以通过
mount -o remount,acl /dev/sda1
来重新挂载。你也可以把这个挂载选项加入到开机启动,也就是写入到/etc/fatab文件里面。

ACL权限的查看与设置(getfacl, setfacl)

知道了ACL的意义了,也知道了系统是否支持ACL,那么下面就是如何来设定/使用这个ACL呢?
getfacl:查看文件/目录的ACL设定内容
setfacl:设置文件/目录的ACL内容

相关参数说明

先来看看setfacl这个命令的相关参数说明
语法:setfacl [-bkRd] [{-m|-x} acl参数] 文件名
-m :设置后续的acl参数
-x :删除后续的acl参数
-b :删除所有的ACL设定参数
-R :递归设置acl参数
-d :设置预设的acl参数(只对目录有效,在该目录新建的文件也会使用此ACL默认值)
-k :删除预设的ACL参数

设置格式如下

[d[efault]:] u[ser]:uid [:perms]
[d[efault]:] g[roup]:gid [:perms]
[d[efault]:] m[ask][:] [:perms]
[d[efault]:] o[ther][:] [:perms]

以上的参数和设置格式说明,可以在MAN中查看到

针对其他人的ACL设置

下面我们就用例子来演示一下ACL的设置与查看
我们在/root目录下进行操作
先查看install.log文件的ACL设置值是什么
[root@tonyzhang ~]# getfacl install.log
# file: install.log
# owner: root
# group: tonyzhang
user::rwx
group::r--
other::r--
[root@tonyzhang ~]# ls -l install.log
-rwxr--r--. 1 root tonyzhang 31537 Jan 20 05:09 install.log

我想通过上面的对比,大家应该能看明白getfacl所显示出来的内容吧!OK,这里我就不多说了,下面我们来看看给这个文件设置ACL值后的效果。
[root@tonyzhang ~]# setfacl -m o:rwx install.log
[root@tonyzhang ~]# getfacl install.log
# file: install.log
# owner: root
# group: tonyzhang
user::rwx
group::r--
other::rwx
[root@tonyzhang ~]# ls -l install.log
-rwxr--rwx. 1 root tonyzhang 31537 Jan 20 05:09 install.log

这时候,我对other赋予了rwx权限了,我们切换到其他用户,就可以对此文件进行写操作了。大家可能也发现了,通过setfacl设置的other权限,和通过chmod设置的效果是一样的。没错,是这样的。

针对用户的ACL设置

把install.log拷贝到根目录,
[root@tonyzhang ~]# cp install.log /
[root@tonyzhang ~]# ls -l /install.log
-rwxr-xr-- 1 root root 31537 Feb  9 16:27 /install.log

我们通过ACL给tonyzhang用户赋予rwx权限
[root@tonyzhang ~]# setfacl -m u:tonyzhang:rwx /install.log
[root@tonyzhang ~]# getfacl /install.log
getfacl: Removing leading '/' from absolute path names
# file: install.log
# owner: root
# group: root
user::rwx
user:tonyzhang:rwx
group::r-x
mask::rwx
other::r--
[root@tonyzhang ~]# ls -l /install.log
-rwxrwxr--+ 1 root root 31537 Feb  9 16:27 /install.log

这时候,通过ls -l查看的文件权限后面多了一个“+”号,这就表示了文件存在ACL权限。我们切换到tonyzhang用户,来对此文件进行一下编辑操作是完全没有问题,这里面就不演示了,自己动手吧。

注:
1、上面的用户可以换成用户列表,中间用英文的“,”分隔就OK了。
2、针对用户组的ACL设置与用户的设置差不多,这里就不演示了。

删除ACL的设置

要是删除我们设置的ACL权限的话,要怎么做呢?有两种方法

1、用-x删除后面接着的ACL权限

[root@tonyzhang ~]# setfacl -x u:tonyzhang /install.log
[root@tonyzhang ~]# getfacl /install.log
getfacl: Removing leading '/' from absolute path names
# file: install.log
# owner: root
# group: root
user::rwx
group::r-x
mask::r-x
other::r--

这时候发现还有个mask的权限没有去掉,
[root@tonyzhang ~]# setfacl -x m:: /install.log
[root@tonyzhang ~]# getfacl /install.log
getfacl: Removing leading '/' from absolute path names
# file: install.log
# owner: root
# group: root
user::rwx
group::r-x
other::r--

经过了上面的操作才算把权限还原了,实在有点不方便,而且在使用-x的时候,不能单独删除某个权限。否则会出现错误提示。如setfacl -x u:tonyzhang:rwx /install.log,这们的命令是不可以的,不知道是我哪里用错了,还是这个命令就是这样。还是用下面这种方法来提直接。

2、用-b删除所有的ACL权限

[root@tonyzhang ~]# setfacl -m u:tonyzhang:rwx /install.log
[root@tonyzhang ~]# getfacl /install.log
getfacl: Removing leading '/' from absolute path names
# file: install.log
# owner: root
# group: root
user::rwx
user:tonyzhang:rwx
group::r-x
mask::rwx
other::r--

[root@tonyzhang ~]# setfacl -b /install.log
[root@tonyzhang ~]# getfacl /install.log
getfacl: Removing leading '/' from absolute path names
# file: install.log
# owner: root
# group: root
user::rwx
group::r-x
other::r--
这个-b参数,一次性把所有的ACL权限全部清空,还原成文件的原来权限。我推荐大家用这个参数。

ACL的mask设置

关于group的设置与user的设置类似,这里也就不做演示了,下面我们来看看mask,他的作用就是让用户/组对某个文件只有某些权限。mask只对其他用户和组的权限有影响,而对owner和other的权限是没有任何影响的。我们还是以/install.log为例来实验。
[root@tonyzhang ~]# ls -l /install.log
-rwxr-xr-- 1 root root 31537 Feb  9 17:03 /install.log
[root@tonyzhang ~]# setfacl -m u:tonyzhang:rwx /install.log
[root@tonyzhang ~]# getfacl /install.log
getfacl: Removing leading '/' from absolute path names
# file: install.log
# owner: root
# group: root
user::rwx
user:tonyzhang:rwx
group::r-x
mask::rwx
other::r--

这时候我们看到mask::rwx是全部的权限,所以,切换到tonyzhang这个帐户的时候,可能对/install.log文件进行写操作的。下面我们让tonyzhang用户对其只有读取的权限。
[root@tonyzhang ~]# setfacl -m m::r /install.log
[root@tonyzhang ~]# getfacl /install.log
getfacl: Removing leading '/' from absolute path names
# file: install.log
# owner: root
# group: root
user::rwx
user:tonyzhang:rwx            #effective:r--
group::r-x            #effective:r--
mask::r--
other::r--

我们可以看到,user:tonyzhang:rwx的后面多了一个提示#effective:r--,也就是说,现在tonyzhang用户只拥有r权限了。切换到tonyzhang用户对/install.log文件进行一下写操作,会有“-- INSERT -- W10: Warning: Changing a readonly file ”这样的提示。

关于-d参数的作用我这里就不赘述了,用法是一样的,只是他是针对目录而已,同时也会对里面后加的文件起作用而已,而-R是我们一直在用的参数,一个递归处理的效果,很多地方都会用到些参数。

本文关键字:一天一点,学习Linux,RHEL6,ACL,权限

阅读(480) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-03-16 21:56:09

学习了,多谢楼主分享哦!也欢迎广大linux爱好者来我的论坛一起讨论arm哦!www.lt-net.cn

chinaunix网友2011-03-16 21:56:09

学习了,多谢楼主分享哦!也欢迎广大linux爱好者来我的论坛一起讨论arm哦!www.lt-net.cn