Skip to main content
RunBook Academy

LinuxIII · Filesystems and FilesLinks

Hard links and symbolic links

Foundation⏱ ~10 minbashlnlsstatfind

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

Not yet marked complete on this device.

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.

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.

Read-only / Safehard link
$ echo hello > original.txt; ln original.txt hardlink.txt; ls -li original.txt hardlink.txt
12345 -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.txt

Illustrative output

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.

Read-only / Safesymbolic link
$ echo hello > original.txt; ln -s original.txt symlink.txt; ls -li original.txt symlink.txt
12345 -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.txt

Illustrative output

Properties compared

PropertyHard linkSymbolic link
Same inode as targetYesNo
Cross-filesystemNo (the inode is filesystem-local)Yes
Cross mountpointsNoYes
Can link to directoriesRestricted (often disabled on modern systems for safety)Yes
Survives target deletionYes (the data is still there until last hard link is removed)No (becomes dangling)
Link count visible in ls -lYes (incremented on the inode)No (the symlink itself has link count 1)
find -inum finds bothYesNo
Atomic swap into placeNo (rename can swap into place; hard link cannot)Yes (rename on the symlink itself works)

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.

Read-only / Safedangling symlink
$ ln -s /opt/myapp/current/bin/run run-current; rm -rf /opt/myapp/current; ls -l run-current; ./run-current
lrwxrwxrwx 1 root root 23 Aug  9 11:11 run-current -> /opt/myapp/current/bin/run
bash: ./run-current: No such file or directory

Illustrative output

  1. Audit dangling symlinks regularly. find /etc /usr/local /opt -xtype l 2>/dev/null lists every dangling symlink on the searched paths
  2. 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
  3. Test symlinks before relying on them. A small CI step that checks every symlink expected to exist is cheap insurance
  4. Prefer hard links for the same data at two paths use cases. They survive target deletion and have no dangling failure mode.

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

  1. Q1. A hard link and the original it points at share what?

  2. Q2. A symbolic link that points at a missing target continues to function as long as the link itself exists.

  3. Q3. Which of the following are properties of hard links? Select all that apply.

Passing score: 75%. Answers are checked in this browser.