用户权限管理始终是 Linux/Unix 系统管理中最重要的环节。UGO 权限管理方式是最常用的,但是,对于一些比较复杂的权限管理,UGO显得无能为力,此时便需要用到ACL。 Linux下的ACL(Access Control List),即文件/目录的访问控制列表,可以针对任意指定的用户或用户组分配rwx权限。通过ACL功能配置的权限优先级高于通过chmod命令配置的普通权限。
以下实验平台为CentOS 6.2。
一、设置ACL支持
Linux ACL需要内核和文件系统的支持,安装操作系统任务完成后所建立的分区默认是不支持ACL功能的,需要手动进行相关配置。
这里以加载到/data的lv_data分区为例, [iyunv@itpro ~]# cat /etc/fstab # /etc/fstab /dev/mapper/vg_itpro-lv_root / ext4 defaults 1 1 /dev/mapper/vg_itpro-lv_data /data ext4 defaults 0 0 ……省略部分输出…… [iyunv@itpro ~]# ll -d /data/ drwxr-xr-x. 4 root root 4096 Mar 12 02:56 /data/ [iyunv@itpro ~]# ll /data/ total 24 drwxr-xr-x. 2 root root 4096 Mar 12 02:56 dirtest -rw-r--r--. 1 root root 5 Mar 12 02:55 filetest drwx------. 2 root root 16384 Mar 12 02:53 lost+found
当root试图针对/data目录给用户sqa设置acl访问权限时,报错, [iyunv@itpro ~]# setfacl -m u:sqa:rwx /data/ setfacl: /data/: Operation not supported
解决方法有如下三种:
方法1, 使用mount –o acl命令 此命令使一个分区临时支持ACL,但系统重启后ACL会失效。另外,由于涉及到umount操作,此方法会带来不便。
[iyunv@itpro ~]# umount /dev/mapper/vg_itpro-lv_data /data umount: /data: not mounted [iyunv@itpro ~]# mount -o acl /dev/mapper/vg_itpro-lv_data /data (注:要先卸载,再加载) [iyunv@itpro ~]# setfacl -m u:sqa:rwx /data/ [iyunv@itpro ~]# setfacl -b /data/ (注:这里成功设置acl,操作完成后清除acl)
方法2,使用tune2fs –o acl命令进行设置(tune2fs用于调整和查看文件系统的参数) 这种方法,在设置时,并不生效,需要重启系统才生效,也会带来不便。
[iyunv@itpro ~]# tune2fs -l /dev/mapper/vg_itpro-lv_root |grep acl Default mount options: user_xattr acl [iyunv@itpro ~]# tune2fs -l /dev/mapper/vg_itpro-lv_data |grep acl (注:lv_root分区的查询结果为user_xattracl,表示支持acl;而lv_data分区的查询结果为空,表示默认值不支持acl) [iyunv@itpro ~]# tune2fs -o acl /dev/mapper/vg_itpro-lv_data tune2fs 1.41.12 (17-May-2010) [iyunv@itpro ~]# tune2fs -l /dev/mapper/vg_itpro-lv_data |grep acl Default mount options: acl (注:手动给lv_data分区设置acl支持)
此时acl是不生效的,需要系统重启才生效,并且永久生效 [iyunv@itpro ~]# setfacl -m u:sqa:rwx /data/ setfacl: /data/: Operation not supported [iyunv@itpro ~]# init 6 ……重启,重新登录…… [iyunv@itpro ~]# setfacl -m u:sqa:rwx /data/ [iyunv@itpro ~]# setfacl –b
如果要恢复为不支持acl, [iyunv@itpro ~]# tune2fs -o ^acl /dev/mapper/vg_itpro-lv_data tune2fs 1.41.12 (17-May-2010) [iyunv@itpro ~]# tune2fs -l /dev/mapper/vg_itpro-lv_data |grep acl (注:acl前面用了取反符号“^”;查询结果为空,表示acl取消了,这个设置也是要系统重启才能生效)
方法3,修改/etc/fstab文件,最有用的方法 在/etc/fstab文件,lv_data条目中,在defaults后面追加acl选项,修改后内容如下: [iyunv@itpro ~]# cat /etc/fstab # /etc/fstab /dev/mapper/vg_itpro-lv_root / ext4 defaults 1 1 /dev/mapper/vg_itpro-lv_data /data ext4 defaults,acl 0 0 ……省略部分输出……
修改完成后,使用-o remount选项,重新读取分区表信息,acl即时生效,并且永久生效。 [iyunv@itpro ~]# mount -o remount /dev/mapper/vg_itpro-lv_data [iyunv@itpro ~]# setfacl -m u:sqa:rwx /data/ [iyunv@itpro ~]# setfacl -b /data/
二、ACL权限设置
对文件或者目录具体设置ACL功能以及查看具体的ACL配置, 用到setfacel 和getfacl两个命令。 下面进行ACL权限设置实验。
准备工作 先创建三个普通用户sqa、sqb、sqc及用户组admins,并将用户sqa、sqb加入到该用户组; 在/下创建待测试的文件夹dirtesta及其子文件。 [iyunv@itpro ~]# useradd sqa [iyunv@itpro ~]# useradd sqb [iyunv@itpro ~]# useradd sqc [iyunv@itpro ~]# groupadd admins [iyunv@itpro ~]# usermod -G admins sqa [iyunv@itpro ~]# usermod -G admins sqb [iyunv@itpro ~]# mkdir /dirtest [iyunv@itpro ~]# touch /dirtest/t01 [iyunv@itpro ~]# ll -d /dirtest/ drwxr-xr-x. 2 root root 4096 Mar 12 01:08 /dirtest/ [iyunv@itpro ~]# ll /dirtest/ total 4 -rw-r--r--. 1 root root 17 Mar 5 07:04 t01
实验一 求要:其他用户对文件夹dirtest有读写权限,但用户sqa没有任何权限 解决方法:给ugo中的o开放读写权限,使用acl限制特定用户sqa
1.先查看当前dirtest的acl [iyunv@itpro ~]# getfacl /dirtest/ getfacl: Removing leading '/' from absolute path names # file: dirtest/ # owner: root # group: root user::rwx group::r-x other::r-x [iyunv@itpro ~]# ll -d /dirtest/ drwxr-xr-x. 2 root root 4096 Mar 12 01:08 /dirtest/
2.更改dirtest的访问权限 [iyunv@itpro ~]# chmod o+w /dirtest/ (注:给其他用户增加“写”权限) [iyunv@itpro ~]# setfacl -m u:sqa:--- /dirtest/ (注:不给用户sqa任何权限)
3.再次查看当前的dirtest权限 [iyunv@itpro ~]# getfacl /dirtest/ getfacl: Removing leading '/' from absolute path names # file: dirtest/ # owner: root # group: root user::rwx user:sqa:--- group::r-x mask::r-x other::rwx [iyunv@itpro ~]# ll -d /dirtest/ drwxr-xrwx+ 2 root root 4096 Mar 12 01:08 /dirtest/ (注:drwxr-xrwx+,这里仍然是按ugo权限排序,最后的“+”,表示设置了acl)
4.测试 切换到用户sqa [iyunv@itpro ~]# su sqa [sqa@itpro root]$ ls /dirtest/ ls: cannot open directory /dirtest/: Permission denied [sqa@itpro root]$ cd /dirtest/ bash: cd: /dirtest/: Permission denied
切换到用户sqb [sqa@itpro root]$ exit exit [iyunv@itpro ~]# su sqb [sqb@itpro root]$ ls /dirtest/ t01 [sqb@itpro root]$ cd /dirtest/ [sqb@itpro dirtest]$ touch t02 [sqb@itpro root]$ ls /dirtest/ t01 t02
切换回root用户 [sqb@itpro root]$ exit exit [iyunv@itpro ~]#
实验二 在实验一的基础上, 要求:用户组admins(包括sqa和sqb两个用户)对dirtest没有写权限,但用户sqb有写权限。 解决方法:使用acl赋予用户组admins 权限为r-x,赋予用户sqa权限为rwx
1.在实验一中,对于dirtest文件夹,用户sqa权限为---,是acl控制的;用户sqb权限为rwx,不是acl控制的; 现在先对用户组admins设置acl [iyunv@itpro ~]# setfacl -m g:admins:r-x /dirtest/ [iyunv@itpro ~]# getfacl /dirtest/ getfacl: Removing leading '/' from absolute path names # file: dirtest/ # owner: root # group: root user::rwx user:sqa:--- group::r-x group:admins:r-x mask::r-x other::rwx (注:此时,用户sqa的权限仍然是---,用户sqb的权限则随用户组admins为r-x 测试结果如下: [iyunv@itpro ~]# su sqa [sqa@itpro root]$ ls /dirtest/ ls: cannot open directory /dirtest/: Permission denied [sqa@itpro root]$ exit exit [iyunv@itpro ~]# su sqb [sqb@itpro root]$ ls /dirtest/ t01 t02 [sqb@itpro root]$ touch /dirtest/t03 touch: cannot touch `/dirtest/t03': Permission denied [sqb@itpro root]$ exit exit [iyunv@itpro ~]# 以上是测试结果)
2.删除实验一中对用户sqa设置的acl (注:在实验一步骤2中,使用命令setfacl -m u:sqa:--- /dirtest/ 对用户sqa设置了权限) [iyunv@itpro ~]# setfacl -x u:sqa /dirtest/ [iyunv@itpro ~]# getfacl /dirtest/ getfacl: Removing leading '/' from absolute path names # file: dirtest/ # owner: root # group: root user::rwx group::r-x group:admins:r-x mask::r-x other::rwx (注:如果没有做实验一,直接做实验二,对用户组admins设置acl后,看到的结果便是这个,当然,other权限默认应为r-w,实验一中改了)
3.对用户sqb设置acl,使其对dirtest具有rwx权限 [iyunv@itpro ~]# setfacl -m u:sqb:rwx /dirtest/ [iyunv@itpro ~]# getfacl /dirtest/ getfacl: Removing leading '/' from absolute path names # file: dirtest/ # owner: root # group: root user::rwx user:sqb:rwx group::r-x group:admins:r-x mask::rwx other::rwx [iyunv@itpro ~]# ll -d /dirtest/ drwxrwxrwx+ 2 root root 4096 Mar 12 01:37 /dirtest/ (注:drwxrwxrwx+,这里不是按ugo权限排序,第一组rwx是文件所有者root的权限,第二组rwx是用户sqb的权限,第三组rwx是其他用户的权限,“+”表示acl设置的权限。 如果将其他用户的权限改回默认的r-x,会更易理解,如: [iyunv@itpro ~]# chmod o-w /dirtest/ [iyunv@itpro ~]# ll -d /dirtest/ drwxrwxr-x+ 2 root root 4096 Mar 12 01:37 /dirtest/ )
4.测试 依次切换到用户sqa、sqb、sqc,查看其对dirtest文件夹的操作权限, [iyunv@itpro ~]# su sqa [sqa@itpro root]$ ls /dirtest/ t01 t02 [sqa@itpro root]$ touch /dirtest/t03 touch: cannot touch `/dirtest/t03': Permission denied [sqa@itpro root]$ exit exit [iyunv@itpro ~]# su sqb [sqb@itpro root]$ ls /dirtest/ t01 t02 [sqb@itpro root]$ touch /dirtest/t03 [sqb@itpro root]$ ls /dirtest/ t01 t02 t03 [sqb@itpro root]$ exit exit [iyunv@itpro ~]# su sqc [sqc@itpro root]$ ls /dirtest/ t01 t02 t03 [sqc@itpro root]$ touch /dirtest/t04 [sqc@itpro root]$ ls /dirtest/ t01 t02 t03 t04 [sqc@itpro root]$ ll /dirtest/ total 8 -rw-r--r--. 1 root root 17 Mar 5 07:04 t01 -rw-rw-r--. 1 sqb sqb 0 Mar 12 01:37 t02 -rw-rw-r--. 1 sqb sqb 0 Mar 12 02:22 t03 -rw-rw-r--. 1 sqc sqc 0 Mar 12 02:24 t04 [sqc@itpro root]$ exit exit (注:由于sqc不在用户组admins,所在sqc的权限为其他用户(ugo中的o)的权限,即rwx(实验一中设置的))
至此,实验结束,如果要恢复dirtest的访问权限为实验前的权限,可进行以下操作: [iyunv@itpro ~]# setfacl -b /dirtest/ (注:-b选项,表示清除指定对象的所有acl设置,慎用) [iyunv@itpro ~]# chmod o-w /dirtest/ (注:other的权限也改回r-x) [iyunv@itpro ~]# ll -d /dirtest/ drwxr-xr-x. 2 root root 4096 Mar 12 02:24 /dirtest/ [iyunv@itpro ~]# getfacl /dirtest/ getfacl: Removing leading '/' from absolute path names # file: dirtest/ # owner: root # group: root user::rwx group::r-x other::r-x (注:可以看到,此时的文件权限恢复到常规状态)
|