# 符号链接

符号链接（亦称软链接）是一种特殊类型的文件，其内容是一个字符串，保存着另一文件或目录的路径名；当程序打开一条途经该链接的路径时，系统会把链接的内容代入正在解析的路径中。<sup>[1](https://man7.org/linux/man-pages/man7/symlink.7.html)</sup><sup> • </sup><sup>[2](https://man7.org/linux/man-pages/man2/symlink.2.html)</sup> 由于链接指向的是一个名字而非底层的文件对象，它可以指向目录、跨越文件系统边界，并且在其目标不存在时仍能作为悬空链接存在。<sup>[1](https://man7.org/linux/man-pages/man7/symlink.7.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 内容为路径名字符串的文件，在运行时被解释为代入所遵循的路径中<sup>[1](https://man7.org/linux/man-pages/man7/symlink.7.html)</sup><sup> • </sup><sup>[2](https://man7.org/linux/man-pages/man2/symlink.2.html)</sup> |
| 与硬链接的对比 | 硬链接引用同一 inode，不能链接目录，不能跨文件系统；符号链接三者皆可<sup>[1](https://man7.org/linux/man-pages/man7/symlink.7.html)</sup> |
| 悬空链接 | 不要求目标存在；指向缺失路径名的链接即为悬空链接<sup>[1](https://man7.org/linux/man-pages/man7/symlink.7.html)</sup> |
| 创建 | `symlink()` 系统调用（SVr4、4.3BSD、POSIX.1-2001）；`symlinkat()` 于 POSIX.1-2008、Linux 2.6.16、glibc 2.4 中加入<sup>[2](https://man7.org/linux/man-pages/man2/symlink.2.html)</sup> |
| 起源 | 作为新文件类型引入于 4.2BSD（1983年）<sup>[3](https://manualzilla.com/doc/5973825/bug-fixes-and-changes-in-4.2bsd-july-28--1983)</sup> |
| 循环控制 | 当链接串联过多时，解析以 ELOOP 失败；早期 BSD 在约 100 次 inode 获取后中止打开操作<sup>[2](https://man7.org/linux/man-pages/man2/symlink.2.html)</sup><sup> • </sup><sup>[4](https://www.cs.hmc.edu/~markk/filesystems/bsd.html)</sup> |
| Windows 对应物 | NTFS 支持硬链接、联接和符号链接，其中联接与符号链接通过重解析点实现<sup>[5](https://learn.microsoft.com/en-us/windows/win32/fileio/hard-links-and-junctions)</sup> |
| 安全问题 | 符号链接路径上的检查时到使用时竞态；可用 `O_NOFOLLOW` 缓解<sup>[6](https://cmu-sei.github.io/secure-coding-standards/sei-cert-c-coding-standard/rules/posix-pos/pos35-c/)</sup> |

## 链接与硬链接的区别

硬链接是第二个目录项，指向同一个 inode，即文件系统上文件元数据表的索引。对文件的更改与使用哪个名字无关，硬链接与原名字无法区分。这一设计带来两条限制：硬链接不得指向目录，因为目录环会迷惑许多程序；也不得跨文件系统，因为 inode 编号在不同文件系统之间并不唯一。<sup>[1](https://man7.org/linux/man-pages/man7/symlink.7.html)</sup>

符号链接通过存储名字而非 inode 编号，避开了这两条限制。由于所存储的名字可以包含通往另一文件系统挂载点的路径，符号链接可以指向其他文件系统上的文件。<sup>[4](https://www.cs.hmc.edu/~markk/filesystems/bsd.html)</sup> 代价是独立性：删除目标后，符号链接仍然存在，但不再指向任何东西；符号链接也不能阻止文件被删除。<sup>[4](https://www.cs.hmc.edu/~markk/filesystems/bsd.html)</sup> 目标缺失的链接称为悬空链接。<sup>[1](https://man7.org/linux/man-pages/man7/symlink.7.html)</sup>

## 创建与解析

`symlink()` [系统调用](https://www.edgepedia.org/zh/system-call)创建一个名为 `linkpath` 的链接，内容为字符串 `target`，不检查目标是否存在。<sup>[2](https://man7.org/linux/man-pages/man2/symlink.2.html)</sup> 链接可以包含 `..` 分量，当用于链接开头时，指的是包含该链接的目录的父目录。<sup>[2](https://man7.org/linux/man-pages/man2/symlink.2.html)</sup> `symlinkat()` 变体相对于调用者提供的目录文件描述符（而非当前工作目录）来解释相对 `linkpath`，这一形式是为在目录遍历中避免竞态而加入的；在较旧的内核上，glibc 封装通过 `/proc/self/fd` 构造路径，回退到 `symlink()`。<sup>[2](https://man7.org/linux/man-pages/man2/symlink.2.html)</sup>

在路径名解析过程中，当系统遇到符号链接时，会把链接内容加在剩余路径名之前；空的链接内容要么以 ENOENT 使解析失败，要么被当作指代包含该链接的目录。<sup>[7](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap04.html)</sup> 当某个目录分量本身又通过进一步的符号链接解析时，解析可以递归进行，Linux 的 `openat2()` 可以用 `RESOLVE_NO_SYMLINKS` 标志完全阻止符号链接解析。<sup>[8](https://www.kernel.org/pub/linux/docs/man-pages/book/man-pages-6.06.pdf)</sup>

POSIX 区分跟随最后分量符号链接的接口（如 `stat()`、`chmod()`、`utime()`）与作用于链接本身的接口（如 `lstat()`、`rename()`、`unlink()`、`rmdir()`）。末尾的斜杠强制跟随链接：若 `/a/b` 是指向目录的符号链接，则 `/a/b` 指代该链接，而 `/a/b/` 指代它所指向的目录。<sup>[9](https://pubs.opengroup.org/onlinepubs/9799919799/xrat/V4_xbd_chap01.html)</sup> `rename()` 对此有明确规定：若任一参数指代符号链接，它作用于链接本身，不解析最后分量。<sup>[10](https://pubs.opengroup.org/onlinepubs/9799919799/functions/rename.html)</sup> 跟随规则的一个刻意例外是 `open(path, O_CREAT|O_EXCL)`，当路径指代一个已存在的符号链接（即便它指向不存在的文件）时，该调用以 EEXIST 失败。<sup>[9](https://pubs.opengroup.org/onlinepubs/9799919799/xrat/V4_xbd_chap01.html)</sup>

## 权限与元数据

在 Linux 上，跟随符号链接时链接自身的权限被忽略；起作用的是目标文件的权限。跟随时的所有权同样被忽略，除非启用了 `protected_symlinks` 特性；但在删除或重命名位于设置了粘滞位的目录中的链接时，所有权会被检查。<sup>[2](https://man7.org/linux/man-pages/man2/symlink.2.html)</sup> POSIX 未规定符号链接在 `st_mode` 中的文件模式位，指出历史实现在应用 umask 与设置固定值（如 0777）之间各有不同，但要求 `readlink()` 和路径解析忽略这些位。<sup>[9](https://pubs.opengroup.org/onlinepubs/9799919799/xrat/V4_xbd_chap01.html)</sup>

## 循环

由于符号链接可以指回通往它的路径，符号链接引入了传统 UNIX 目录树（每个目录恰有一个父目录）无法产生的无限解析循环之可能。<sup>[4](https://www.cs.hmc.edu/~markk/filesystems/bsd.html)</sup> 伯克利实现加入了一个简单的计数器：在单次打开过程中统计 inode 获取次数，若计数增长过大（超过约 100 次），就认定存在循环并中止打开。这一测试是任意而不完美的，但在实践中有效。<sup>[4](https://www.cs.hmc.edu/~markk/filesystems/bsd.html)</sup> Linux 将该情况报告为 ELOOP 错误，即“解析”路径时“遇到过多符号链接”。<sup>[2](https://man7.org/linux/man-pages/man2/symlink.2.html)</sup> POSIX 工具 `ls`、`find` 和 `pax` 在对层级进行逻辑遍历时须检测无限循环。<sup>[9](https://pubs.opengroup.org/onlinepubs/9799919799/xrat/V4_xbd_chap01.html)</sup>

## 历史

符号链接作为一种新文件类型出现于 4.2BSD，由加州大学伯克利分校于 1983 年发布。<sup>[3](https://manualzilla.com/doc/5973825/bug-fixes-and-changes-in-4.2bsd-july-28--1983)</sup><sup> • </sup><sup>[11](https://personal.opengroup.org/~ajosey/LWE2004/UNIX_Poster/unix_posterA3_Screen.pdf)</sup> 这一机制源自更早的工作：[丹尼斯·里奇](https://www.edgepedia.org/zh/dennis-ritchie)（Dennis Ritchie）在第八版研究用 UNIX 上修改了路径名查找例程 `nami.c`，为符号链接遍历加入 follow 参数，这些修改在 4.2BSD 发布之前被带入 4.1BSD。<sup>[12](https://retrocomputing.stackexchange.com/questions/26602/origin-of-unix-symbolic-links)</sup> `symlink()` 接口随后出现于 SVr4 和 4.3BSD，并在 POSIX.1-2001 中标准化。<sup>[2](https://man7.org/linux/man-pages/man2/symlink.2.html)</sup>

## Windows 与 macOS

NTFS 支持三类文件链接：硬链接、联接和符号链接。联接（junction）在微软术语中亦称软链接，它链接目录，可以跨越同一计算机上的本地卷；联接通过重解析点实现，重解析点是符号链接的一种泛化形式，由 32 位标记和可变长度数据触发筛选器驱动或 I/O 系统的重定向。<sup>[5](https://learn.microsoft.com/en-us/windows/win32/fileio/hard-links-and-junctions)</sup><sup> • </sup><sup>[13](https://www.microsoft.com/en-us/research/wp-content/uploads/2000/01/WSS2000.pdf)</sup> `CreateSymbolicLink` 函数创建带绝对或相对目标的链接；相对链接使用点号约定、根相对或当前目录相对形式，限于单个卷内，而当前工作目录相对的链接实际会被创建为绝对链接。符号链接也可以通过 UNC 路径直接指向远程文件或目录。<sup>[14](https://learn.microsoft.com/en-us/windows/win32/fileio/creating-symbolic-links)</sup> Win32 函数对链接的处理各不相同：对符号链接路径调用 `DeleteFile` 删除的是链接而非目标，而 `CopyFile` 复制的是目标，除非指定 `COPY_FILE_COPY_SYMLINK`。<sup>[15](https://learn.microsoft.com/en-us/windows/win32/fileio/symbolic-link-effects-on-file-systems-functions)</sup>

在 macOS 上，Finder 别名与符号链接相似，但行为差异显著；别名最初通过文件系统路径解析，现在提供与符号链接类似的行为。<sup>[16](https://developer-rno.apple.com/library/archive/documentation/MacOSX/Conceptual/BPFileSystem/Articles/Aliases.html)</sup> 在经典的 Mac OS X 中，创建符号链接的唯一途径是在终端窗口或 shell 脚本中使用 BSD 命令 `ln -s`。<sup>[17](https://developer-rno.apple.com/library/archive/documentation/MacOSX/Conceptual/BPFileSystem/Articles/FilesAndFinder.html)</sup>

## 安全

符号链接是文件系统攻击中反复出现的要素。CAPEC 目录描述了一种攻击模式：攻击者创建一个符号链接（例如名为 `/tmp/foo`、指向 `/.rhosts` 的链接），并利用特权程序的文件操作；该模式仅适用于支持符号链接的平台，建立在检查时到使用时竞态条件之上。<sup>[18](https://capec.mitre.org/data/definitions/27.html)</sup> CERT 规则 POS35-C 针对此类攻击背后的编程错误：先用 `lstat()` 检查文件名再打开，会留下一个时间窗口，攻击者可在此期间换入符号链接。针对 POSIX.1-2008 及以后版本的合规修复方式是使用 `O_NOFOLLOW` 打开，若最后分量是符号链接则使 `open()` 失败，从而消除单独的检查与使用；对较旧系统，修复方式是先 `lstat()`、再 `open()`，然后对描述符 `fstat()` 并比较结果以检测替换。<sup>[6](https://cmu-sei.github.io/secure-coding-standards/sei-cert-c-coding-standard/rules/posix-pos/pos35-c/)</sup> 更一般地说，如果攻击者能写入路径经过的任何目录，符号链接操纵就能让攻击者控制路径所解析到的对象；而在多次系统调用中复用同一路径，则可使一次调用触及一个对象，之后的调用触及另一个对象。<sup>[19](https://research.cs.wisc.edu/mist/safefile/safeopen.pdf)</sup>

## References

1. [symlink(7) - Linux manual page](https://man7.org/linux/man-pages/man7/symlink.7.html)
2. [symlink(2) - Linux manual page](https://man7.org/linux/man-pages/man2/symlink.2.html)
3. [Bug fixes and changes in 4.2BSD, July 28, 1983](https://manualzilla.com/doc/5973825/bug-fixes-and-changes-in-4.2bsd-july-28--1983)
4. [Introduction to BSD UNIX Volumes and File Structure, Harvey Mudd College](https://www.cs.hmc.edu/~markk/filesystems/bsd.html)
5. [Hard Links and Junctions, Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/fileio/hard-links-and-junctions)
6. [POS35-C. Avoid race conditions while checking for the existence of a symbolic link, CERT Secure Coding](https://cmu-sei.github.io/secure-coding-standards/sei-cert-c-coding-standard/rules/posix-pos/pos35-c/)
7. [General Concepts, POSIX.1-2024](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap04.html)
8. [The Linux man-pages Book, man-pages 6.06](https://www.kernel.org/pub/linux/docs/man-pages/book/man-pages-6.06.pdf)
9. [Rationale for Base Definitions, POSIX.1-2024](https://pubs.opengroup.org/onlinepubs/9799919799/xrat/V4_xbd_chap01.html)
10. [rename, POSIX.1-2024](https://pubs.opengroup.org/onlinepubs/9799919799/functions/rename.html)
11. [The Single UNIX Specification History & Timeline, The Open Group](https://personal.opengroup.org/~ajosey/LWE2004/UNIX_Poster/unix_posterA3_Screen.pdf)
12. [Origin of UNIX symbolic links?, Retrocomputing Stack Exchange](https://retrocomputing.stackexchange.com/questions/26602/origin-of-unix-symbolic-links)
13. [Windows 2000 Single Instance Storage (reparse points), Microsoft Research](https://www.microsoft.com/en-us/research/wp-content/uploads/2000/01/WSS2000.pdf)
14. [Creating Symbolic Links, Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/fileio/creating-symbolic-links)
15. [Symbolic Link Effects on File Systems Functions, Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/fileio/symbolic-link-effects-on-file-systems-functions)
16. [Aliases and Symbolic Links, Apple Developer Documentation](https://developer-rno.apple.com/library/archive/documentation/MacOSX/Conceptual/BPFileSystem/Articles/Aliases.html)
17. [Files and the Finder, Apple Developer Documentation](https://developer-rno.apple.com/library/archive/documentation/MacOSX/Conceptual/BPFileSystem/Articles/FilesAndFinder.html)
18. [CAPEC-27: Leveraging Race Conditions via Symbolic Links, MITRE](https://capec.mitre.org/data/definitions/27.html)
19. [Safeopen: denial of service and symlink manipulation risks, University of Wisconsin-Madison](https://research.cs.wisc.edu/mist/safefile/safeopen.pdf)

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

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Operating systems › File systems

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/symbolic-link

English original: https://www.edgepedia.org/symbolic-link
