Why I can't create a hard link from device file in other than /dev directory?

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.

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.

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.