linux下的ACL权限管理

 ACL 是 Access Control List 的缩写,主要的目的是在提供传统的 owner,group,others 的read,write,execute 权限之外的详细权限训定。ACL 可以针对单一使用者,单一档案或目录来进行r,w,x 的权限设定,对于需要特殊权限的使用状况非常有帮助

下面来具体的设置文件ACL权限。

如果想拥有文件ACL的权限,就必须为文件系统添加一个ACL属性。

[root@localhost ~]#

[root@localhost ~]# mount -o remount,acl /dev/sda9

[root@localhost ~]#

[root@localhost ~]# mount |  grep /dev/sda9

/dev/sda9 on /mnt type ext3 (rw,acl)

[root@localhost ~]#

[root@localhost ~]#

可以看到,/dev/sda9已经有了ACL属性。

现在我们新建一个redhat目录。

[root@localhost ~]#

[root@localhost ~]# cd /mnt/

[root@localhost mnt]#

[root@localhost mnt]# mkdir redhat

[root@localhost mnt]#

[root@localhost mnt]# ll -ld /redhat/

drwxr-xr-x 2 root root 4096 Feb 26 20:56 /redhat/

[root@localhost mnt]#

可以看到,这个redhat目录的权限为755,这个目录的拥有人和拥有组都是root,也就是说其他人对这个目录是没有写入的权限的。

[root@localhost ~]# su – user1

[user1@localhost ~]$

[user1@localhost ~]$ cd /mnt/

[user1@localhost mnt]$ ls

lost+found  redhat

[user1@localhost mnt]$ cd redhat/

[user1@localhost redhat]$ touch file

touch: cannot touch `file’: Permission denied

[user1@localhost redhat]$ ls

[user1@localhost redhat]$

可以看到,user1是other,user1对redhat这个目录果然没有写入的权限。

如果我们想要给user1对这个目录有一个rwx的权限。除了开放other的权限,我们还可以使用文件的ACL功能。

现在我们就给user1对该目录设置ACL权限。

#setfacl   -m   u:user1:rwx  /mnt/redhat/

-m         修改

u           用户

user1     指定用户

rwx       指定权限

/redhat  指定目录

[root@localhost ~]#

[root@localhost ~]# cd /mnt/

[root@localhost mnt]#

[root@localhost mnt]# setfacl -m u:user1:rwx /mnt/redhat/

[root@localhost mnt]#

[root@localhost mnt]# ll

total 14

drwx——  2 root root 12288 Feb 26 21:53 lost+found

drwxrwxr-x+ 2 root root  1024 Feb 26 22:11 redhat

[root@localhost mnt]#

我们已经给redhat目录设置了ACL权限,通过ll可以看到,redhat目录的权限位后面多了一个加号,这个加号就代表不要看表面的权限,说明还有更深的权限隐藏在里面。

如何去查看这个文件的具体权限呢

#Getfacl  /mnt/redhat

[root@localhost mnt]#

[root@localhost mnt]# getfacl /mnt/redhat

getfacl: Removing leading ‘/’ from absolute path names

# file: mnt/redhat

# owner: root

# group: root

user::rwx

user:user1:rwx

group::r-x

mask::rwx

other::r-x

[root@localhost mnt]#

我们可以看到文件的拥有人和拥有组都是root,user的权限是rwx,group的权限也r-x,other的权限是r-x。而且最重要的是user:user1:rwx,也就是user1这个用户对该目录也有rwx的权限。Mask代表用户的实际权限。

现在我们利用user1到redhat目录里面尝试去写入。

[root@localhost ~]#

[root@localhost ~]# su – user1

[user1@localhost ~]$

[user1@localhost ~]$ cd /mnt/redhat/

[user1@localhost redhat]$

[user1@localhost redhat]$ mkdir file

[user1@localhost redhat]$ ls

file

[user1@localhost redhat]$

可以看到,刚才我们是不可以往里面写入的,但是现在我们就写入成功了。

刚才我们对user1这个用户做了文件的ACL权限。

现在我们对组来做文件的ACL权限。

#setfacl  -m  g:user1:rwx   /mnt/redhat/

[root@localhost ~]#

[root@localhost ~]# setfacl -m g:user1:rwx /mnt/redhat/

[root@localhost ~]#

[root@localhost ~]# getfacl /mnt/redhat/

getfacl: Removing leading ‘/’ from absolute path names

# file: mnt/redhat

# owner: root

# group: root

user::rwx

user:user1:rwx

group::r-x

group:user1:rwx

mask::rwx

other::r-x

[root@localhost ~]#

现在user1这个组里面的成员对/mnt/redhat目录也有rwx权限了。

下面还有个关于文件ACL功能的问题

刚才我们用user1在/mnt/redhat目录下面创建了一个file的文件夹,说明我们对redhat这个目录有rwx的权限,但是如果在redhat目录下面还有一个文件夹,我们是否对它有rwx的权限呢。试下,

[root@localhost ~]#

[root@localhost ~]# cd /mnt/redhat/

[root@localhost redhat]# ls

file

[root@localhost redhat]# mkdir file1

[root@localhost redhat]# cd

[root@localhost ~]#

[root@localhost ~]# su – user1

[user1@localhost ~]$ cd /mnt/redhat/

[user1@localhost redhat]$ cd file1

[user1@localhost file1]$ touch 1.txt

touch: cannot touch `1.txt’: Permission denied

