简介

用户权限管理始终是 Unix 系统管理中最重要的环节。大家对 Linux/Unix 的 UGO 权限管理方式一定不陌生,还有最常用的 chmod 命令。为了实现一些比较复杂的权限管理,往往不得不创建很多的组,并加以详细的记录和区分(这是一件很蛋疼的事情),那么有没有办法对指定的用户进行更细的权限划分呢?答案是有的!那就是ACL,ACL是Access Control List(访问控制列表),对某个文件或者目录设置的访问控制列表,可以对任意的用户/组设置具体的rwx权限

ACL的支持

ACL需要文件系统和内核的支持,在使用ACL之前,我们应该首先检查文件系统和内核是否支持ACL,在内核方面,内核2.6+版本都支持ACL;在文件系统方面,EXT2/EXT3, JFS, XFS, ReiserFS等文件系统都是可以支持ACL的

检查内核版本是否满足要求

# uname -r
2.6.18-308.el5

检查文件系统类型和是否已经启用ACL

# dumpe2fs /dev/sda2|grep 'mount options'
dumpe2fs 1.39 (29-May-2006)
Default mount options:    user_xattr acl

从上面可以看出默认挂载选项已经启用了acl功能,如果没有启用的话,可以使用以下命令临时启用acl功能

启用acl功能
# tune2fs -o acl /dev/sda2
或者
# mount -o remount,acl /dev/sda2
在开机时自动挂载acl选项,例如设置/home
LABEL=/home   /home     ext3    defaults,acl     1 2
关闭acl功能
# tune2fs -o ^acl /dev/sda2

经过上面的设置,文件系统已经启用了acl功能的支持

acl设置命令的简单使用

setfacl:用于设置acl

语法:setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ...

选项:

-m:modify,修改文件的acl

-n:不重新计算有效的mask,给新用户设置acl时,使用-n,则新用户的权限与文件或目录的属组权限相同,如果不使用-n,则setfacl默认会重新计算mask,且mask与你给用户设置的acl的权限相同

   例:使用-n选项给user2用户设置目录/home/test/testdir的acl权限为rwx,但实际生效权限为r-x;然后不使用-n选项重新设置acl,实际生效的权限为rwx,结果如下:


[root@localhost ~]# getfacl /home/test/testdir/
getfacl: Removing leading '/' from absolute path names
# file: home/test/testdir
# owner: user1
# group: user1
user::rwx
group::r-x
other::r-x
[root@localhost ~]# setfacl -n -m u:user2:rwx /home/test/testdir/
[root@localhost ~]# getfacl /home/test/testdir/
getfacl: Removing leading '/' from absolute path names
# file: home/test/testdir
# owner: user1
# group: user1
user::rwx
user:user2:rwx          #effective:r-x
group::r-x
mask::r-x
other::r-x
[root@localhost ~]# su  user2 -c "touch /home/test/testdir/user2file"
touch: 无法触碰 “/home/test/testdir/user2file”: 权限不够
[root@localhost ~]# setfacl -m u:user2:rwx /home/test/testdir/
[root@localhost ~]# getfacl /home/test/testdir/
getfacl: Removing leading '/' from absolute path names
# file: home/test/testdir
# owner: user1
# group: user1
user::rwx
user:user2:rwx
group::r-x
mask::rwx
other::r-x
[root@localhost ~]# su  user2 -c "touch /home/test/testdir/user2file"
[root@localhost ~]# ll /home/test/testdir/user2file
-rw-r--r-- 1 user2 user2 0 04-26 04:36 /home/test/testdir/user2file


-b:移除文件所有额外的acl,包括默认acl,但保留文件属主、属组、other的acl

   例:先设置目录/home/test/testdir的属主、属组、other的acl,再移除目录/home/test/testdir的所有acl,结果如下,文件属主、属组、other的acl没有被移除。


[root@localhost ~]# setfacl -m u::r-- /home/test/testdir/
[root@localhost ~]# setfacl -m g::-w- /home/test/testdir/
[root@localhost ~]# setfacl -m o::--x /home/test/testdir/
[root@localhost ~]# getfacl /home/test/testdir/
getfacl: Removing leading '/' from absolute path names
# file: home/test/testdir
# owner: user1
# group: user1
user::r--
user:user2:rwx
group::-w-
mask::rwx
other::--x
[root@localhost ~]# setfacl -b /home/test/testdir/
[root@localhost ~]# getfacl /home/test/testdir/
getfacl: Removing leading '/' from absolute path names
# file: home/test/testdir
# owner: user1
# group: user1
user::r--
group::-w-
other::--x


