linux中使用ACL保护文件目录​
2021-09-24 10:29:06 Author: mp.weixin.qq.com(查看原文) 阅读量:30 收藏


文章来源:入门小站

ACl (Access Control List),主要目的是提供传统的 owner、group、others 的 read、write、execute 权限之外的特殊权限需求设置。ACL 可以针对单一使用者、单一文件或目录来进行 r、w、x 的权限规范,对于需要特殊权限的使用状况非常有帮助

ACl 主要针对以下方面来控制权限:

  • 使用者 user
  • 群组 group
  • 默认属性 mask:针对在该目录下在建立新文件/目录时,规范新数据的默认权限

有一个目录,给一堆人使用,每个人或每个群组所需要的权限并不相同,使用传统 Linux 三种身份的三种权限是无法达到的,因此基本上,传统的 Linux 权限只能针对一个用户、一个群组以及非此群组的其他人设置权限。无法针对单一用户或个人来设计权限。

Note:根据 Redhat 产品文档,它为 ext3 文件系统和 NFS 导出文件系统提供 ACL 支持。

如何检查 Linux 系统中的 ACL 支持

在继续之前,你应该在当前内核和挂载的文件系统上支持 ACL。

1. 检查内核是否支持 ACL

运行以下命令检查文件系统的 ACL 支持和 POSIX_ACL=Y 选项(如果有 N 代替 Y,则表示内核不支持ACL,需要重新编译)。

[root@linux ~]# grep -i acl /boot/config*

CONFIG_`EXT4_FS_POSIX_ACL=y`
CONFIG_`REISERFS_FS_POSIX_ACL=y`
CONFIG_JFS_POSIX_ACL=y
CONFIG_XFS_POSIX_ACL=y
CONFIG_BTRFS_FS_POSIX_ACL=y
CONFIG_FS_POSIX_ACL=y
CONFIG_GENERIC_ACL=y
CONFIG_TMPFS_POSIX_ACL=y
CONFIG_NFS_V3_ACL=y
CONFIG_NFSD_V2_ACL=y
CONFIG_NFSD_V3_ACL=y
CONFIG_NFS_ACL_SUPPORT=m
CONFIG_CIFS_ACL=y
CONFIG_9P_FS_POSIX_ACL=y

2.检查所需的包

在开始使用 ACL 之前,请确保你已安装所需的软件包。以下是需要使用安装的必需软件包yum 或者 apt-get.

[root@linux ~]# yum install nfs4-acl-tools acl libacl  [on `RedHat` based systems]
[rumenz@linux ~]$ sudo apt-get install nfs4-acl-tools acl [on `Debian` based systems]

3. 检查挂载的文件系统是否支持 ACL

现在,检查挂载的文件系统是否使用 ACL 选项挂载。我们可以用mount 用于检查相同的命令,如下所示。

[root@linux ~]# mount  | grep -i root
/dev/mapper/fedora-root on / type ext4 (rw,relatime,data=ordered)

但在我们的例子中,它默认不显示 acl。因此,接下来我们可以选择使用 acl 选项再次重新挂载已安装的分区。但是,在继续之前,我们有另一个选项来确保分区是否使用 acl 选项挂载,因为对于最近的系统,它可能与默认挂载选项集成。

[root@linux ~]# tune2fs -l /dev/mapper/fedora-root | grep acl
Default mount options:    user_xattr acl

在上面的输出中,你可以看到默认挂载选项已经支持 acl。另一种选择是重新挂载分区,如下所示。

[root@linux ~]# mount -o remount,acl /

接下来,将以下条目添加到/etc/fstab文件中以使其永久化。

/dev/mapper/fedora-root / ext4    defaults,acl 1 1

再次,重新挂载分区。

[root@linux ~]# mount -o remount  /

4.对于NFS服务器

在 NFS 服务器上,如果 NSF 服务器导出的文件系统支持 ACL,并且 NFS 客户端可以读取 ACL,那么客户端系统就会使用 ACL。

要禁用 NFS 共享上的 ACL,你必须添加选项no_acl 在 /etc/exportfs NFS 服务器上的文件。要再次在 NSF 客户端禁用它,请使用no_acl 挂载时间的选项。

如何在 Linux 系统中实现 ACL 支持

有两种类型 ACLs

  1. Access ACLs:访问 ACL 用于授予对任何文件或目录的权限。
  2. Default ACLs:默认 ACL 仅用于在特定目录上授予/设置访问控制列表。

访问 ACL 和默认 ACL 的区别:

  1. 默认 ACL 只能在目录级别使用。
  2. 在该目录中创建的任何子目录或文件都将从其父目录继承 ACL。另一方面,文件继承默认 ACL 作为其访问 ACL。
  3. 我们利用–d用于设置默认 ACL,默认 ACL 是可选的。
设置默认 ACL 之前

要确定特定文件或目录的默认 ACL,请使用 getfacl 命令。在下面的例子中,getfacl 用于获取文件夹的默认 ACL Music

[root@linux ~]# getfacl Music/

# file: Music/
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
default:user::rwx
default:group::r-x
default:other::rw-

设置默认 ACL 后

要为特定文件或目录设置默认 ACL,请使用 setfacl 命令。在下面的例子中,setfacl 命令将设置一个新的 ACL(read 和 execute) 给文件夹 Music.

[root@linux ~]# setfacl -m d:o:rx Music/
[root@linux ~]# getfacl Music/
# file: Music/
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
`default:user::rwx
default:group::r-x
default:other::r-x`

如何设置新的 ACL

