Linux中通过setfacl实现目录下创建文件

在Linux 系统中,可以通过setfacl 来实现目录下创建文件或子目录,并继承父目录的权限。

下面以 root 用普通用户 user1 .在目录/mnt下

[root@RedHat-7 mnt]# setfacl -m u:user1:rwx share        //为目录添加ower = user1 ,并赋予rwx 的权根。
[root@redhat-7 mnt]# setfacl -d -m  u:user1:rwx share    //为目录添加默认的acl权限,此目录下创建目录和文件都会继承此权限信息
[root@redhat-7 mnt]#
[root@redhat-7 mnt]#
[root@redhat-7 mnt]# getfacl share        //查看share的facl信息
# file: share
# owner: root
# group: root
user::rwx
user:user1:rwx                            // 此时user1才能对share有写权限
group::r-x
mask::rwx
other::r-x
default:user::rwx                        //默认子目录或文件的权限信息
default:user:user1:rwx
default:group::r-x
default:mask::rwx
default:other::r-x
[root@redhat-7 mnt]# cd share/
[root@redhat-7 share]# ls
[root@redhat-7 share]# touch roota
[root@redhat-7 share]# touch rootb
[root@redhat-7 share]# mkdir rootdir1
[root@redhat-7 share]# mkdir rootdir2
[root@redhat-7 share]# ls
roota  rootb  rootdir1  rootdir2
[root@redhat-7 share]# getfacl roota
# file: roota
# owner: root
# group: root
user::rw-
user:user1:rwx                  #effective:rw-
group::r-x                      #effective:r--
mask::rw-
other::r--
[root@redhat-7 share]# getfacl rootdira
getfacl: rootdira: No such file or directory
[root@redhat-7 share]# getfacl rootdir
rootdir1/ rootdir2/
[root@redhat-7 share]# getfacl rootdir1
# file: rootdir1
# owner: root
# group: root
user::rwx
user:user1:rwx
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:user1:rwx
default:group::r-x
default:mask::rwx
default:other::r-x
[root@redhat-7 share]#

现在用user1 进入此目录来检测权限:

[user1@redhat-7 share]$

[user1@redhat-7 share]$

[user1@redhat-7 share]$

[user1@redhat-7 share]$ ll

total 8

-rw-rw-r--+ 1 root root 0 Nov 19 22:52 roota

-rw-rw-r--+ 1 root root 0 Nov 19 22:52 rootb

drwxrwxr-x+ 2 root root 6 Nov 19 22:52 rootdir1

drwxrwxr-x+ 2 root root 6 Nov 19 22:52 rootdir2

[user1@redhat-7 share]$ touch user1a

[user1@redhat-7 share]$ touch user1dir1

[user1@redhat-7 share]$ getfacl user1a        //查看新文件的权限

# file: user1a

# owner: user1

# group: user1

user::rw-

user:user1:rwx                  #effective:rw-

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

mask::rw-

other::r--


[user1@redhat-7 share]$ rm roota        //尝试删除root创建的文件,成功

[user1@redhat-7 share]$ rm rootdir1    //尝试删除root创建的目录,成功

rm: cannot remove arootdir1a: Is a directory

[user1@redhat-7 share]$ rm rootdir1 -r

[user1@redhat-7 share]$ ll

total 4

-rw-rw-r--+ 1 root  root  0 Nov 19 22:52 rootb

drwxrwxr-x+ 2 root  root  6 Nov 19 22:52 rootdir2

-rw-rw-r--+ 1 user1 user1 0 Nov 19 22:57 user1a

-rw-rw-r--+ 1 user1 user1 0 Nov 19 22:57 user1dir1

[user1@redhat-7 share]$

所以,当有需求要做权根的继承操作时,可以使用这个方来实现。

在新的rhel7 中, 会有新的工具chacl 类似于 setfacl .

setfacl实现Linux文件权限继承

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/17234.html