11

When I wanted to create a hard link in my /home directory in root mode, Linux showed the following error message:

ln: failed to create hard link ‘my_sdb’ => ‘/dev/sda1’: Invalid cross-device link

The above error message is shown below:

# cd /home/user/
# ln /dev/sda1 my_sdb

But I could only create a hard link in the /dev directory, and it was not possible in other directories.

Now, I want to know how to create a hard link from an existing device file (like sdb1) in /home directory (or other directories) ?

21

But I could only create a hard link in the /dev directory and it was not possible in other directories.

As shown by the error message, it is not possible to create a hard link across different filesystems; you can create only soft (symbolic) links.

For instance, if your /home is in a different partition than your root partition, you won't be able to hard link /tmp/foo to /home/user/.

Now, as @RichardNeumann pointed out, /dev is usually mounted as a devtmpfs filesystem. See this example:

[dr01@centos7 ~]$ df
Filesystem                      1K-blocks    Used Available Use% Mounted on
/dev/mapper/centos_centos7-root  46110724 3792836  42317888   9% /
devtmpfs                          4063180       0   4063180   0% /dev
tmpfs                             4078924       0   4078924   0% /dev/shm
tmpfs                             4078924    9148   4069776   1% /run
tmpfs                             4078924       0   4078924   0% /sys/fs/cgroup
/dev/sda1                         1038336  202684    835652  20% /boot
tmpfs                              815788      28    815760   1% /run/user/1000

Therefore you can only create hard links to files in /dev within /dev.

  • 3
    The partition is not the issue. It is, as you stated the cross-filesystem limitation. /dev is usually mounted as a devtmpfs filesystem. Thus you can only create hardlinks to files in /dev within /dev. You e.g. cannot even create hardlinks to /dev/null within /dev/pts, because it is mounted with devpts as a different file system. – Richard Neumann Mar 29 '18 at 11:20
  • 1
    @RichardNeumann Good point. I corrected my answer which was only partly correct. – dr01 Mar 29 '18 at 12:20
11

A hard link cannot be used to achieve what you want, because hard links do not work between file systems.

However, you can achieve what you want with the mknod command.

  1. Run ls -l /dev/devicefile. You should see an output like this:

    crw-rw-rw- 1 root root 1, 9 Mar 29 15:46 /dev/urandom
    
  2. Take note of the number in the size column (1, 9).
  3. Run the command mknod /path/to/destination c 1 9 (substituting the values you want).

Why does this work?

Device files are effectively hard links to an abstract file implemented by the kernel or kernel drivers. Whilst you can't create hard links to file system objects from another file system, these aren't file system objects and so, by knowing their major and minor reference numbers, you can create a reference to them from any file system.

2

Hard links just create another entry in a directory, pointing to a file (where file is a file like thing, such as a directory). Therefore hard-links can not reference a file in another file-system.

Soft-links are you friend in this case. Use ln -s. Softlinks can go across filesystems, and can even point to something that is not there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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