您的位置:首页 > 其它

Acl(Access Control List)访问控制列表

2015-04-03 09:39 537 查看
setfacl,顾名思义就是设置文件的ACL规则。

Acl(Access Control List)就是访问控制列表,最初好像是unix里面为了提供更高级的权限管理而搞出来的。
可能是被chmod命令的3个权限控制无法满足,而被迫搞出来的吧!
ACL的设置技巧

getfacl: 取得某个文件/目录的ACL设置项目setfacl: 设置某个目录/文件的ACL规定

查看磁盘是否支持acl
[root@kinggoo.com ~]#  tune2fs -l /dev/hda1 | grep option
Default mount options:    user_xattr acl
–help一下
[root@kinggoo.com ~]# setfacl --help
setfacl 2.2.39 -- set file access control listsUsage: setfacl [-bkndRLP] { -m|-M|-x|-X ... } file ...		# -m|-M|-x|-X
-m, --modify=acl        modify the current ACL(s) of file(s) 		#设置文件acl规则(s估计是sock文件)
-M, --modify-file=file  read ACL entries to modify from file	#修改文件acl规则
-x, --remove=acl        remove entries from the ACL(s) of file(s)	#删除文件的acl规则(s估计是sock文件)
-X, --remove-file=file  read ACL entries to remove from file	#删除文件的acl规则
-b, --remove-all        remove all extended ACL entries	# 删除所有扩展的acl规则,基本的acl规则(所有者,群组,其他)将被保留.
-k, --remove-default    remove the default ACL	#删除缺省的acl规则。如果没有缺省规则,将不提示.
--set=acl           set the ACL of file(s), replacing the current ACL.
--set-file=file     read ACL entries to set from file	#从文件中读设置ACL规则.
--mask              do recalculate the effective rights mask	#重新计算有效权限,即使ACL mask被明确指定.
-n, --no-mask           don't recalculate the effective rights mask	#不要重新计算有效权限。setfacl默认会重新计算ACL mask,除非mask被明确的制定.
-d, --default           operations apply to the default ACL	#设定默认的acl规则,针对目录而言.
-R, --recursive         recurse into subdirectories	#递归的对所有文件及目录进行操作.
-L, --logical           logical walk, follow symbolic links	#跟踪符号链接,默认情况下只跟踪符号链接文件,跳过符号链接目录。
-P, --physical          physical walk, do not follow symbolic links	#跳过所有符号链接,包括符号链接文件。
--restore=file      restore ACLs (inverse of `getfacl -R')	#从文件恢复备份的acl规则(这些文件可由getfacl -R产生)。通过这种机制可以恢复整个目录树的acl规则。此参数不能和除--test以外的任何参数一同执行.
--test              test mode (ACLs are not modified)	#测试模式,不会改变任何文件的acl规则,操作后的acl规格将被列出.
-v, --version           print version and exit	#版本.
-h, --help              this help text	#不用说肯定是帮助了.
当使用-M,-X选项从文件中读取规则时,setfacl接受getfacl命令输出的格式。每行至少一条规则,以#开始的行将被视为注释.其他的权限setfacl命令可以识别以下的规则格式。
setfacl命令可以识别以下的规则格式。[d[efault]:] [u[ser]:]uid [:perms]指定用户的权限,文件所有者的权限(如果uid没有指定)。[d[efault]:] g[roup]:gid [:perms]指定群组的权限,文件所有群组的权限(如果gid未指定)[d[efault]:] m[ask][:] [:perms]有效权限掩码[d[efault]:] o[ther] [:perms]
perms域是一个代表各种权限的字母的组合:读:r 写:w 执行:x,执行只适合目录和一些可执行的文件。perms域也可设置为八进制格式0~7。其他的权限

试验一下,创建一个文件,然后获取该文件的acl
[shell@kinggoo.com ~]# touch  file.kinggoo[shell@kinggoo.com ~]# getfacl file.kinggoo #获取acl# file: file.kinggoo# owner: root# group: kinggoouser::rw-group::r--other::r--
给kinggoo.com用户向file.kingoo文件增加读和执行的acl规则
[shell@kinggoo.com ~]# setfacl -m u:kinggoo.com:rx file.kinggoo
#查看user:kinggoo.com:r-x出来了吧,而且mask被默认被设置[shell@kinggoo.com ~]# getfacl file.kinggoo
# file: file.kinggoo# owner: root# group: kinggoouser::rw-user:kinggoo.com:r-xgroup::r--mask::r-x
other::r--
木有写用户的时候会修改默认文件所有者的权限
[shell@kinggoo.com ~]# setfacl -m u::rwx file.kinggoo
#最初是user:rw-,现在被修改rwx了[shell@kinggoo.com ~]# getfacl file.kinggoo
# file: file.kinggoo# owner: root# group: kinggoouser::rwx
user:kinggoo.com:r-xgroup::r--mask::r-x
other::r--
设置组的话只需要把setfacl -m u::rwx file.kinggoo中的u改为g即可,大致差不多。

设置mask的话,setfacl -m u::rwx file.kinggoo中的u改为m,并且这个可不针对用户和组哦,其他的大致差不多。

在使用-R时,记得放在-m前面,否则不可以地

使用-d的话,就会把默认的都加上去,针对目录哦。设置文件会警告[setfacl: /root/a/file.kinggoo: Only directories can have default ACLs]

mkdir一个目录
[shell@kinggoo.com ~]# mkdir directories.kinggoo[shell@kinggoo.com ~]# setfacl  -m u:kinggoo.com:x d.kinggoo/  #目录如果不加-d就会警告,所以-d要和目录使用[shell@kinggoo.com ~]# setfacl: d.kinggoo/: No such file or directory[shell@kinggoo.com ~]# setfacl -d -m u:kinggoo.com:x directories.kinggoo/		#设置这个目录acl,并且使用-d参数[shell@kinggoo.com ~]# getfacl directories.kinggoo# file: directories.kinggoo# owner: root# group: kinggoouser::rwxgroup::r-x
other::r-xdefault:user::rwxdefault:user:kinggoo.com:--xdefault:group::r-xdefault:mask::r-xdefault:other::r-x
去掉所有的acl规则
[shell@kinggoo.com ~]# setfacl -b  directories.kinggoo/ [shell@kinggoo.com ~]# getfacl directories.kinggoo/# file: directories.kinggoo# owner: root# group: kinggoouser::rwxgroup::r-x
other::r-x
先查看一下文件默认的acl规则,然后去掉指定的acl规则
[shell@kinggoo.com ~]# getfacl    file.kinggoo
# file: file.kinggoo# owner: root# group: kinggoouser::rwx
user:kinggoo.com:r-x  #< ----注意这里group::r--mask::r-x
other::r--
然后执行-x参数
[shell@kinggoo.com ~]# setfacl -x u:kinggoo.com  file.kinggoo
[shell@kinggoo.com ~]# getfacl    file.kinggoo
# file: file.kinggoo# owner: root# group: vancluser::rwxgroup::r--		#没有user:kinggoo.com:r-x这行了mask::r--other::r--
下面这个是我脚本里面写的一个东西,不过为了能起到例子的作用我把变量多数都改掉了(木有用在我的服务器哦)

${DocRoot}是一个变量,代表是一个文件夹目录!!!!!
设置两个用户的ACL
setfacl -R -m u:[user.kinggoo.com]:rw- ${DocRoot};setfacl -R -m u:[user1.kinggoo.com]:rwx ${DocRoot};setfacl -d -R -m u:[user.kinggoo.com]:rw- ${DocRoot};setfacl -d -R -m u:[user1.kinggoo.com]:rw- ${DocRoot};设置两个组的ACL
setfacl -R -m g:[group.kinggoo]:--- ${DocRoot};setfacl -R -m g:[group1.kinggoo]:--- ${DocRoot};setfacl -d -R -m g:[group.kinggoo]:--- ${DocRoot};setfacl -d -R -m g:[group1.kinggoo]:--- ${DocRoot};设置other的ACL
setfacl -R -m o::--- ${DocRoot};setfacl -d -R -m o::--- ${DocRoot};设置默认mask的ACL
setfacl -R -m m::rwx ${DocRoot};setfacl -d -R -m m::rw- ${DocRoot};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: