Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1114110
  • 博文数量: 309
  • 博客积分: 6093
  • 博客等级: 准将
  • 技术积分: 3038
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-03 17:14
个人简介

linux学习记录

文章分类

全部博文(309)

文章存档

2014年(2)

2012年(37)

2011年(41)

2010年(87)

2009年(54)

2008年(88)

分类:

2010-08-25 15:36:03

                     RHCE学习笔记

 

下面是关于linux下面文件ACL权限的讨论,

 

我们知道,在linux系统中,我们将访问文件的用户分为三类,usergroupother

如果系统中某个用户想对某个文件有写入的权限,但是该用户是属于other,如果是这样,就只能够开放other的权限。但是一旦开放other的权限,那么所有人就对该文件有写入的权限了。文件的ACL权限就很好的解决了这个问题,它可以设置一个特定文件的特定权限。

 

下面来具体的设置文件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 21: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]$

可以看到,user1otheruser1redhat这个目录果然没有写入的权限。

如果我们想要给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]#

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

 

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

[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权限给复制过去。

 

linux下面文件ACL权限的讨论就到这里。

阅读(597) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~