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
Creating a hard link (the command
ln old new, the system calllink) adds a directory entry for an existing inode and raises the inode’s link count, the number of directory entries that name it, by 1. Nothing is copied. All hard links share one set of metadata, because owner, permission bits, size and timestamps belong to the inode; a change made through one name shows through all.Creating a symbolic link (the command
ln -s target name, the system callsymlink) creates a new inode of type symbolic link, also called a soft link, whose content is the texttarget. The target need not exist. The link has its own metadata; POSIX (Portable Operating System Interface, the standard for Unix-like systems) defines its size as the length of the stored pathname.Following a symbolic link happens during pathname lookup: the system replaces the link by its stored pathname and continues. A relative stored pathname is looked up from the directory that holds the link. Most calls follow a link in the last component, among them
open,statandchmod; some act on the link itself, among themlstat,readlink,renameandunlink.Unlinking a name (the command
rm, the system callunlink) removes that one directory entry and lowers the link count of the inode it named by 1. It never follows a symbolic link: unlinking a symbolic link removes the link, not its target.Freeing: the system frees an inode and its content only when its link count is 0 and no process still has it open, either through a file descriptor §15.21 (the handle a program receives when it opens a file) or through a memory mapping (a range of the program’s memory backed by the file). A program that opened the file before its last name was removed keeps reading and writing it until it closes it.
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.
Create
data.txtholding the 6 bytes of the textv1 okand a line end. The file system gives it inode 1201, with link count 1.Run
ln data.txt backup.txt. Both names now refer to inode 1201, whose link count becomes .Run
ln -s data.txt alias.txt. This creates inode 1202, a symbolic link whose content is the 8 charactersdata.txt, solstatreports its size as 8 bytes. Readingalias.txtshows the content of inode 1201.A program opens
data.txtand keeps the file descriptor open.Run
rm data.txt. The entrydata.txtdisappears and the link count of inode 1201 falls to . Readingbackup.txtstill gives the 6 bytes. Readingalias.txtfails with “No such file or directory”, because the stored namedata.txtno longer exists; the link does not look for another name of inode 1201.Create a new
data.txtholdingv2. It gets a new inode, say 1203. Readingalias.txtnow givesv2, whilebackup.txtstill givesv1 ok.Run
rm backup.txt. The link count of inode 1201 falls to , 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 be the link count of inode , the number of directory entries that name it, and let be the number of open references to that processes hold through file descriptors or memory mappings.
linkadds an entry for and sets .unlinkremoves one entry; if that entry named , it sets . It does not follow a symbolic link in the last component.The system frees and its content only once and both hold.
Following a symbolic link whose content is the pathname yields whatever resolves to at the moment of following, starting from the directory that holds if is relative. At another moment the result can be a different inode, or nothing.
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.
If other hard links, other directory entries for the same inode, remain, the object is untouched and those names still reach the same content.
If the removed name was the last one but a process still has the file open, the content survives until that process gives up its last handle to it: its last file descriptor (the handle a program receives when it opens a file) or memory mapping (a range of memory backed by the file).
Only when the link count is 0 and no process has the object open does the system free the object and its storage.
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].