[user1@localhost file1]$

很显然,我们对redhat目录下面的子目录是没有rwx的权限的。也就是说文件的ACL功能只对当前目录生效,并不可以对子目录生效。

看下这条命令的作用,

# setfacl -m d:u:user2:rwx /mnt/redhat/

[root@localhost ~]#

[root@localhost ~]# setfacl -m d:u:user2:rwx /mnt/redhat/

[root@localhost ~]#

这个时候,我们同样给user2一个rwx的权限。

[root@localhost ~]#

[root@localhost ~]# su – user2

[user2@localhost ~]$

[user2@localhost ~]$ cd /mnt/redhat/

[user2@localhost redhat]$

[user2@localhost redhat]$ mkdir file100

mkdir: cannot create directory `file100′: Permission denied

[user2@localhost redhat]$

但是现在我们不可以往/mnt/redhat目录里面写入数据,就是因为多了一个d,这个d代表default。

我们现在来看看它的用处是什么。

[root@localhost ~]#

[root@localhost ~]# cd /mnt/redhat/

[root@localhost redhat]# mkdir test

[root@localhost redhat]# ll

total 4

drwxrwxr-x  2 user1 user1 1024 Feb 26 22:44  file

drwxr-xr-x   2 root  root  1024 Feb 26 22:46 file1

drwxrwxr-x+ 2 root  root  1024 Feb 26 22:58 test

[root@localhost redhat]#

现在可以看到,test这个目录权限位上面多了一个加号,看下它的具体权限。

[root@localhost redhat]#

[root@localhost redhat]# getfacl test/

# file: test

# owner: root

# group: root

user::rwx

user:user2:rwx

group::r-x

mask::rwx

other::r-x

default:user::rwx

default:user:user2:rwx

default:group::r-x

default:mask::rwx

default:other::r-x

[root@localhost redhat]#

可以看到,user2这个用户对该目录有rwx的权限。尝试往test目录里面写入数据。

[root@localhost ~]#

[root@localhost ~]# su – user2

[user2@localhost ~]$

[user2@localhost ~]$ cd /mnt/redhat/

[user2@localhost redhat]$

[user2@localhost redhat]$ cd test/

[user2@localhost test]$

[user2@localhost test]$ touch 1.txt

[user2@localhost test]$ ls

1.txt

[user2@localhost test]$

写入成功了,刚才我们只是对/redhat目录做了文件的ACL功能,为什么现在user2对其子目录会有rwx的权限呢,这个就是因为刚才d参数的含义,就是递归的含义,对当前目录无效果,对子目录才有效果。

不管是任何用户向/mnt/redhat目录建立的数据,user2都会继承它们的权限。

这条命令只可以针对目录来做ACL权限。不可以对文件做,因为对文件做没有意义。文件不可能有下一级目录的。

如果文件的拥有人和拥有组和文件的ACL权限发生冲突的时候,以拥有人和拥有组为准,其实文件的ACL功能只对other人有意义。

如何移除文件的ACL权限呢

#setfacl -k /mnt/redhat/

移除文件的默认ACL属性

#setfacl –b /mnt/redhat

移除文件的所有ACL权限

#setfacl –x u:user1 /mnt/redhat

移除user1对文件的ACL权限

当我们在复制文件的时候会,如果使用-p的参数,也一样可以将文件的ACL权限给复制过去。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章