Linux:ACL设置
目录:
1. acl设置
2. mask值
3. acl备份
4. 实例演示
1.acl设置
Acl是关联用户和文件或目录的访问控制,因此对用户而言,可以具体分为两类,用户和组;对文件而言可以分为文件和目录;对继承而言,可以分为默认继承权限和非继承权限,从语法上讲d表示在某目录下新建文件或目录继续父目录的default权限。具体如下常用格式:
setfacl -m u:wang:rwx file|directory
setfacl -Rm g:sales:rwX directory
setfacl -m g:salesgroup:rw file| directory
setfacl -m d:u:wang:rx directory
setfacl –R –b directory|file 递归删除目录或文件下的acl权限
setfacl –x u:wang:rwxfile|directory 删除本条acl
其中最为关键和常用的,在实际项目实施和方案中,将规划好的acl复制给多个目录或文件,此时需要复制acl的语法格式,最为常见的语法格式如下:
Getfacl file1 | setfacl --set-file=-file2 将file1的acl权限复制给file2,其中=-中的“-”代表上file1,此种写法优势避免重定向给另外一个空文件,等价于getfacl file1> f1 setfacl –set-file=f1 file2。
2.mask值
文件设置acl后,文件的基本权限含义更新,文件的9个字符之前分别表示所属主的rwx权限、所属组的rwx权限和其他用户的rwx权限。在设置acl后文件权限9个字符分别表示所属主的rwx权限、mask的rwx权限和其他用户的rwx权限。而mask值则是acl中的限高线,因此在文件访问控制过程中起到至高的作用,如下所示:
[root@centos7 ~]# getfacl /testdir/dir #查看mask值为5,表示读和执行权限 getfacl: Removing leading '/' from absolute path names # file: testdir/dir # owner: root # group: root # flags: -s- user::rwx group::r-x group:g2:r-x group:g3:r-x mask::r-x other::--- default:user::rwx default:group::r-x default:group:g2:rwx default:group:g3:r-x default:mask::rwx default:other::--- [root@centos7 ~]# ll -d /testdir/dir #验证说明mask值为r和s权限 drwxr-s---+ 2 root root 21 Jul 25 12:52 /testdir/dir [root@centos7 ~]# chmod 2740 /testdir/dir #修改mask值为4,表示有r的权限 [root@centos7 ~]# getfacl /testdir/dir #重新查看并验证mask值 getfacl: Removing leading '/' from absolute path names # file: testdir/dir # owner: root # group: root # flags: -s- user::rwx group::r-x #effective:r—[mask限高线,只有r权限] group:g2:r-x #effective:r—[mask限高线,只有r权限] group:g3:r-x #effective:r—[mask限高线,只有r权限] mask::r-- other::--- default:user::rwx default:group::r-x default:group:g2:rwx default:group:g3:r-x default:mask::rwx default:other::--- |
3.acl 备份
Acl备份是通常采用备份还原方式,具体思路如下
第一:将之前文件复制一份重定向到文本中,通过—set-file=重定向文本名,然后将文本名进行还原;
第二:通过专用的的恢复策略参数,setfacl –restore文本名,恢复acl。
[root@centos7 /]# getfacl -R /app/test > acl.txt #备份策略到acl.txt getfacl: Removing leading '/' from absolute path names #进入/,确定根目录 [root@centos7 /]# cat acl.txt 不会报错 # file: app/test # owner: root # group: root user::rwx group::r-x other::r-x [root@centos7 /]# setfacl --set-file=acl.txt app/test #备份恢复方法1:acl.txt [root@centos7 /]# setfacl --restore acl.txt #备份恢复方法2,通过acl.txt |
4.实例演示:
在/testdir/dir里创建的新文件自动属于g1组,组g2的成员如:alice能对这些新文件有读写权限,组g3的成员如:tom只能对新文件有读权限,其它用户(不属于g1,g2,g3)不能访问这个文件夹。备份/testdir/dir里所有文件的ACL权限到/root/acl.txt中,清除/testdir/dir中所有ACL权限,最后还原ACL权限
[root@centos7 ~]# useradd alice #添加用户alice [root@centos7 ~]# useradd tom #添加用户tom [root@centos7 ~]# groupadd g1 #创建组g1 [root@centos7 ~]# groupadd g2 #创建组g2 [root@centos7 ~]# groupadd g3 #创建组g3 [root@centos7 ~]# groupmems -a alice -g g2 #alice加入g2 [root@centos7 ~]# groupmems -l -g g2 alice [root@centos7 ~]# groupmems -a tom -g g3 #tom加入g3 [root@centos7 ~]# groupmems -l -g g3 tom [root@centos7 ~]# mkdir -p /testdir/dir [root@centos7 ~]# chmod 2750 /testdir/dir #给目录sgid和其他用户0权限 [root@centos7 ~]# chgrp g1 /testdir/dir #给目录所属组为g1 [root@centos7 ~]# ll -d /testdir/dir drwxr-s---. 2 root root 6 Jul 25 12:40 /testdir/dir [root@centos7 ~]# setfacl -m g:g2:rx /testdir/dir #设置目录本身的权限,允许g2 [root@centos7 ~]# su – alice 进入目录dir [alice@centos7 ~]$ cd /testdir/dir/ [alice@centos7 dir]$ [root@centos7 ~]# setfacl -m d:g:g2:rw /testdir/dir #设置新建文件的继承权限 [xuewb@centos7 ~]$ getfacl /testdir/dir #验证acl设置 getfacl: Removing leading '/' from absolute path names # file: testdir/dir # owner: root # group: g1 # flags: -s- user::rwx group::r-x group:g2:r-x mask::r-x other::--- default:user::rwx default:group::r-x default:group:g2:rw default:mask::rwx default:other::--- [root@centos7 ~]# touch /testdir/dir/root.txt #创建文件 [root@centos7 ~]# vim /testdir/dir/root.txt #编辑文件 [root@centos7 ~]# ll /testdir/dir/ #验证文件权限 total 8 -rw-rw----+ 1 root root 17 Jul 25 12:52 root.txt [root@centos7 ~]# su – alice #切换用户alice Last login: Tue Jul 25 12:51:32 HKT 2017 on pts/3 [alice@centos7 ~]$ cat /testdir/dir/root.txt #查看文件可读 welcome to xi'an [alice@centos7 ~]$ vim /testdir/dir/root.txt #查看文件可写 [alice@centos7 ~]$ cat /testdir/dir/root.txt welcome to xi'an xi'an is a old city [root@centos7 ~]# setfacl -m g:g3:rx /testdir/dir #设置g3对/testdir/dir权限 [root@centos7 ~]# setfacl -m d:g:g3:rx /testdir/dir #设置文件继续权限 [root@centos7 ~]# getfacl /testdir/dir #查看验证其acl属性 getfacl: Removing leading '/' from absolute path names # file: testdir/dir # owner: root # group: g1 # flags: -s- user::rwx group::r-x group:g2:r-x group:g3:r-x mask::r-x other::--- default:user::rwx default:group::r-x default:group:g2:rw default:group:g3:r- default:mask::rwx default:other::--- root@centos7 ~]# su - xuewb Last login: Tue Jul 25 12:55:36 HKT 2017 on pts/3 [xuewb@centos7 ~]$ [xuewb@centos7 ~]$ cd /testdir/dir/ #验证其他非g2和g3组用户,无法进入dir目录 bash: cd: /testdir/dir/: Permission denied
[root@centos7 ~]# getfacl -R /testdir/dir/ > acl.txt #备份acl [root@centos7 ~]# setfacl -R -b /testdir/dir #清空acl [root@centos7 ~]# getfacl /testdir/dir #验证是否清空acl getfacl: Removing leading '/' from absolute path names # file: testdir/dir # owner: root # group: g1 # flags: -s- user::rwx group::r-- other::--- [root@centos7 ~]# setfacl --set-file=acl.txt /testdir/dir #恢复acl设置 |
转载于:https://blog.51cto.com/wbxue/1960278