Why are hard links only valid within the same filesystem?

Hopefully I can answer this in a way that makes sense for you. A file system in Linux, is generally made up of a partition that is formatted in one of various ways (gotta love choice!) that you store your files on. Be that your system files, or your personal files... they are all stored on a file system. This part you seem to understand.

But what if you partition your hard drive to have more than one partition (think Apple Pie cut up into pieces), or add an additional hard drive (perhaps a USB stick?). For the sake of argument, they all have file systems on them as well.

When you look at the files on your computer, you're seeing a visual representation of data on your partition's file system. Each file name corresponds to what is called an inode, which is where your data, behind the scenes, really lives. A hard link lets you have multiple "file names" (for lack of a better description) that point to the same inode. This only works if those hard links are on the same file system. A symbolic link instead points to the "file name", which then is linked to the inode holding your data. Forgive my crude artwork but hopefully this explains better.

image.jpg             image2.jpg
          \           /
           [your data]

here, image.jpg, and image2.jpg both point directly to your data. They are both hardlinks. However...

image.jpg    <-----------  image2.jpg
           \ 
             [your data]

In this (crude) example, image2.jpg doesn't point to your data, it points to the image.jpg... which is a link to your data.

Symbolic links can work across file system boundaries (assuming that file system is attached and mounted, like your usb stick). However a hard link cannot. It knows nothing about what is on your other file system, or where your data there is stored.

Hopefully this helps make better sense.


The File system is composed by a directory structure composed for directory entries to organize files. Each directory entry associates a file-name with an inode.

Soft links (symbolic) are directory entries that does not contain data, it just points to another entry (a file or directory in the same file system or other file system). And when you delete the pointed file, the symbolic link becomes unusable.

Hard links are directory entry that contains the file name and inode number. When you remove the last hard link, you can no longer access the file.

Difference between soft-link and hard-link

Conclusion:

As the inode is a data structure used to represent a file-system object, it's internal to the File system, and you can't point to an inode of another file system.

Thus, hard-links are only valid within the same File system, but soft-links (Symbolic link) can span file systems as they simply point to another directory entry (The interface of the file-system, and not an internal object).


The root filesystem can be made up of several filesystems; /usr/local might be mounted on a separate partition and /home might be on another partition on a networked disk somewhere else. In this case, a hard link for /usr/local/bin/git (for example) may not be created outside of /usr/local, because it would span filesystems.

The reason for this is that the inodes are allocated separately for /, /usr/local and /home (again, in this example), and when you create a hard link you really just make an additional name for an inode.