LinuxIII · Filesystems and FilesLinks
Hard links and symbolic links
What you'll learn
- Create and identify hard links and symbolic links
- Explain why hard links cannot cross filesystems but symbolic links can
- Identify dangling symbolic links and their operational impact
- Choose the right link type for production use
Prerequisites
Verified against Ubuntu 24.04 LTS · Debian 12 (Bookworm) · RHEL 9.x · Rocky Linux 9.x · AlmaLinux 9.x · Linux kernel 6.1 LTS / 6.6 LTS · systemd 255+ · OpenSSH 8.7p1 (RHEL 9) / 9.6p1 (Ubuntu 24.04) · nftables 1.0.x · chrony 4.x · Pacemaker 2.1.x · Corosync 3.1.x · 2026-08-09
Two “link” concepts live in Linux. Both make a file reachable from more than one path, but they do it in fundamentally different ways. Choosing the wrong one produces production surprises.
Hard links
A hard link is a directory entry that points at the same inode as another directory entry. Both paths resolve to the same file; they are the same file. Deleting one does not affect the other as long as the inode’s link count stays above zero.
$ echo hello > original.txt; ln original.txt hardlink.txt; ls -li original.txt hardlink.txt12345 -rw-r--r-- 2 root root 6 Aug 9 11:11 original.txt
12345 -rw-r--r-- 2 root root 6 Aug 9 11:11 hardlink.txtIllustrative output
Symbolic links
A symbolic link (symlink) is a special file whose contents are a path string. When the kernel resolves a symlink, it reads the string and follows it. If the target does not exist, the symlink is dangling.
$ echo hello > original.txt; ln -s original.txt symlink.txt; ls -li original.txt symlink.txt12345 -rw-r--r-- 1 root root 6 Aug 9 11:11 original.txt
12346 lrwxrwxrwx 1 root root 12 Aug 9 11:11 symlink.txt -> original.txtIllustrative output
Properties compared
| Property | Hard link | Symbolic link |
|---|---|---|
| Same inode as target | Yes | No |
| Cross-filesystem | No (the inode is filesystem-local) | Yes |
| Cross mountpoints | No | Yes |
| Can link to directories | Restricted (often disabled on modern systems for safety) | Yes |
| Survives target deletion | Yes (the data is still there until last hard link is removed) | No (becomes dangling) |
Link count visible in ls -l | Yes (incremented on the inode) | No (the symlink itself has link count 1) |
find -inum finds both | Yes | No |
| Atomic swap into place | No (rename can swap into place; hard link cannot) | Yes (rename on the symlink itself works) |
Dangling symlinks: the operational risk
A dangling symlink points at a path that does not exist. Reading from
or writing to the symlink returns ENOENT (no such file or
directory). Many production failures are caused by symlinks that
were valid at install time and became dangling later.
$ ln -s /opt/myapp/current/bin/run run-current; rm -rf /opt/myapp/current; ls -l run-current; ./run-currentlrwxrwxrwx 1 root root 23 Aug 9 11:11 run-current -> /opt/myapp/current/bin/run
bash: ./run-current: No such file or directoryIllustrative output
- Audit dangling symlinks regularly.
find /etc /usr/local /opt -xtype l 2>/dev/nulllists every dangling symlink on the searched paths - Prefer absolute target paths in production. A relative symlink depends on the directory it is in; an absolute path is unambiguous regardless of where you resolve from
- Test symlinks before relying on them. A small CI step that checks every symlink expected to exist is cheap insurance
- Prefer hard links for the same data at two paths use cases. They survive target deletion and have no dangling failure mode.
Atomic swap with symlinks
Symlinks are the canonical “atomic swap into place” tool in deployment scripts:
ln -s releases/v2 bin/myapp-current
mv -T bin/myapp-current bin/myapp # atomic rename of the symlink
Renaming a symlink is atomic at the filesystem level; readers either see the old target or the new one, never a half-state. This is the basis of every blue/green deploy and every capistrano-style release directory layout.
Knowledge check
Knowledge check · 3 questions
Q1. A hard link and the original it points at share what?
Q2. A symbolic link that points at a missing target continues to function as long as the link itself exists.
Q3. Which of the following are properties of hard links? Select all that apply.
Passing score: 75%. Answers are checked in this browser.