文件权限与 ACL
适用于 CentOS Stream 9 & 10 / AlmaLinux 9.x & 10.x / Rocky Linux 9.x & 10.x
Linux 文件权限是系统安全的基石。每个文件和目录都有一套权限规则,控制谁可以读取、写入和执行它们。当基本权限无法满足需求时,ACL(访问控制列表)可以提供更精细的控制。
- 理解 rwx 权限模型
- 使用
chmod修改权限(符号模式和八进制模式) - 使用
chown和chgrp修改属主和属组 - SUID、SGID 和 Sticky Bit 特殊权限
- 使用 ACL 实现精细权限控制
- 理解和配置 umask
- 一台已安装 EL 9.x 的系统
- 拥有
sudo权限的用户账户 - 了解用户和组管理的基本概念
基本权限模型
Section titled “基本权限模型”查看文件权限
Section titled “查看文件权限”$ ls -l /etc/nginx/nginx.conf-rw-r--r--. 1 root root 2488 Mar 24 10:00 /etc/nginx/nginx.conf输出各部分含义:
-rw-r--r--. 1 root root 2488 Mar 24 10:00 nginx.conf│├──┤├──┤├──┤ │ │ │ │ │ ││ │ │ │ │ │ │ │ │ └─ 文件名│ │ │ │ │ │ │ │ └─ 修改时间│ │ │ │ │ │ │ └─ 文件大小│ │ │ │ │ │ └─ 属组│ │ │ │ │ └─ 属主│ │ │ │ └─ 硬链接数│ │ │ └─ 其他用户权限 (other)│ │ └─ 属组权限 (group)│ └─ 属主权限 (owner)└─ 文件类型(- 普通文件,d 目录,l 符号链接)rwx 权限含义
Section titled “rwx 权限含义”| 权限 | 字符 | 对文件的含义 | 对目录的含义 |
|---|---|---|---|
| 读 | r | 读取文件内容 | 列出目录内容 |
| 写 | w | 修改文件内容 | 在目录中创建/删除文件 |
| 执行 | x | 执行文件(脚本/程序) | 进入目录(cd) |
chmod:修改权限
Section titled “chmod:修改权限”符号模式使用 u(属主)、g(属组)、o(其他)、a(所有)配合 +(添加)、-(移除)、=(设置)来修改权限。
$ chmod u+x script.sh$ chmod o-w file.txt$ chmod g+rw shared.doc$ chmod u=rwx,g=r,o=r script.sh$ chmod a+x script.sh每个权限位对应一个数值:
| 权限 | 值 |
|---|---|
r | 4 |
w | 2 |
x | 1 |
- | 0 |
将属主、属组、其他用户的权限值分别相加:
| 数字 | 权限 | 说明 |
|---|---|---|
7 | rwx | 读 + 写 + 执行 |
6 | rw- | 读 + 写 |
5 | r-x | 读 + 执行 |
4 | r-- | 只读 |
3 | -wx | 写 + 执行 |
2 | -w- | 只写 |
1 | --x | 只执行 |
0 | --- | 无权限 |
$ chmod 755 script.sh$ chmod 644 config.conf$ chmod 700 private-dir/递归修改权限
Section titled “递归修改权限”$ chmod -R 755 /var/www/html/$ find /var/www/html/ -type d -exec chmod 755 {} +$ find /var/www/html/ -type f -exec chmod 644 {} +chown 和 chgrp:修改属主和属组
Section titled “chown 和 chgrp:修改属主和属组”$ sudo chown webuser /var/www/html/index.html$ sudo chown webuser:webgroup /var/www/html/index.html$ sudo chown -R webuser:webgroup /var/www/html/$ sudo chgrp developers project-file.txt$ sudo chgrp -R developers /opt/project/除了基本的 rwx 权限,Linux 还有三个特殊权限位。
SUID (Set User ID)
Section titled “SUID (Set User ID)”当可执行文件设置了 SUID 位后,任何用户执行该文件时,都会以文件属主的身份运行(而不是执行者自身)。
$ ls -l /usr/bin/passwd-rwsr-xr-x. 1 root root 32648 ... /usr/bin/passwd属主权限中的 s 就是 SUID 标志。普通用户执行 passwd 时,进程以 root 身份运行,因此能够修改 /etc/shadow。
$ sudo chmod u+s /path/to/program$ sudo chmod 4755 /path/to/program$ sudo find / -perm -4000 -type f 2>/dev/nullSGID (Set Group ID)
Section titled “SGID (Set Group ID)”对文件:执行时以文件属组身份运行。
对目录:在目录中创建的新文件/子目录会自动继承该目录的属组,而不是创建者的主组。这对于团队协作目录非常有用。
$ sudo chmod g+s /opt/shared-project/$ sudo chmod 2775 /opt/shared-project/$ ls -ld /opt/shared-project/drwxrwsr-x. 2 root developers 4096 ... /opt/shared-project/属组权限中的 s 就是 SGID 标志。
Sticky Bit
Section titled “Sticky Bit”对目录设置 Sticky Bit 后,目录中的文件只能被文件的属主或 root 删除,即使其他用户对目录有写权限。最典型的例子是 /tmp。
$ ls -ld /tmpdrwxrwxrwt. 15 root root 4096 ... /tmp其他用户权限中的 t 就是 Sticky Bit 标志。
$ sudo chmod +t /opt/shared-uploads/$ sudo chmod 1777 /opt/shared-uploads/特殊权限汇总
Section titled “特殊权限汇总”| 权限 | 八进制 | 符号 | 对文件 | 对目录 |
|---|---|---|---|---|
| SUID | 4 | u+s | 以属主身份执行 | (无特殊效果) |
| SGID | 2 | g+s | 以属组身份执行 | 新文件继承目录的属组 |
| Sticky | 1 | +t | (无特殊效果) | 只有属主能删除文件 |
实战:创建团队共享目录
Section titled “实战:创建团队共享目录”-
创建共享目录
创建目录 $ sudo mkdir /opt/team-share -
设置属主和属组
设置为 developers 组所有 $ sudo chown root:developers /opt/team-share -
设置权限和 SGID
设置 SGID 确保新文件继承组 $ sudo chmod 2775 /opt/team-share -
验证
确认权限设置正确 $ ls -ld /opt/team-sharedrwxrwsr-x. 2 root developers 4096 ... /opt/team-share现在,
developers组的任何成员在该目录下创建的文件都会自动属于developers组。 -
测试
以 developers 组成员身份创建文件 $ touch /opt/team-share/test.txt$ ls -l /opt/team-share/test.txt-rw-rw-r--. 1 zhangsan developers 0 ... test.txt
umask:默认权限掩码
Section titled “umask:默认权限掩码”umask 决定了新创建文件和目录的默认权限。新文件的权限 = 基础权限 - umask 值。
- 文件的基础权限:
666(不含执行位) - 目录的基础权限:
777
$ umask0022$ umask -Su=rwx,g=rx,o=rxumask 0022 的效果:
| 基础权限 | umask | 实际权限 | |
|---|---|---|---|
| 文件 | 666 | 022 | 644 (rw-r—r—) |
| 目录 | 777 | 022 | 755 (rwxr-xr-x) |
修改 umask
Section titled “修改 umask”$ umask 027umask 027 的效果:
| 基础权限 | umask | 实际权限 | |
|---|---|---|---|
| 文件 | 666 | 027 | 640 (rw-r-----) |
| 目录 | 777 | 027 | 750 (rwxr-x---) |
$ echo "umask 027" >> ~/.bashrcACL(访问控制列表)
Section titled “ACL(访问控制列表)”当基本的 owner/group/other 权限模型无法满足需求时,ACL 可以为特定的用户或组设置独立的权限。
查看 ACL
Section titled “查看 ACL”$ getfacl /opt/team-share/config.txt输出示例(无额外 ACL):
# owner: zhangsan# group: developersuser::rw-group::rw-other::r--设置 ACL
Section titled “设置 ACL”$ sudo setfacl -m u:lisi:rw /opt/team-share/config.txt$ sudo setfacl -m g:qa:r /opt/team-share/config.txt$ getfacl /opt/team-share/config.txt输出示例:
# owner: zhangsan# group: developersuser::rw-user:lisi:rw-group::rw-group:qa:r--mask::rw-other::r--ACL 操作详解
Section titled “ACL 操作详解”$ sudo setfacl -m u:用户名:权限 文件路径$ sudo setfacl -m g:组名:权限 文件路径$ sudo setfacl -x u:lisi /opt/team-share/config.txt$ sudo setfacl -x g:qa /opt/team-share/config.txt$ sudo setfacl -b /opt/team-share/config.txt$ sudo setfacl -R -m u:lisi:rwx /opt/team-share/默认 ACL
Section titled “默认 ACL”默认 ACL 可以让目录中新创建的文件自动继承 ACL 规则:
$ sudo setfacl -d -m u:lisi:rw /opt/team-share/$ sudo setfacl -d -m g:qa:r /opt/team-share/$ getfacl /opt/team-share/输出示例:
# file: opt/team-share/# owner: root# group: developers# flags: -s-user::rwxgroup::rwxother::r-xdefault:user::rwxdefault:user:lisi:rw-default:group::rwxdefault:group:qa:r--default:mask::rwxdefault:other::r-x现在在该目录中创建的新文件会自动包含对 lisi 和 qa 组的 ACL 规则。
ACL 中的 mask
Section titled “ACL 中的 mask”mask 定义了 ACL 中用户和组能获得的最大权限。即使你设置了某个用户的权限为 rwx,如果 mask 是 r--,该用户实际有效权限也只有 r--。
$ sudo setfacl -m m::rx /opt/team-share/config.txt实战:精细化项目权限控制
Section titled “实战:精细化项目权限控制”假设有以下需求:
- 项目目录
/opt/webapp属于webdev组 webdev组的成员有完整读写权限qa组只能读取不能修改- 用户
deployer有完整权限用于部署 - 新创建的文件自动继承这些规则
-
创建目录和组
创建目录和组 $ sudo mkdir -p /opt/webapp$ sudo groupadd webdev$ sudo groupadd qa -
设置基本权限
设置属主、属组和 SGID $ sudo chown root:webdev /opt/webapp$ sudo chmod 2770 /opt/webapp -
设置 ACL
qa 组只读权限 $ sudo setfacl -m g:qa:rx /opt/webappdeployer 用户完整权限 $ sudo setfacl -m u:deployer:rwx /opt/webapp -
设置默认 ACL(对新建文件生效)
设置默认 ACL $ sudo setfacl -d -m g:webdev:rwx /opt/webapp$ sudo setfacl -d -m g:qa:rx /opt/webapp$ sudo setfacl -d -m u:deployer:rwx /opt/webapp -
验证
查看完整的 ACL 设置 $ getfacl /opt/webapp
chmod 和 ACL 哪个优先
Section titled “chmod 和 ACL 哪个优先”ACL 是对基本权限的扩展。当两者同时存在时:
- 文件属主的权限仍由
user::控制 - 其他用户的权限受 ACL 规则和 mask 共同影响
chmod修改组权限时会影响 mask 值
文件属主已经是 root,为什么普通用户还能读取
Section titled “文件属主已经是 root,为什么普通用户还能读取”因为 other(其他用户)的权限允许读取。例如权限 644 意味着所有用户都可以读取。
如何检查为什么某个用户无法访问文件
Section titled “如何检查为什么某个用户无法访问文件”$ ls -la /path/to/file$ getfacl /path/to/file$ namei -l /path/to/filenamei -l 会显示路径中每一级目录的权限,帮助你找到哪一级阻止了访问。
如何备份和恢复 ACL
Section titled “如何备份和恢复 ACL”$ getfacl -R /opt/webapp > acl-backup.txt$ sudo setfacl --restore=acl-backup.txt- 用户和组管理 — 创建和管理用户与组
- sudo 配置 — 管理员权限配置
- SELinux 入门 — 强制访问控制
man chmod/man chown/man setfacl/man getfacl