MySQL, Oracle, Linux, 软件架构及大数据技术知识分享平台

网站首页 > linux / 正文

Linux的文件权限很简陋?那是因为你不知道高级操作:chacl命令

2024-11-26 15:37 huorong linux 3 ℃ 0 评论

chacl 命令简介

在 Linux 中,文件和目录对文件的所有者、与文件关联的组以及系统的其他用户具有权限限制。但是这些权限管理有局限性,因为不能为不同的用户配置不同的权限。
linux普通权限:用户对文件只有三种身份,就是所有者、组和其他人;每种用户身份拥有读(read)、写(write)和执行(execute)三种权限。
例如,需要为用户 A 提供读/写访问权限,为用户 B 和用户 C 提供只读权限。普通权限无法设置。
因此,linux 系统实现了访问控制列表(ACL)。ACL 为 linux 文件系统提供了更灵活的权限机制。
用户可以使用 setfacl 命令设置文件和目录的访问控制列表 (ACL),并使用 getfacl 命令获取文件访问控制列表。

使用 chacl 命令的语法

$ chacl [option] acl pathname

chacl 命令中的一些重要选项是:

  • -b:表示有两个ACL要更改,文件访问ACL和目录默认ACL
  • -d:仅设置目录的默认 ACL
  • -R:仅删除文件访问 ACL
  • -D:仅删除目录默认 ACL
  • -B:删除所有 ACL
  • -l:列出指定文件或目录的访问ACL和默认ACL
  • -r:递归设置访问 ACL

了解 ACL 条目

每个 ACL 条目都包含逗号分隔的多个权限定义,每个权限定义的模式为 tag:name:perm。

tag可以是:

  • user或u:表示该表项为用户ACL表项。
  • group或g:表示该表项是组ACL表项。
  • other或o:表示该表项为其他ACL表项。
  • mask或m:表示该表项为掩码ACL表项。它指示用户(所有者除外)和组允许的最大权限。例如,掩码条目m:r--表示用户和组只能具有读取权限,即使他们被赋予了写入/执行权限。

name是一个字符串,它是 ACL 条目的用户名或组名。用户或组 ACL 条目中的空名称表示文件的所有者或文件的组。

perm是字符串rwx,其中每个条目都可以替换为-指示该类型的无访问权限。例如,您必须使用r-x用于读取和执行、--x用于仅执行、rw-用于读取和写入等。
以下是最小 ACL 条目的示例,其中文件所有者将拥有rwx(读取、写入、执行),文件组将拥有r-x(读取和执行),其他人对文件具有只读访问权限。

u::rwx,g::rx,o::r--

非最小值的 ACL 条目指定文件所有者或所有者组以外的用户或组。此类条目必须包含掩码条目。

u::rwx,g::rx,o::r--,u:bob:r--,m::rx

使用 chacl 命令的不同示例

1.更改文件的ACL

getfacl可以使用该命令查看文件的当前 ACL 。
前三行表示文件名、所有者和所属组。文件用户有rw-权限,组有权限r--。其他人有r--权限。

$ getfacl friutes.txt
# file: friutes.txt
# owner: yunzhong
# group: yunzhong
user::rw-
group::r--
other::r--

更改friutes.txt权限,以便文件用户可以rw-访问。该组将有权r--访问,其他人将有权---访问。

$ chacl u::rw-,g::r--,o::--- friutes.txt
$ getfacl friutes.txt
# file: friutes.txt
# owner: yunzhong
# group: yunzhong
user::rw-
group::r--
other::---

在以下示例中,文件用户将具有rwx访问权限,但用户deepak具有r--访问权限。文件组将具有r-x访问权限,但该组computer将仅具有r--访问权限。

$ chacl u::rwx,g::rx,o::r--,u:deepak:r--,g:computer:r--,m::rx friutes.txt
yunzhong@DESKTOP-9VB7LN7:/tmp$ getfacl friutes.txt
# file: friutes.txt
# owner: yunzhong
# group: yunzhong
user::rwx
user:deepak:r--
group::r-x
group:computer:r--
mask::r-x
other::r--

注意:用户deepak,组computer需要存在系统中。

2.删除文件的ACL

-R选项仅可用于删除文件的 ACL。

$ chack -R file

删除文件friutes.txt的acl

