Section 13.10

Hard link versus symbolic link

Definition

A hard link is a directory entry that names a file object directly. On Unix-like file systems that object is an inode (index node), which holds the file’s metadata and locates its content; every ordinary file name is a hard link, and an inode can have several. A symbolic link (symlink) is a separate small file whose content is a pathname, looked up afresh each time the link is used. A hard link names an object; a symbolic link names a path, which can later lead elsewhere or nowhere.

The idea

A user sees three names, data.txt, backup.txt and alias.txt, and all three show the same text. They can still behave very differently. If backup.txt is a hard link, it is the same file under a second name: there is one copy of the bytes, and an edit through either name shows through both. If alias.txt is a symbolic link, it is a signpost that says “go to data.txt”. Delete data.txt and the hard link still works, while the signpost points at nothing. Create a new data.txt and the signpost leads to the new file.

Keep three layers apart: names in directories, the objects the names refer to, and the open references that running programs hold to objects. A hard link is a name in the first layer that refers to an object in the second. A symbolic link is itself an object in the second layer, whose content is text that points back into the first. The signpost picture stops being accurate in one respect: nobody ever moves this signpost to follow its destination. A symbolic link holds text only, and the text is interpreted afresh every time the link is used.

How it works

Two rules restrict hard links. A hard link must be on the same file system as its inode, because an inode number means something only within one file system; an attempt across file systems fails with the error EXDEV. Most systems forbid hard links to directories, which keeps the directory tree free of cycles; Linux refuses with EPERM. Symbolic links have neither restriction: they can point across file systems, to directories, and to names that do not exist.

Worked example

The inode numbers below are invented; the file system chooses real ones.

  1. Create data.txt holding the 6 bytes of the text v1 ok and a line end. The file system gives it inode 1201, with link count 1.

  2. Run ln data.txt backup.txt. Both names now refer to inode 1201, whose link count becomes 1+1=21 + 1 = 2.

  3. Run ln -s data.txt alias.txt. This creates inode 1202, a symbolic link whose content is the 8 characters data.txt, so lstat reports its size as 8 bytes. Reading alias.txt shows the content of inode 1201.

  4. A program opens data.txt and keeps the file descriptor open.

  5. Run rm data.txt. The entry data.txt disappears and the link count of inode 1201 falls to 2−1=12 - 1 = 1. Reading backup.txt still gives the 6 bytes. Reading alias.txt fails with “No such file or directory”, because the stored name data.txt no longer exists; the link does not look for another name of inode 1201.

  6. Create a new data.txt holding v2. It gets a new inode, say 1203. Reading alias.txt now gives v2, while backup.txt still gives v1 ok.

  7. Run rm backup.txt. The link count of inode 1201 falls to 1−1=01 - 1 = 0, but the program from step 4 still has the file open, so the content survives and the program can still read it. When the program closes its descriptor, the system frees inode 1201.

Stated precisely

Let n(i)n(i) be the link count of inode ii, the number of directory entries that name it, and let r(i)r(i) be the number of open references to ii that processes hold through file descriptors or memory mappings.

These rules hold on POSIX file systems, such as ext4 on Linux. A file system without hard links, such as FAT (File Allocation Table), rejects the link call.

Limits and common mistakes

A link is not a copy. Editing through a hard link changes the only copy of the bytes. A backup made by hard-linking files protects against the loss of a name, not against a change of content.

Neither kind of link freezes or authenticates bytes. Checking a file through one name and later reading it through another, or through the same name after it changed, can examine different content. The File descriptor versus pathname entry treats this time-of-check to time-of-use §9.26 (TOCTOU) race.

A symbolic link’s own metadata is not its target’s. lstat reports the link’s owner and size; stat reports the target’s. A check that a file has the right owner must say which of the two it examined.

Symbolic links are a classic attack in shared directories. If a privileged program writes to /tmp/report and an attacker has already created /tmp/report as a symbolic link to a system file, the program overwrites the system file. Linux limits this with the setting fs.protected_symlinks, which refuses to follow a link in a shared directory, one that every user may write to and whose sticky bit (a permission bit that lets only an entry’s owner remove or rename it) is set, unless the follower or the directory’s owner also owns the link. A program should still create such files with the open flags O_CREAT and O_EXCL together, which make the call fail if the name already exists, even as a link.

Questions answered

What does unlinking one name do to the underlying object?

Unlinking removes one name, a directory entry, and lowers the object’s link count, its number of names, by 1; the object itself is destroyed only when no name and no open reference to it remains. The object, called an inode on Unix-like systems, holds the file’s content and metadata.

Unlinking never follows a symbolic link (a small file that stores a pathname): unlinking a symbolic link removes the link and leaves its target alone. Conversely, unlinking the name that a symbolic link stores leaves the link dangling, pointing at a name that no longer exists; if a new file later appears under that name, the link leads to the new file.

Related entries: File descriptor versus pathname (§15.21), Inode reuse (§15.24), pathlib (§12.39), Namespace and symlink resolution (§13.9), Time-of-check to time-of-use (TOCTOU) (§9.26). Further reading: [50, 139].