Solaris · Link

Solaris · Lesson 14

Link

Hard and soft link concepts in Solaris. inode behavior explained. Practical use cases. Troubleshoot broken links.

What are links in Unix/Solaris?

A “link” is simply another name or pointer to a file. Solaris, like other Unix systems, supports hard links and soft (symbolic) links. Both help you avoid copying data and allow multiple paths to refer to the same content.

To really understand the difference, you must think in terms of inodes — the internal structures that store metadata and point to file data on disk.

Inodes, hard links and soft links – conceptually

Inode = the real file

The inode stores file metadata (size, timestamps, permissions, disk blocks). Directory entries (names) point to inodes; data lives behind the inode.

Hard link = extra name

A hard link is another directory entry pointing to the same inode. Same inode number, same data; only the name is different.

Soft link = special file

A soft (symbolic) link is a small file that stores a path to another file or directory. It has its own inode and can break if the target path disappears.

Step-by-step: hard vs soft links with inodes

Work through these examples in your lab. Watch the inode numbers and link counts carefully — they tell the real story behind the names.

1. Create a hard link and compare inodes

Hard links are additional directory entries pointing to the same inode (same underlying file) on the same filesystem.

terminal — links
solaris-lab
[root@solaris ~]# cd /export/home/lab
[root@solaris ~/lab]# echo "Solaris link demo" > original.txt
 
[root@solaris ~/lab]# ln original.txt hardlink.txt
 
[root@solaris ~/lab]# ls -li
123456 -rw-r--r-- 2 root root 20 Jan 11 12:00 hardlink.txt
123456 -rw-r--r-- 2 root root 20 Jan 11 12:00 original.txt
 
# Note:
# - Same inode number (123456)
# - Link count is 2 (second column)

2. Create a soft (symbolic) link and compare inodes

Soft links are separate files that store a path to the target. They have their own inode and can span filesystems.

terminal — links
solaris-lab
[root@solaris ~/lab]# ln -s original.txt softlink.txt
 
[root@solaris ~/lab]# ls -li
123456 -rw-r--r-- 2 root root 20 Jan 11 12:00 hardlink.txt
123456 -rw-r--r-- 2 root root 20 Jan 11 12:00 original.txt
123789 lrwxrwxrwx 1 root root 12 Jan 11 12:02 softlink.txt -> original.txt
 
# Note:
# - softlink.txt has a DIFFERENT inode (123789)
# - Type 'l' (symlink) in ls output
# - It only stores the path 'original.txt'

3. Delete the original file and see how links behave

When you remove the original name, hard links still work (same inode). Soft links become broken pointers.

terminal — links
solaris-lab
[root@solaris ~/lab]# ls -li
123456 -rw-r--r-- 2 root root 20 Jan 11 12:00 hardlink.txt
123456 -rw-r--r-- 2 root root 20 Jan 11 12:00 original.txt
123789 lrwxrwxrwx 1 root root 12 Jan 11 12:02 softlink.txt -> original.txt
 
[root@solaris ~/lab]# rm original.txt
 
[root@solaris ~/lab]# ls -li
123456 -rw-r--r-- 1 root root 20 Jan 11 12:00 hardlink.txt
123789 lrwxrwxrwx 1 root root 12 Jan 11 12:02 softlink.txt -> original.txt
 
# Check contents:
[root@solaris ~/lab]# cat hardlink.txt
Solaris link demo
 
[root@solaris ~/lab]# cat softlink.txt
cat: cannot open softlink.txt: No such file or directory
 
# Explanation:
# - The data still exists via hardlink.txt (inode 123456)
# - softlink.txt points to a path that no longer exists → broken symlink

4. Delete hard links and watch the link count

The file data on disk is removed only when the last hard link is deleted and no process is using it.

terminal — links
solaris-lab
[root@solaris ~/lab]# ls -li
123456 -rw-r--r-- 1 root root 20 Jan 11 12:00 hardlink.txt
123789 lrwxrwxrwx 1 root root 12 Jan 11 12:02 softlink.txt -> original.txt
 
[root@solaris ~/lab]# rm hardlink.txt
 
[root@solaris ~/lab]# ls -li
123789 lrwxrwxrwx 1 root root 12 Jan 11 12:02 softlink.txt -> original.txt
 
[root@solaris ~/lab]# cat softlink.txt
cat: cannot open softlink.txt: No such file or directory
 
# Now:
# - inode 123456 has no directory entries, so data is freed.
# - softlink.txt still exists but is a broken link.

5. Limitations: directories and filesystems

Hard links cannot cross filesystems and are normally not used for directories. Soft links have more flexibility but can break.

terminal — links
solaris-lab
# Cross-filesystem example (assume /data is a different filesystem)
[root@solaris ~]# cd /export/home/lab
[root@solaris ~/lab]# touch file1
 
[root@solaris ~/lab]# ln file1 /data/file1
ln: cannot create /data/file1: Invalid cross-device link
 
# But soft link works across filesystems:
[root@solaris ~/lab]# ln -s /export/home/lab/file1 /data/file1-link
 
# Hard link to directory is usually restricted:
[root@solaris ~/lab]# ln project project_hard
ln: cannot create hard link project_hard to project: Is a directory
 
# But soft link to directory is common:
[root@solaris ~/lab]# ln -s /export/home/lab/project /projects/current
[root@solaris ~/lab]# ls -ld /projects/current
lrwxrwxrwx 1 root root 24 Jan 11 12:30 /projects/current -> /export/home/lab/project

Hard link vs soft link – detailed comparison

Hard links

  • Same inode as the original file (check with ls -li).
  • Share the same permissions, owner, group and timestamps.
  • Data stays on disk until the LAST hard link is removed.
  • Cannot span filesystems (must be on the same filesystem).
  • Normally not allowed for directories (to avoid loops).
  • Deleting any one name does NOT affect access via other hard links.

Soft (symbolic) links

  • Different inode; type shown as 'l' in ls output.
  • Stores a path to the target; can point to files or directories.
  • Can cross filesystems and even point to non-existent paths.
  • If the target is removed or moved, the symlink becomes broken.
  • Permissions of the symlink itself are usually irrelevant; access depends on the target file.
  • Very common for configs, shared directories and compatibility symlinks.

When to use which type of link?

  • Use soft links when you want flexibility: pointing to directories, crossing filesystems, or creating human-friendly paths (e.g. /var/www/currentrelease-123).
  • Use hard links when you want multiple names for exactly the same file data and you are sure everything is on the same filesystem.
  • Be careful when cleaning up: removing the “original” name of a file may not actually delete the data if hard links still exist.
  • Periodically check for broken symlinks in critical trees (e.g. find /path -xtype l) to avoid surprises.

Understanding inodes and links will help you a lot when working with backups, NFS mounts, ZFS snapshots and even package management.