3

I know it is a condition of hard links that they cannot span filesystems. Does this apply to NFS mounts? Given the following directory structure, would I be able to create a hard link in directory A that points to a file in directory B?

/root
    /A
    /B  <-NFS mount

For example, I'd like to run ln /root/B/file.txt /root/A/linkedfile.txt

2 Answers 2

8

It'd be a good idea to first understand exactly what a hard link is.

Usually on a unix-like system, a filename in a directory points to an inode number - essentially a number for a file. A "hard link" is just creating another filename with that same inode number. Now you have to different names that point to the same numbered file.

But notice that there's not really a direct connection between those two names. The relationship is that Name1 and Name2 both have their inode number set to 12756 - but there's nothing you can hold up and say "this thing in my hand is the link between two files". They're just two database entries that share an id number. You can do a query (slow, since you're walking through every file entry on the system) for filenames that share an id number, but that's it.

So it doesn't mean anything to create a "hard link between two filesystems" - since two filesystems have different numbering schemes (inode 1234 on system one, and 1234 on system two, point to completely different files), and the only thing you have to store is a name+inodeNumber, there's nothing to be done.

7

Well, since /B is a separate file system (a mounted NFS file system) you cannot make a hard link between it and /A, because they are not on the same file system.

It's because a hardlink doesn't make a copy of the data put only a copy of the pointer to that data, so they have to be in the same "address space".

3
  • 7
    Hard links and soft links are both pointers, in a sense. The difference is that a soft link stores a textual representation of the file system path to the target, while a hard link (since it's just a directory entry) just stores the inode nubmer of the target, which must therefore be on the same filesystem as the directory the link itself is in. Commented Aug 28, 2009 at 17:10
  • 1
    The pendant in me feels compelled to point out that the alternative to a hard link is a symbolic link. Commented Jul 7, 2011 at 19:49
  • 4
    I'd add that filenames in a directory are just pointers, too. There's no concept of the "original file" and a "hard link to that file" - they're two names for one object, and neither name is special. and @JesseHallett, I disagree a bit with the idea that they're alternatives; I think it's better just to say that they're different things. Commented Oct 4, 2011 at 21:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.