21

I have /home/myuser/Desktop/rc/.netrc file that i want hardlink to /root, ie home directory of root user. When i do:

ln /home/user/Destkop/rc/.netrc /root

it gives the following error:

ln: creating hard link /root/.netrc' =>.netrc': Invalid cross-device link

but it works when i hardlink the file to myuser's home, ie to /home/myuser.

So, what's the problem, why it says invalid cross-devices when there is only one file system here?

PS. I am using RHEL6

Elvin Aslanov
  • 367
  • 1
  • 3
  • 8
  • btw my .netrc permissions is 600, as it should be – Elvin Aslanov Jun 12 '13 at 09:48
  • 1
    Are you sure the two directories are on the same filesystem? Run `mount | column -t` and take a look. Or, run `mountpoint -d /`, `mountpoint -d /home`, `mountpoint -d user`, and `mountpoint -d /root`. You should get `... is not a mountpoint` for the last three. –  Jun 12 '13 at 10:22
  • 2
    Please don't [cross post](http://stackoverflow.com/questions/17062231/invalid-cross-device-link-while-hardlinking-in-the-same-file-system) on multiple StackExchange sites. –  Jun 12 '13 at 10:26
  • @EvanTeitelman The real look is not `mount | column -t` but `column -t < /proc/mounts`. – Hauke Laging Jun 12 '13 at 12:11
  • 1
    @HaukeLaging: You're right. `/proc/mounts` is more likely to show all of the mounts on a system than `/etc/mtab`, which is where `mount` pulls its information from. Though, it is worth noting that on many modern systems, `/etc/mtab` and `/proc/mounts` are both linked to `/proc/self/mounts`. –  Jun 12 '13 at 19:57
  • The error message you give is inconsistent with the command you quote. Copy-paste the exact command you ran and the corresponding error message. While you're at it, add the output of `df /home/user/Destkop/rc/.netrc /root/.netrc` and `ls -ld /home/user/Destkop/rc/.netrc /root/.netrc`. – Gilles 'SO- stop being evil' Jun 12 '13 at 23:06

5 Answers5

18

it's most likely that your /home directory isn't on the same partition as the /root directory.

You can easily check this with cat /etc/fstab hardlinks cannot be created between different partitions, only symlinks can.

fduff
  • 4,925
  • 4
  • 35
  • 39
11

It seem to me the BEST way is NOT to use a HARD link but to use a symbolic link instead. You'll find it to be far less of a hassle and things work just fine. Hard link have some interesting problems if you are not aware of them.

See man ln and checkout the -s option!

Please in general use symbolic links instead of HARD links.

mdpc
  • 6,736
  • 3
  • 32
  • 46
  • 13
    While it might solve the problem, it doesn't really explain why this is happening. – Bex Aug 18 '15 at 10:22
2

This error can also occur if either the source or destination folder being copied to/from is an fscrypt encrypted folder.

Within the same fscrypt encrypted folder (i.e., both source and destination in the same fscrypt encrypted folder or any of its subdirectories), hard linking should work normally.

ZarakshR
  • 131
  • 3
1

file systems like ext3/ext4 uses i-nodes to store file meta-data (mode, atime, ctime, mtime, uid, gid, size, links_count, acl etc.)

i-node table is a linear array of struct ext4_inode. this struct ext4_inode has field i_links_count. that refers to hard link count.

whenever new hard link to file is created this link_count value of that files i-node is incremented.

e.g.

ls -l /home/raju/test.sh

-rwxrwxrwx 2 raju raju 287 Aug 23 23:19 /home/raju/test.sh

here 2 is link count.

different volumes have different i-node tables.

directory is flat file that maps file names to an i-node number on the filesystem.

directory entries across the filesystem that reference the same i-node number--these are known as hard links.

directory entry of one filesystem (volume1) can not reference i-node which is on different filesystem (volume2).

so ln fails with invalid cross-device link error.

but you can always create symbolic links to files on different filesystems/ volumes.

symlinks do not use i-node to reference file they use file path names. check link target of symlink.

reference: https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout#Inode_Table

raju
  • 41
  • 1
0

Probably, earlier a symlinked folder was replaced by its real destination folder on source. And now your backup script fails.