# 粘着位

粘着位是 Unix 及类 Unix 文件系统中的一个权限位，模式常量为 `S_ISVTX`（八进制 01000），其含义取决于文件类型。在目录上，它充当受限删除标志，阻止非特权用户删除或重命名文件，除非该用户拥有该文件或该目录；在普通文件上，它历史上用于将程序的正文映像保留在交换设备上，以便程序加载更快，而现代虚拟内存系统已使这一用途变得多余。<sup>[1](https://man7.org/linux/man-pages/man1/chmod.1.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 模式常量 | `S_ISVTX`，八进制 01000，四位八进制模式的首位数字<sup>[2](https://www.kernel.org/pub/linux/docs/man-pages/book/man-pages-6.06.pdf)</sup><sup> • </sup><sup>[10](https://www.gnu.org/software/coreutils/manual/html_node/Numeric-Modes.html)</sup><sup> • </sup><sup>[1](https://man7.org/linux/man-pages/man1/chmod.1.html)</sup> |
| 对目录的作用 | 阻止非特权用户删除或重命名文件，除非该用户拥有该文件、拥有该目录或具有特权<sup>[1](https://man7.org/linux/man-pages/man1/chmod.1.html)</sup> |
| 典型用途 | 全局可写目录，如 /tmp，这类目录必须公开可写，但不应允许用户删除彼此的文件<sup>[1](https://man7.org/linux/man-pages/man1/chmod.1.html)</sup> |
| 设置方式 | `chmod +t 目录` 或在数字模式前加 1，如 `chmod 1777 /tmp`；在权限字符串末尾显示为 `t`<sup>[3](https://docs.freebsd.org/en/books/handbook/book/index.html)</sup> |
| 对文件的作用 | 历史上将可执行文件的正文段保留在交换区以供复用；在现代系统上仍可在文件上设置该位，但已无效果<sup>[4](https://man.openbsd.org/sticky.8)</sup> |
| 内核强制执行 | 当所在目录设置了粘着位，且调用者的有效 UID 既不匹配文件所有者也不匹配目录所有者时，`unlink(2)` 会以 EPERM 或 EACCES 失败，除非进程具有特权（Linux 上为 CAP_FOWNER）<sup>[5](https://man7.org/linux/man-pages/man2/unlink.2.html)</sup> |
| POSIX 范围 | 若在非目录文件上设置了 S_ISVTX，其行为未作规定<sup>[6](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap04.html)</sup> |

## 在目录上的作用

在没有粘着位的目录中，对目录的写权限就足以删除或重命名其中的任何文件，无论文件属于谁。设置了粘着位后，该目录中的文件只能由同时具备以下条件的用户删除或重命名：对该目录有写权限，并且是文件所有者、目录所有者或超级用户。<sup>[4](https://man.openbsd.org/sticky.8)</sup> Oracle Solaris 的文档记载了同样的规则：粘着目录中的文件只能由文件所有者、目录所有者或 root 等特权用户删除。<sup>[7](https://docs.oracle.com/en/operating-systems/solaris/oracle-solaris/11.4/secure-files/sticky-bit.html)</sup>

[Linux 内核](https://www.edgepedia.org/zh/linux-kernel)在路径解析代码中执行这一规则：`may_delete` 例程在允许删除之前，会连同只追加标志和不可变标志一起检查 `check_sticky(dir, inode)`；`unlink(2)` 手册页记载了当调用者的有效 UID 既不匹配文件也不匹配所在目录、且进程缺少 CAP_FOWNER 能力时所产生的 EPERM 或 EACCES 错误。<sup>[5](https://man7.org/linux/man-pages/man2/unlink.2.html)</sup><sup> • </sup><sup>[8](https://kernel.googlesource.com/pub/scm/linux/kernel/git/jic23/parrot/%2B/6ecad912a0073c768db1491c27ca55ad2d0ee68f/fs/namei.c)</sup>

经典应用是 /tmp，它必须公开可写，以便任何用户都能创建临时文件，但应禁止用户任意删除或重命名彼此的文件。<sup>[4](https://man.openbsd.org/sticky.8)</sup> 目录列表会在权限字符串的最后一位显示该位为 `t`，例如 /tmp 显示为 `drwxrwxrwt`。<sup>[3](https://docs.freebsd.org/en/books/handbook/book/index.html)</sup>

## 设置与清除该位

该位通过 `chmod` 管理。符号模式 `+t` 设置它，`-t` 清除它；在数字模式中，它是最多四位八进制数字的第一位，值为 1，与设置用户 ID（4）和设置组 ID（2）并列。<sup>[1](https://man7.org/linux/man-pages/man1/chmod.1.html)</sup> FreeBSD 手册给出了 /tmp 的两种等价写法：`chmod +t /tmp` 和 `chmod 1777 /tmp`。<sup>[3](https://docs.freebsd.org/en/books/handbook/book/index.html)</sup> 在 OpenBSD 上，任何用户都可以创建粘着目录。<sup>[4](https://man.openbsd.org/sticky.8)</sup>

文档记载了两项创建时的限制。在 OpenBSD 上，`open(2)` 和 `mkdir(2)` 都不会创建设置了粘着位的文件。<sup>[4](https://man.openbsd.org/sticky.8)</sup> 在 Linux 上，用数字模式清除目录的特殊位需要显式写出首位数字，如 `00755`、`-6000` 或 `=755`，因为三位数字的模式不会触动第一位八进制数字。<sup>[1](https://man7.org/linux/man-pages/man1/chmod.1.html)</sup>

## 在文件上的历史含义

在普通文件上，该位的原始用途赋予了它名称。在一些较老的系统上，在可执行文件上设置该位会将程序的正文映像保存在交换设备上，以便程序运行时加载更快。<sup>[1](https://man7.org/linux/man-pages/man1/chmod.1.html)</sup> OpenBSD 手册更详细地描述了这一机制：设置了粘着位的可执行共享文件在执行后不会立即从交换空间中丢弃，因为内核将正文段囤积起来以备将来复用，从而避免重新加载程序。<sup>[4](https://man.openbsd.org/sticky.8)</sup>

这一行为在现代系统上已不复存在，虚拟内存系统会自行跟踪最近使用的可执行文件，使文件上的粘着位变得多余。该位仍可在文件上设置，但没有任何效果；在 OpenBSD 上只有超级用户可以在文件上设置它，不过文件所有者可以清除它。<sup>[4](https://man.openbsd.org/sticky.8)</sup> POSIX 对非目录文件上该位的含义完全未作规定，因此可移植程序不能依赖任何文件级行为。<sup>[6](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap04.html)</sup>

## 历史

`sticky` 命令出现在 Version 7 AT&T UNIX/32V 中，模式常量 S_ISVTX（“sticky, save text”，即粘着、保存正文）在名称中保留了原始的保存正文含义。<sup>[4](https://man.openbsd.org/sticky.8)</sup><sup> • </sup><sup>[2](https://www.kernel.org/pub/linux/docs/man-pages/book/man-pages-6.06.pdf)</sup> 目录用途，即受限删除标志，是在当前系统中存续下来的解释，并且仍然是共享临时目录上的标准做法。<sup>[1](https://man7.org/linux/man-pages/man1/chmod.1.html)</sup>

## 安全背景

粘着位缩小了共享可写目录的一项特定风险：其他用户文件的删除和重命名。一般而言，它并不能保障共享目录中敏感临时文件的安全。CERT 安全编码建议 FIO21-C 建议根本不要在 /tmp 和 /var/tmp 等共享目录中创建临时文件，因为共享目录中众所周知的文件可能被攻击者劫持或操纵；它建议使用不可预测且唯一的文件名、原子独占打开，并最终改用其他进程间通信机制。<sup>[9](https://cmu-sei.github.io/secure-coding-standards/sei-cert-c-coding-standard/recommendations/input-output-fio/fio21-c/)</sup>

## References

1. [chmod(1) - Linux manual page](https://man7.org/linux/man-pages/man1/chmod.1.html)
2. [The Linux man-pages Book, man-pages 6.06](https://www.kernel.org/pub/linux/docs/man-pages/book/man-pages-6.06.pdf)
3. [FreeBSD Handbook, File Permissions](https://docs.freebsd.org/en/books/handbook/book/index.html)
4. [sticky(8) - OpenBSD manual pages](https://man.openbsd.org/sticky.8)
5. [unlink(2) - Linux manual page](https://man7.org/linux/man-pages/man2/unlink.2.html)
6. [POSIX.1-2024, General Concepts](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap04.html)
7. [Sticky Bit, Securing Files and Verifying File Integrity in Oracle Solaris 11.4](https://docs.oracle.com/en/operating-systems/solaris/oracle-solaris/11.4/secure-files/sticky-bit.html)
8. [fs/namei.c, Linux kernel source](https://kernel.googlesource.com/pub/scm/linux/kernel/git/jic23/parrot/%2B/6ecad912a0073c768db1491c27ca55ad2d0ee68f/fs/namei.c)
9. [FIO21-C. Do not create temporary files in shared directories, CERT Secure Coding](https://cmu-sei.github.io/secure-coding-standards/sei-cert-c-coding-standard/recommendations/input-output-fio/fio21-c/)
10. [Numeric Modes (GNU Coreutils 9.12)](https://www.gnu.org/software/coreutils/manual/html_node/Numeric-Modes.html)

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

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Operating systems › Unix-like and Linux › Unix, BSD, and kernel internals

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/sticky-bit

English original: https://www.edgepedia.org/sticky-bit
