Hard Links and Symbolic Links: The ln Command and inodes

Hard Links and Symbolic Links: The ln Command and inodes

What You Will Achieve

  • Explain hard vs symbolic links from the inode perspective
  • Use ln / ln -s correctly depending on the situation
  • Diagnose and avoid dangling links
  • Understand link constraints across filesystem boundaries and directories
  • Answer the exam-frequent "hard links cannot cross filesystems" with reasoning

This is the core of LPIC-1 objective 104.6 "Create and change hard and symbolic links". Understanding links requires the concept of the inode (a number pointing to the file's actual data).

Deciding Between Hard and Symbolic Links

Aspect Hard link Symbolic link
Create command ln src link ln -s target link
Substance Another name for the same inode Separate inode holding a path string
Across filesystems Not possible Possible
Target a directory Generally not possible Possible
When original deleted Data remains Becomes a dangling link
inode in ls -li Same as original Different from original

If you need to "cross filesystems" or "point to a directory", a symbolic link is the only choice. To share a file's substance within the same filesystem as a backup-like multi-reference, use a hard link.

Steps

echo "data" > original.txt
ls -li original.txt
1310721 -rw-r--r-- 1 user user 5 May 17 10:00 original.txt

The leading 1310721 is the inode number and 1 is the hard link count. The inode uniquely points to the file's substance (data blocks and metadata). A filename is merely a reference to an inode.

ln original.txt hardlink.txt
ls -li original.txt hardlink.txt
1310721 -rw-r--r-- 2 user user 5 May 17 10:00 original.txt
1310721 -rw-r--r-- 2 user user 5 May 17 10:00 hardlink.txt

Both point to the same inode 1310721 and the link count increased to 2. Editing through either name updates the same substance. Deleting original.txt does not free the inode until the link count reaches 0.

ln -s original.txt symlink.txt
ls -li original.txt symlink.txt
1310721 -rw-r--r-- 2 user user 5 May 17 10:00 original.txt
1310733 lrwxrwxrwx 1 user user 12 May 17 10:01 symlink.txt -> original.txt

symlink.txt has a different inode 1310733 and holds the path string -> original.txt. Note that the type flag is l (link).

readlink symlink.txt
readlink -f symlink.txt
ls -L symlink.txt
original.txt
/home/user/original.txt
-rw-r--r-- 2 user user 5 May 17 10:00 symlink.txt

readlink returns the raw target string and readlink -f returns the finally resolved absolute path. ls -L follows the link and shows the substance's information.

rm original.txt
cat symlink.txt
find . -xtype l
cat: symlink.txt: No such file or directory
./symlink.txt

Deleting the original makes symlink.txt a dangling symlink. find . -xtype l lists broken symbolic links. A hard link, by contrast, keeps its data even after the original name is deleted.

Why Hard Links Cannot Cross Filesystems

Inode numbers are an independent namespace per filesystem. inode 100 on /dev/sda1 and inode 100 on /dev/sdb1 are unrelated substances. Because a hard link is "another name for the same inode", it cannot share an inode across different filesystems, so creation is rejected.

A symbolic link is an independent file holding a path string instead of an inode. A path can be expressed across filesystems, so it can point to another partition, another disk, or even a nonexistent path. The cost is that it becomes a dangling link the moment the target disappears. This design difference creates the trade-off "hard links are robust but constrained; symbolic links are flexible but fragile".

Hard links to directories are generally forbidden to prevent circular references in the directory hierarchy that would trap tree traversal tools like find in infinite loops.

Troubleshooting

Cause: The source and target are on different filesystems

Check:

df original.txt /mnt/other/

Fix: If crossing filesystems is required, use ln -s for a symbolic link. Confirm both are on the same mount with df.

Cause: The link was created with a relative path and then the link itself was moved

Check:

readlink -f link

Fix: If the link may be moved, specify the target with an absolute path (ln -s /abs/path/target link). Note that relative links resolve relative to the link's location.

Cause: A link with the same name already exists, causing File exists

Check:

ls -l link

Fix: Force-overwrite with ln -sf target link. If the target is a directory, also use ln -sfn to avoid accidentally creating the link inside the target directory.

Completion Checklist

  • [ ] Checked inode number and link count with ls -li
  • [ ] Confirmed the link count increases after creating a hard link
  • [ ] Confirmed a symbolic link has a separate inode and holds a path string
  • [ ] Confirmed the final resolution target with readlink -f
  • [ ] Detected broken links with find . -xtype l

Summary

Scenario Command Purpose
Hard link ln src link Share substance within one FS
Symbolic link ln -s target link Cross-FS / directory support
inode check ls -li Link count / substance identity
Resolve target readlink -f Final absolute path
Detect broken find . -xtype l List dangling symlinks

The link mechanism is central to understanding filesystems. Next, move to shell environment and process priorities to connect operational knowledge.

Next Reading