使用 setfacl用于设置或修改任何文件或目录的命令。例如,rumenz1用户赋予read和 write 的权限。

# setfacl -m u:rumenz1:rw /rumenz1/example

如何查看 ACL

使用 getfacl 用于查看任何文件或目录上的 ACL 的命令。例如,要查看  上的 ACL/rumenz1/example 使用下面的命令。

# getfacl /rumenz1/example

# file: rumenz1/example/
# owner: rumenz1
# group: rumenz1
user::rwx
user:rumenz1:rwx
user:rumenz2:r--
group::rwx
mask::rwx
other::---

如何删除 ACL

为了从任何文件/目录中删除 ACL,我们使用 x 和 b 选项

# setfacl -x ACL file/directory   # remove only specified ACL from file/directory.
# setfacl -b  file/directory     #removing all ACL from file/direcoty

两个用户(rumenz1 和 rumenz2),两者都有名为的共同次要组acl。我们将创建一个所有者是rumenz1的目录, 并将该目录的 read 和 execute 授权给用户 rumenz2

Step 1: 创建两个用户并从两者中删除密码

[root@linux ~]# for user in rumenz1 rumenz2
do
> useradd $user
> passwd -d $user
done
Removing password for user rumenz
passwd: Success
Removing password for user rumenz
passwd: Success

Step 2: 创建一个组和用户到次要组。

[root@linux ~]# groupadd acl
[root@linux ~]# usermod -G acl rumenz1
[root@linux ~]# usermod -G acl rumenz2

Step 3:创建目录/rumenz并将所有权更改为rumenz1`.

[root@linux ~]# mkdir /rumenz1
[root@linux ~]# chown rumenz1 /rumenz1/
[root@linux ~]# ls -ld /rumenz1/

drwxr-xr-x 2 rumenz1 root 4096 Apr 17 14:46 /rumenz1/

[root@linux ~]# getfacl /rumenz1

`getfacl: Removing leading '/' from absolute path names`
# file: rumenz1
# owner: rumenz1
# group: root
user::rwx
group::r-x
other::r-x

Step 4: 登录 rumenz1 并在其中创建一个目录。

[rumenz@linux ~]$ su - rumenz1

Last login: Thu Apr 17 14:49:16 IST 2014 on pts/4

[rumenz1@linux ~]$ cd /rumenz1/
[rumenz1@linux rumenz1]$ mkdir example
[rumenz1@linux rumenz1]$ ll

total 4
drwxrwxr-x 2 rumenz1 rumenz1 4096 Apr 17 14:50 example

[rumenz1@linux rumenz1]$ whoami 
rumenz1

Step 5: 现在使用设置ACLsetfacl,  rumenz1 将拥有所有 rwx 权限,rumenz2 将只有 read 的权限,example 文件夹和其他文件夹将没有权限。

$ setfacl -m u:rumenz1:rwx example/
$ setfacl -m u:rumenz2:r-- example/
$ setfacl -m  other:--- example/
$ getfacl example/

# file: example
# owner: rumenz1
# group: rumenz1
user::rwx
user:rumenz1:rwx
user:rumenz2:r--
group::r-x
mask::rwx
other::---

Step 6: rumenz2 在另一个终端上并将目录更改为 /rumenz1。现在尝试使用ls命令 查看内容,然后尝试更改目录并查看如下差异。

[rumenz@linux ~]$ su - rumenz2

Last login: Thu Apr 17 15:03:31 IST 2014 on pts/5

[rumenz2@linux ~]$ cd /rumenz1/
[rumenz2@linux rumenz1]$ ls -lR example/
example/:
total 0
[rumenz2@linux rumenz1]$ cd example/

-bash: cd: example/: Permission denied

[rumenz2@linux rumenz1]$ getfacl example/

# file: example
# owner: rumenz1
# group: rumenz1
user::rwx
user:rumenz1:rwx
user:rumenz2:r--
group::rwx
mask::rwx
other::---

Step 7: 现在给 execute许可rumenz2 在 example 文件夹,然后使用 cd 命令查看效果。现在 rumenz2 具有查看和更改目录的权限,但没有写入任何内容的权限。

[rumenz1@linux rumenz1]$ setfacl -m u:rumenz2:r-x example/
[rumenz1@linux rumenz1]$ getfacl example/

# file: example
# owner: rumenz1
# group: rumenz1
user::rwx
user:rumenz1:rwx
user:rumenz2:r-x
group::rwx
mask::rwx
other::---

[rumenz@linux ~]$ su - rumenz2

Last login: Thu Apr 17 15:09:49 IST 2014 on pts/5

[rumenz2@linux ~]$ cd /rumenz1/
[rumenz2@linux rumenz1]$ cd example/
[rumenz2@linux example]$ getfacl .
[rumenz2@linux example]$ mkdir test

mkdir: cannot create directory test: Permission denied

[rumenz2@linux example]$ touch test

touch: cannot touch test: Permission denied

Note: 执行 ACL 后,你将看到一个额外的 + 符号为 ls –l 输出如下。

[root@linux rumenz1]# ll

total 4
drwxrwx---+ 2 rumenz1 rumenz1 4096 Apr 17 17:01 example


文章来源: http://mp.weixin.qq.com/s?__biz=MzAxMjE3ODU3MQ==&mid=2650523322&idx=4&sn=fa5b45c3c483f4d341002b129591f1fe&chksm=83baa35eb4cd2a48ae7db3f162aab783407eb78a409bc269988e626c1f71e3a27fab3e9a7349#rd
如有侵权请联系:admin#unsafe.sh