-d:按我理解,这个选项应用的对象应该是目录,配置用户在该目录下所创建的文件的默认权限

   例:设置用户user2在目录/home/test/testdir下所创建的文件的默认权限为444


[root@localhost ~]# setfacl -m u:user2:rwx /home/test/testdir
[root@localhost ~]# setfacl -d -n -m u::r-- /home/tes/testdir
[root@localhost ~]# setfacl -d -n -m g::r-- /home/tes/testdir
[root@localhost ~]# setfacl -d -m o::r-- /home/test/testdir/
[root@localhost ~]# getfacl /home/test/testdir/
getfacl: Removing leading '/' from absolute path names
# file: home/test/testdir
# owner: user1
# group: user1
user::rwx
user:user2:rwx
group::r-x
mask::rwx
other::r-x
default:user::r--
default:group::r--
default:other::r--
user2用户在home/test/testdir目录下创建文件,所创建的文件的默认权限是444(只读),测试结果如下:
[root@localhost ~]# su user2 -c "touch /home/test/testdir/file1"
[root@localhost ~]# su user2 -c "mkdir /home/test/testdir/dir1"
[root@localhost ~]# ll /home/test/testdir/
total 8
dr--r--r--+ 2 user2 user2 4096 Apr 26 06:55 dir1
-r--r--r--  1 user2 user2    0 Apr 26 06:55 file1

-k:移除默认的acl,如果默认的acl不存在也不会警告

   例:


下面这个指令不会提示警告信息
[root@localhost ~]# setfacl -k  /home/test/testdir/
[root@localhost ~]# getfacl /home/test/testdir/
getfacl: Removing leading '/' from absolute path names
# file: home/test/testdir
# owner: user1
# group: user1
user::rwx
group::r-x
other::r-x
[root@localhost ~]# setfacl -k  /home/test/testdir/
[root@localhost ~]# setfacl -d -m u::rwx /home/test/testdir/
[root@localhost ~]# getfacl /home/test/testdir/
getfacl: Removing leading '/' from absolute path names
# file: home/test/testdir
# owner: user1
# group: user1
user::rwx
group::r-x
other::r-x
default:user::rwx
default:group::r-x
default:other::r-x
[root@localhost ~]# setfacl -k  /home/test/testdir/
[root@localhost ~]# getfacl /home/test/testdir/
getfacl: Removing leading '/' from absolute path names
# file: home/test/testdir
# owner: user1
# group: user1
user::rwx
group::r-x
other::r-x


-x:移除指定用户或组的acl,但不会移除默认的acl,用法:setfacl -x [u|g]:[uid|gid]  /path/to/filename


[root@localhost ~]# getfacl /home/test/testdir/
getfacl: Removing leading '/' from absolute path names
# file: home/test/testdir
# owner: user1
# group: user1
user::rwx
user:user3:rwx
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:user3:rwx
default:group::r-x
default:mask::rwx
default:other::r-x
[root@localhost ~]# setfacl -x u:user3 /home/test/testdir/
[root@localhost ~]# getfacl /home/test/testdir/
getfacl: Removing leading '/' from absolute path names
# file: home/test/testdir
# owner: user1
# group: user1
user::rwx
group::r-x
mask::r-x
other::r-x
default:user::rwx
default:user:user3:rwx
default:group::r-x
default:mask::rwx
default:other::r-x

-M:可以读取acl文件或通过标准输入来设置acl,下面演示通过标准输入来复制一个文件的acl到另一个文件的


[root@localhost ~]# getfacl /home/test/testdir/
getfacl: Removing leading '/' from absolute path names
# file: home/test/testdir
# owner: user1
# group: user1
user::rwx
user:user4:rwx
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:user3:rwx
default:user:user4:r--
default:group::r-x
default:group:user4:r--
default:mask::rwx
default:other::r-x
[root@localhost ~]# mkdir dir4
[root@localhost ~]# getfacl /home/test/testdir/ |setfacl -M - dir4
getfacl: Removing leading '/' from absolute path names
[root@localhost ~]# getfacl dir4/
# file: dir4
# owner: root
# group: root
user::rwx
user:user4:rwx
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:user3:rwx
default:user:user4:r--
default:group::r-x
default:group:user4:r--
default:mask::rwx
default:other::r-x


新手写博客,目前只了解到这些,可能有些错误,欢迎大家拍砖j_0018.gif