最近在学习Glusterfs,其中有涉及到文件的访问控制,所以就简单记录个笔记。
1 命令帮助
setfacl是用来设置文件访问控制的命令:
[jiaxiaol@scops-buildsvr test]$ which setfacl /usr/bin/setfacl
[jiaxiaol@scops-buildsvr ~]$ setfacl --help setfacl 2.2.49 -- set file access control lists Usage: setfacl [-bkndRLP] { -m|-M|-x|-X ... } file ... -m, --modify=acl modify the current ACL(s) of file(s) -M, --modify-file=file read ACL entries to modify from file -x, --remove=acl remove entries from the ACL(s) of file(s) -X, --remove-file=file read ACL entries to remove from file -b, --remove-all remove all extended ACL entries -k, --remove-default remove the default ACL --set=acl set the ACL of file(s), replacing the current ACL --set-file=file read ACL entries to set from file --mask do recalculate the effective rights mask -n, --no-mask don't recalculate the effective rights mask -d, --default operations apply to the default 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') --test test mode (ACLs are not modified) -v, --version print version and exit -h, --help this help text
-b,--remove-all:删除所有扩展的acl规则,基本的acl规则(所有者,群组,其他)将被保留。
-k,--remove-default:删除缺省的acl规则。如果没有缺省规则,将不提示。
-n,--no-mask:不要重新计算有效权限。setfacl默认会重新计算ACL mask,除非mask被明确的制定。
--mask:重新计算有效权限,即使ACL mask被明确指定。
-d,--default:设定默认的acl规则。
--restore=file:从文件恢复备份的acl规则(这些文件可由getfacl -R产生)。通过这种机制可以恢复整个目录树的acl规则。此参数不能和除--test以外的任何参数一同执行。
--test:测试模式,不会改变任何文件的acl规则,操作后的acl规格将被列出。 -R,--recursive:递归的对所有文件及目录进行操作。
-L,--logical:跟踪符号链接,默认情况下只跟踪符号链接文件,跳过符号链接目录。
-P,--physical:跳过所有符号链接,包括符号链接文件。
--version:输出setfacl的版本号并退出。
--help:输出帮助信息。
--:标识命令行参数结束,其后的所有参数都将被认为是文件名
-:如果文件名是-,则setfacl将从标准输入读取文件名。
setfacl 后面可以接的命令格式有:
[d[efault]:] [u[ser]:]uid [:permissions] 指定用户的权限,文件所有者的权限(如果uid没有指定)。
[d[efault]:] g[roup]:gid [:permissions] 指定群组的权限,文件所有群组的权限(如果gid未指定)
[d[efault]:] m[ask][:] [:permissions] 有效权限掩码
[d[efault]:] o[ther] [:permissions] 其他的权限
对于uid和gid,可以指定一个数字,也可指定一个名字。permissions代表各种权限的组合:读-r写-w执行-x,执行只适合目录和一些可执行的文件,permissions也可设置为八进制格式。
getfacl是用来查看文件访问控制列表的命令
[jiaxiaol@scops-buildsvr test]$ getfacl --help getfacl 2.2.49 -- get file access control lists Usage: getfacl [-aceEsRLPtpndvh] file ... -a, --access display the file access control list only -d, --default display the default access control list only -c, --omit-header do not display the comment header -e, --all-effective print all effective rights -E, --no-effective print no effective rights -s, --skip-base skip files that only have the base entries -R, --recursive recurse into subdirectories -L, --logical logical walk, follow symbolic links -P, --physical physical walk, do not follow symbolic links -t, --tabular use tabular output format -n, --numeric print numeric user/group identifiers -p, --absolute-names don't strip leading '/' in pathnames -v, --version print version and exit -h, --help this help text
2 实例
2.0 查看文件系统是否支持ACL
[root@scops-buildsvr ~]# tune2fs -l /dev/mapper/VolGroup-lv_root | grep option Default mount options: user_xattr acl
——————————————————————————————————————————
如果发现选项中有acl则代表文件系统支持acl,如果文件系统不支持acl,则需要为其添加。开启acl支持有两种方法:
1)修改mount选项:
mount -o remount,acl /dev/vda3 /mnt/acltest
开机自动挂载:
vim /etc/fstab
/dev/vda3 /mnt/acltest ext4 defaults,acl 0 0
2)使用tune2fs修改文件系统信息:
tune2fs开启acl后已是永久有效,无需再修改fstab的mount选项:
tune2fs -o acl /dev/vda3 修改文件系统自身信息来设置acl选项
tune2fs -o ^acl /dev/vda3 取消acl选项
——————————————————————————————————————————
2.1 查看一个文件或目录的ACL权限
[jiaxiaol@scops-buildsvr tmp]$ getfacl /tmp/hello.py getfacl: Removing leading '/' from absolute path names # file: tmp/hello.py # owner: jiaxiaol # group: jiaxiaol user::rwx group::--- other::---
[jiaxiaol@scops-buildsvr tmp]$ getfacl /tmp/ getfacl: Removing leading '/' from absolute path names # file: tmp/ # owner: root # group: root # flags: --t user::rwx group::rwx other::rwx
2.2 设置文件的ACL权限
2.2.1将用户组test对文件hello.py的ACL权限设置为只读r
[jiaxiaol@scops-buildsvr tmp]$ ls -l /tmp/hello.py -rwx------ 1 jiaxiaol jiaxiaol 152 Jul 26 02:18 /tmp/hello.py
[jiaxiaol@scops-buildsvr tmp]$ setfacl -m g:test:r-x /tmp/hello.py
[jiaxiaol@scops-buildsvr tmp]$ ls -l /tmp/hello.py -rwxr-x---+ 1 jiaxiaol jiaxiaol 152 Jul 26 02:18 /tmp/hello.py [jiaxiaol@scops-buildsvr tmp]$ getfacl /tmp/hello.py getfacl: Removing leading '/' from absolute path names # file: tmp/hello.py # owner: jiaxiaol # group: jiaxiaol user::rwx group::--- group:test:r-x mask::r-x other::---
从上面可以看到文件/tmp/hello.py的基本权限也发生了变化,由原来的700变为了750...我们来试一下,test组里的用户是不是有查看文件hello.py的权限
[test@scops-buildsvr ~]$ cat /tmp/hello.py #!/usr/bin/python fruits = ['banana','apple','mango'] for index in range(len(fruits)): print 'Current fruit is:', fruits[index] print "Goodbye!"
2.2.2 取消用户组test对文件hello.py的ACL权限
[jiaxiaol@scops-buildsvr tmp]$ setfacl -x g:test /tmp/hello.py
[jiaxiaol@scops-buildsvr tmp]$ getfacl /tmp/hello.py getfacl: Removing leading '/' from absolute path names # file: tmp/hello.py # owner: jiaxiaol # group: jiaxiaol user::rwx group::--- mask::--- other::---
[jiaxiaol@scops-buildsvr tmp]$ ls -l /tmp/hello.py -rwx------+ 1 jiaxiaol jiaxiaol 152 Jul 26 02:18 /tmp/hello.py
此时再看test已经没有查看文件的权限了
[test@scops-buildsvr tmp]$ cat /tmp/hello.py cat: /tmp/hello.py: Permission denied
有同学说了,那是因为没有给test基本权限里面的其他用户r权限,我添加下
[jiaxiaol@scops-buildsvr tmp]$ chmod o+rx /tmp/hello.py
[jiaxiaol@scops-buildsvr tmp]$ ls -l /tmp/hello.py -rwx---r-x+ 1 jiaxiaol jiaxiaol 152 Jul 26 02:18 /tmp/hello.py
[jiaxiaol@scops-buildsvr tmp]$ getfacl /tmp/hello.py getfacl: Removing leading '/' from absolute path names # file: tmp/hello.py # owner: jiaxiaol # group: jiaxiaol user::rwx group::--- mask::--- other::r-x
什么鬼?貌似我修改了文件hello.py的基本权限后,它的ACL权限也跟着一起变化了。这也解释了为什么我之前从没在我的机器上关注过ACL权限,但是我的文件access却没有影响,先这样安慰自己吧。。。
2.2.3删除文件的所有扩展ACL权限
[jiaxiaol@scops-buildsvr tmp]$ setfacl -b /tmp/hello.py
[jiaxiaol@scops-buildsvr tmp]$ getfacl /tmp/hello.py getfacl: Removing leading '/' from absolute path names # file: tmp/hello.py # owner: jiaxiaol # group: jiaxiaol user::rwx group::--- other::r-x
[jiaxiaol@scops-buildsvr tmp]$ ls -l /tmp/hello.py -rwx---r-x 1 jiaxiaol jiaxiaol 152 Jul 26 02:18 /tmp/hello.py
貌似文件权限没有变化啊?细心的同学会发现,变化还是有的,之前的mask::---在后面没有了
后来读到了这篇文章后http://man.linuxde.net/setfacl,我的所有疑问终于解开了,感觉满满都是干货啊。我这里也大量饮用了文章中的一些描述,希望原作者谅解。