日期:2014-05-16  浏览次数:20649 次

linux之ACL授权

ACL授权比chmod更加灵活,也更加精细化,在进行授权操作时,推荐使用ACL方式。

?

一.添加权限

setfacl -m u:username:rwx dir/file ?#此时会看的dir的权限多了一个加号

-m:添加或者修改

u[设置用户]:username[用户名]:rwx[设置权限]

g[设置组]:groupname[组名]:rwx[设置权限]

?

setfacl -m u:username:r -R dir #同时设置子目录下的权限

-R : 递归设置子目录下的权限

?

setfacl -m mask::r-- dir/file #设置mask的值,mask用于限制ACL_USER, ACL_GROUP和ACL_GROUP_OBJ的最大值

ACL_USER_OBJ : user::rwx ?#文件所有者

ACL_USER:user : user:username:rwx

ACL_GROUP_OBJ : group::r-x #文件所属组

ACL_GROUP : group:groupname:r-x

?

?

二.查看权限

getfacl dir/file ?#查看该目录被设置的所有acl权限

--omit-header :去掉头信息

?

三.删除权限

setfacl -x u:username dir/file #删除用户的权限

setfacl -x g:groupname dir/file #删除组的权限

setfacl -b dir/file ?#删除该目录的全部acl权限

?

?

四.缺省的ACL设置

说明:如果希望在dir下建立的所有文件都可以被某一用户访问. 那么我们就应该对dir目录设置Default ACL。

setfacl -d -m user:username:rw dir

-d : 设置default acl

这样,任何人在该目录下创建的文件,都可以被username读写。

?

setfacl -k dir # 删除缺省acl设置

?

?

五.示例:

1.文件

1)ll -d testfile

drwxr-xr-x 2 root root 4096 1月 ?20 18:59 testfile

?

2)setfacl -m u:hanqunfeng:rwx testfile

?

3)ll -d testfile

drwxrwxr-x+ 2 root root 4096 1月 ?20 18:59 testfile

?

4)getfacl testfile

# file: test

# owner: root

# group: root

user::rwx

user:hanqunfeng:rwx

group::r-x

mask::rwx

other::r-x

?

说明:缺省的mask值也被设定为rwx.那是因为它规定了ACL_USER[user:hanqunfeng:rwx], ACL_GROUP和ACL_GROUP_OBJ[group::r-x]的最大值。

?

5)setfacl -m mask::r-- testfile

6)getfacl test

# file: test

# owner: root

# group: root

user::rwx

user:hanqunfeng:rwx ? ? ? ? ? ? #effective:r--

group::r-x ? ? ? ? ? ? ? ? ? ? ?#effective:r--

mask::r--

other::r-x

?

说明:此时会看到相应的权限后面多出了#effective:r--,因为mask规定了ACL_USER, ACL_GROUP和ACL_GROUP_OBJ的最大值,所以实际的权限也是r--。

?

7)getfacl --omit-header testfile #去掉头信息

user::rwx

user:hanqunfeng:rwx ? ? ? ? ? ? #effective:r--

group::r-x ? ? ? ? ? ? ? ? ? ? ?#effective:r--

mask::r--

other::r-x

?

8)setfacl -x u:hanqunfeng testfile

?

9)ll -d testfile

drwxr-xr-x+ 2 root root 4096 1月 ?20 18:59 testfile

?

10)setfacl -b testfile

?

11)ll -d testfile

drwxr-xr-x 2 root root 4096 1月 ?20 18:59 testfile

?

2.目录

1)setfacl -m u:hanqunfeng:rwx -R testdir

?

2)ll -d testdir

drwxrwxr-x+ 2 root root 4096 1月 ?20 18:59 testdir

?

3)getfacl --omit-header testdir

user::rwx

user:hanqunfeng:rwx

group::r-x

mask::rwx

other::r-x

?

4)setfacl -d -m u:hanqunfeng:rwx -R testdir

?

5)getfacl --omit-header testdir

user::rwx

user:hanqunfeng:rwx

group::r-x

mask::rwx

other::r-x

default:user::rwx

default:user:hanqunfeng:rwx

default:group::r-x

default:mask::rwx

default:other::r-x

?

6)setfacl -b testdir

?

7)ll -d testfile

drwxr-xr-x 2 root root 4096 1月 ?20 18:59 testdir

?

?