# chmod

**chmod**（“change mode”，即更改模式）是一条 Unix 和 POSIX 命令，用于更改文件和目录的文件模式位：所有者、组和其他用户的读、写、执行权限位，以及设置用户 ID、设置组 ID 和粘滞位。它出现在 1971 年 11 月 3 日出版的《Unix 程序员手册》第一版中，并在 POSIX 中标准化为 `chmod(1p)`，其底层系统调用记录为 `chmod(2)`。<sup>[1](https://www.nokia.com/bell-labs/about/dennis-m-ritchie/1stEdman.html)</sup><sup> • </sup><sup>[2](https://www.man7.org/linux/man-pages/man1/chmod.1p.html)</sup><sup> • </sup><sup>[3](https://man7.org/linux/man-pages/man2/chmod.2.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 用途 | 根据以符号形式或八进制数给出的模式，更改每个指定文件的文件模式位<sup>[4](https://man7.org/linux/man-pages/man1/chmod.1.html)</sup> |
| 符号模式 | 格式为 `[ugoa...][[-+=][perms...]...]`，权限字母取自集合 `rwxXst`<sup>[4](https://man7.org/linux/man-pages/man1/chmod.1.html)</sup> |
| 数字模式 | 一至四位八进制数字；每位数字为读（4）、写（2）和执行（1）之和；第一位数字选择 setuid（4）、setgid（2）和粘滞（1）<sup>[4](https://man7.org/linux/man-pages/man1/chmod.1.html)</sup> |
| 谁可以更改模式 | 只有有效用户 ID 与文件所有者匹配的进程，或具有相应特权的进程<sup>[2](https://www.man7.org/linux/man-pages/man1/chmod.1p.html)</sup> |
| 符号链接 | chmod 不更改符号链接的权限；命令行上指定的符号链接会影响其指向的文件，而递归遍历中遇到的符号链接则被忽略<sup>[4](https://man7.org/linux/man-pages/man1/chmod.1.html)</sup> |
| 常见模式 | 644（所有者读/写，其他用户读）、755（所有者全部权限，其他用户读/执行）、700（仅所有者）、777（所有人完全访问，存在安全风险）<sup>[5](https://linuxize.com/post/what-does-chmod-777-mean/)</sup> |
| 标准化 | POSIX.1（系统调用最早在 IEEE Std 1003.1-1988 中标准化；该实用程序在“命令解释器与实用程序”卷中规定）<sup>[3](https://man7.org/linux/man-pages/man2/chmod.2.html)</sup><sup> • </sup><sup>[2](https://www.man7.org/linux/man-pages/man1/chmod.1p.html)</sup> |

## 符号模式

符号模式指明受影响的用户、一个操作符以及要更改的权限。字母 `u`、`g`、`o` 和 `a` 分别选择文件的所有者、文件组中的其他用户、既非所有者又非组用户的用户，以及所有用户。如果未给出任何字母，则效果等同于给出了 `a`，但 umask 中设置的位不受影响。<sup>[4](https://man7.org/linux/man-pages/man1/chmod.1.html)</sup>

操作符 `+` 将选定的位添加到现有模式中，`-` 将其移除，`=` 将其设置并移除未提及的位，但目录的未提及的设置用户 ID 和设置组 ID 位保持不变。权限字母为 `r`（读）、`w`（写）、`x`（执行，对目录则为搜索）、`X`（仅当文件是目录或已对某用户具有执行位时才设置执行权限）、`s`（执行时设置用户 ID 或设置组 ID）和 `t`（受限删除标志或粘滞位）。除权限字母外，还可以给出 `u`、`g` 或 `o` 之一，以复制所有者、组或其他类的权限。可以给出多个符号模式，以逗号分隔。<sup>[4](https://man7.org/linux/man-pages/man1/chmod.1.html)</sup>

符号模式适合在不干扰其余位的情况下调整某一位：`chmod u+x script.sh` 为所有者添加执行权限，组和其他用户保持不变，而八进制模式则会重写整个模式。<sup>[6](https://linuxize.com/post/chmod-command-in-linux/)</sup>

## 数字模式

数字模式是一至四位八进制数字（0–7），每位数字由位值 4、2 和 1 相加得出；省略的数字视为前导零。第一位数字选择设置用户 ID（4）、设置组 ID（2）和受限删除或粘滞（1）属性。第二位数字给出所有者的权限，第三位给出组的权限，第四位给出其他用户的权限，每位均以读（4）、写（2）和执行（1）表示。<sup>[4](https://man7.org/linux/man-pages/man1/chmod.1.html)</sup> 因此 `chmod 755 file` 为所有者赋予读、写和执行权限（7 = 4+2+1），为组和其他用户赋予读和执行权限（5 = 4+1）。<sup>[7](https://ubuntu.com/server/docs/tutorial/welcome-to-the-terminal/)</sup>

GNU 实现还接受 `--reference=RFILE` 选项，它将每个文件的模式设置为参考文件的模式，而非显式指定的模式。<sup>[4](https://man7.org/linux/man-pages/man1/chmod.1.html)</sup>

## 特殊位

**Setuid 和 setgid。** 当在可执行文件上设置 setuid 或 setgid 标志时，文件将以文件所有者或组的特权运行，而非调用用户的特权。<sup>[6](https://linuxize.com/post/chmod-command-in-linux/)</sup> 在 Linux 上，如果调用进程没有特权（缺少 CAP_FSETID 能力）且文件的组与进程的有效组 ID 或其补充组 ID 之一不匹配，`chmod()` 系统调用会清除常规文件的设置组 ID 位；该清除不会产生错误。<sup>[3](https://man7.org/linux/man-pages/man2/chmod.2.html)</sup> 对于目录，GNU chmod 保留设置用户 ID 和设置组 ID 位，除非显式更改；用数字模式清除它们需要前导零（如 `00755`）、前导减号（`-6000`）或前导等号（`=755`）。<sup>[4](https://man7.org/linux/man-pages/man1/chmod.1.html)</sup>

**粘滞位。** 当在目录上设置时，粘滞位充当受限删除标志：没有提升特权的用户不得删除或重命名其中的文件，除非他们是文件或目录的所有者。它通常设置在所有用户均可写的目录上，如 `/tmp`。在一些较旧系统的常规文件上，该位将程序的代码映像保留在交换设备上以便更快加载，这就是该名称的由来。<sup>[4](https://man7.org/linux/man-pages/man1/chmod.1.html)</sup>

## 符号链接与递归

chmod 不触碰符号链接的权限：在大多数系统上，`chmod` 系统调用无法更改它们，其权限通常被忽略。然而，对于命令行上列出的每个符号链接，chmod 会更改其指向文件的权限。在递归目录遍历（`-R` 选项）期间，chmod 忽略遇到的[符号链接](https://www.edgepedia.org/zh/symbolic-link)。GNU 选项 `-H`、`-L` 和 `-P` 修改此遍历行为：`-H` 跟随命令行上指向目录的符号链接，`-L` 跟随遇到的每个指向目录的符号链接，`-P` 不跟随任何符号链接。<sup>[4](https://man7.org/linux/man-pages/man1/chmod.1.html)</sup> 将解引用选项与 `--recursive` 结合使用可能产生安全风险：能够将符号链接引入被遍历树的攻击者可以将操作重定向到任意目标，从而可能提升特权。<sup>[8](https://www.gnu.org/software/coreutils/manual/coreutils.pdf)</sup>

## 与 umask 和 ACL 的关系

chmod 设置现有文件的模式；新创建文件的权限来自创建模式经进程 umask 掩码后的结果。在典型的 umask 022 下，以模式 0666 创建的文件最终为 0644（`rw-r--r--`）。<sup>[9](https://man7.org/linux/man-pages/man2/umask.2.html)</sup>

在具有 POSIX 访问控制列表（ACL）的系统上，所有者、组和其他权限位对应于特定的 ACL 条目。如果 ACL 具有掩码条目，组权限位反映的是掩码而非所属组条目，并且更改权限位会更改相应的 ACL 条目，反之亦然。<sup>[10](https://man7.org/linux/man-pages/man5/acl.5.html)</sup> POSIX 将 chmod 是否以及如何影响任何替代或附加访问控制机制留给实现定义。<sup>[2](https://www.man7.org/linux/man-pages/man1/chmod.1p.html)</sup>

## 历史

该命令可追溯至最早的 Unix 文档：1971 年 11 月 3 日出版的《Unix 程序员手册》第一版包含 chmod 条目，第二版手册给出了概要 `chmod octal file...`，表明最初的形式仅接受八进制模式。<sup>[1](https://www.nokia.com/bell-labs/about/dennis-m-ritchie/1stEdman.html)</sup><sup> • </sup><sup>[11](https://www.tuhs.org/Archive/Distributions/Research/1972_stuff/unix_2nd_edition_manual.pdf)</sup> `chmod()` 系统调用在 POSIX.1-1988 中标准化，`fchmod()` 在 4.4BSD 中添加，`fchmodat()` 在 POSIX.1-2008 中添加。<sup>[3](https://man7.org/linux/man-pages/man2/chmod.2.html)</sup> 由 David MacKenzie 和 Jim Meyering 编写、在 coreutils 中分发的 GNU 版本添加了符号模式语法、`--reference` 选项以及上述遍历选项。<sup>[4](https://man7.org/linux/man-pages/man1/chmod.1.html)</sup>

## 安全考虑

设置模式 777 使文件或目录对系统上所有用户可读、可写、可执行，这在多用户和共享环境中被视为严重的安全风险。<sup>[5](https://linuxize.com/post/what-does-chmod-777-mean/)</sup> setuid 和 setgid 位受到安全工具的密切关注：Splunk 附带一条检测规则，用于检测设置 SUID 或 SGID 的 chmod 执行（例如 `u+s`、`g+s` 或 `4777`），因为此类更改可能表明特权提升或持久化企图。<sup>[12](https://research.splunk.com/endpoint/bf0304b6-6250-11ec-9d7c-acde48001122/)</sup>

当路径在检查和使用之间可被替换时，基于路径的 `chmod()` 调用也是漏洞的反复来源。2026 年一份关于 util-linux 的 SUID `mount` 二进制程序的安全公告描述了一个检查与使用之间的竞态（TOCTOU），其中非特权用户可以使用 `renameat2(RENAME_EXCHANGE)` 将挂载后的 `chmod()` 重定向到任意路径，实现任意权限修改和本地特权提升至 root；推荐的修复方法是使用基于描述符的 `fchmod()` 而非基于路径的 `chmod()`。<sup>[13](https://github.com/util-linux/util-linux/security/advisories/GHSA-g8wm-75wr-g2vh)</sup> cupsd 打印服务器中的类似缺陷使攻击者可以通过植入的符号链接，让 `chmod` 将任意文件的权限设置为所有用户均可写。<sup>[14](https://github.com/OpenPrinting/cups/security/advisories/GHSA-vvwp-mv6j-hw6f)</sup>

权限模型还与版本控制相互作用。Git 仅记录每个文件模式的可执行位，将模式存储为 644 或 755，`core.fileMode` 配置控制是否遵循工作树的可执行位，因为某些文件系统会丢失或伪造该位。<sup>[15](https://git-scm.com/docs/user-manual)</sup><sup> • </sup><sup>[16](https://git-scm.com/docs/git-config/2.24.0)</sup>

## References

1. [Unix Programmer's Manual, first edition, November 3, 1971 (Nokia Bell Labs, Dennis Ritchie)](https://www.nokia.com/bell-labs/about/dennis-m-ritchie/1stEdman.html)
2. [chmod(1p) — POSIX Programmer's Manual, Linux man-pages](https://www.man7.org/linux/man-pages/man1/chmod.1p.html)
3. [chmod(2) — Linux manual page](https://man7.org/linux/man-pages/man2/chmod.2.html)
4. [chmod(1) — Linux manual page (GNU coreutils)](https://man7.org/linux/man-pages/man1/chmod.1.html)
5. [chmod 777: What It Means and Why You Should Never Use It, Linuxize](https://linuxize.com/post/what-does-chmod-777-mean/)
6. [chmod Command in Linux: Change File and Directory Permissions, Linuxize](https://linuxize.com/post/chmod-command-in-linux/)
7. [Welcome to the terminal, Ubuntu Server documentation](https://ubuntu.com/server/docs/tutorial/welcome-to-the-terminal/)
8. [GNU Coreutils manual, Chapter 13: Changing file attributes](https://www.gnu.org/software/coreutils/manual/coreutils.pdf)
9. [umask(2) — Linux manual page](https://man7.org/linux/man-pages/man2/umask.2.html)
10. [acl(5) — Linux manual page](https://man7.org/linux/man-pages/man5/acl.5.html)
11. [UNIX Programmer's Manual, Second Edition (TUHS archive)](https://www.tuhs.org/Archive/Distributions/Research/1972_stuff/unix_2nd_edition_manual.pdf)
12. [Detection: Linux Setuid Using Chmod Utility, Splunk Security Content](https://research.splunk.com/endpoint/bf0304b6-6250-11ec-9d7c-acde48001122/)
13. [Local Privilege Escalation via TOCTOU in mount(8) hook_owner.c chmod/chown, util-linux advisory GHSA-g8wm-75wr-g2vh](https://github.com/util-linux/util-linux/security/advisories/GHSA-g8wm-75wr-g2vh)
14. [Cupsd Listen arbitrary chmod 0140777, OpenPrinting/cups advisory GHSA-vvwp-mv6j-hw6f](https://github.com/OpenPrinting/cups/security/advisories/GHSA-vvwp-mv6j-hw6f)
15. [Git user-manual Documentation](https://git-scm.com/docs/user-manual)
16. [Git git-config Documentation, core.fileMode](https://git-scm.com/docs/git-config/2.24.0)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Build, deployment, and development tooling

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/chmod

English original: https://www.edgepedia.org/chmod