$ chacl -R friutes.txt
$ getfacl friutes.txt
# file: friutes.txt
# owner: yunzhong
# group: yunzhong
user::rwx
group::r-x
other::r--

3.更改目录的ACL

可以通过指定目录而不是文件来更改目录的 ACL。

$ chacl u::rw-,g::r--,o::--- directory

例子:

yunzhong@DESKTOP-9VB7LN7:/tmp$ getfacl test-dir/
# file: test-dir/
# owner: yunzhong
# group: yunzhong
user::rwx
group::r-x
other::r-x

yunzhong@DESKTOP-9VB7LN7:/tmp$ chacl u::rw-,g::r--,o::--- test-dir/
yunzhong@DESKTOP-9VB7LN7:/tmp$ getfacl test-dir/
# file: test-dir/
# owner: yunzhong
# group: yunzhong
user::rw-
group::r--
other::---

4.设置目录的默认ACL

使用-d参数,为目录设置默认的acl权限。

$ chacl -d u::rwx,g::rw-,o::r-- Documents

例子

yunzhong@DESKTOP-9VB7LN7:/tmp$ chacl -d u::rw-,g::r--,o::--- test-dir/
yunzhong@DESKTOP-9VB7LN7:/tmp$ getfacl test-dir/
# file: test-dir/
# owner: yunzhong
# group: yunzhong
user::rw-
group::r--
other::---
default:user::rw-
default:group::r--
default:other::---

5.删除目录的默认ACL

通过参数-D删除目录的默认acl设置

$ chacl -D Documents

例子

yunzhong@DESKTOP-9VB7LN7:/tmp$ chacl -D test-dir/
yunzhong@DESKTOP-9VB7LN7:/tmp$ getfacl test-dir/
# file: test-dir/
# owner: yunzhong
# group: yunzhong
user::rw-
group::r--
other::---

6.列出文件或目录的ACL

可以使用-l选项列出文件或目录的访问 ACL 。它还显示指定文件或目录的默认 ACL。

$ chacl -l 文件

示例

yunzhong@DESKTOP-9VB7LN7:/tmp$ chacl -l friutes.txt
friutes.txt [u::rwx,g::r-x,o::r--]
yunzhong@DESKTOP-9VB7LN7:/tmp$ chacl -l test-dir/
test-dir/ [u::rw-,g::r--,o::---]

7.删除文件或目录的所有ACL

使用-B选项删除文件或目录的所有 ACL。

$ chacl -B 文件

8.递归设置访问ACL

该-r选项以递归方式更改目录的访问 ACL。这意味着目录的内容也将具有相同的 ACL。

$ chacl -r u::rwx,g::rw-,o::r-- 目录

示例

yunzhong@DESKTOP-9VB7LN7:/tmp$ chacl u::rwx,g::r-x,o::r-- test-dir/
yunzhong@DESKTOP-9VB7LN7:/tmp$ touch test-dir/testfile2
yunzhong@DESKTOP-9VB7LN7:/tmp$ chacl -l test-dir/testfile
test-dir/testfile [u::rw-,g::r--,o::r--]
yunzhong@DESKTOP-9VB7LN7:/tmp$ chacl -l test-dir/testfile2
test-dir/testfile2 [u::rw-,g::r--,o::r--]
yunzhong@DESKTOP-9VB7LN7:/tmp$ chacl -r u::r-x,g::r--,o::r-- test-dir/
chacl: cannot set access acl on "test-dir//testfile": Operation not permitted
yunzhong@DESKTOP-9VB7LN7:/tmp$ sudo chacl -r u::r-x,g::r--,o::r-- test-dir/
yunzhong@DESKTOP-9VB7LN7:/tmp$ chacl -l test-dir/testfile
test-dir/testfile [u::r-x,g::r--,o::r--]
yunzhong@DESKTOP-9VB7LN7:/tmp$ chacl -l test-dir/testfile2
test-dir/testfile2 [u::r-x,g::r--,o::r--]
yunzhong@DESKTOP-9VB7LN7:/tmp$

9.更改文件访问ACL和目录默认ACL

-b选项后面有两个acl参数。第一个是文件访问 ACL,第二个是目录默认 ACL。

$ chacl -b acl dacl 目录

Tags:linux修改只读文件

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言