Linux – Why I can’t create a hard link from device file in other than /dev directory

hard linklinuxln

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) ?

Best Answer

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.

Related Question