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












10














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










share|improve this question





























    10














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










    share|improve this question



























      10












      10








      10


      1





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










      share|improve this question















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







      linux hard-link ln






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 29 at 20:13









      Michael

      1476




      1476










      asked Mar 29 at 10:46









      Reza Harasani

      17210




      17210






















          3 Answers
          3






          active

          oldest

          votes


















          20















          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.






          share|improve this answer



















          • 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 at 11:20






          • 1




            @RichardNeumann Good point. I corrected my answer which was only partly correct.
            – dr01
            Mar 29 at 12:20



















          9














          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.






          share|improve this answer























          • It does not work on some kernels other than Linux, of course.
            – JdeBP
            Mar 29 at 18:55



















          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.






          share|improve this answer





















            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "106"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f434278%2fwhy-i-cant-create-a-hard-link-from-device-file-in-other-than-dev-directory%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            20















            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.






            share|improve this answer



















            • 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 at 11:20






            • 1




              @RichardNeumann Good point. I corrected my answer which was only partly correct.
              – dr01
              Mar 29 at 12:20
















            20















            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.






            share|improve this answer



















            • 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 at 11:20






            • 1




              @RichardNeumann Good point. I corrected my answer which was only partly correct.
              – dr01
              Mar 29 at 12:20














            20












            20








            20







            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.






            share|improve this 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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 29 at 12:19

























            answered Mar 29 at 10:52









            dr01

            15.9k114970




            15.9k114970








            • 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 at 11:20






            • 1




              @RichardNeumann Good point. I corrected my answer which was only partly correct.
              – dr01
              Mar 29 at 12:20














            • 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 at 11:20






            • 1




              @RichardNeumann Good point. I corrected my answer which was only partly correct.
              – dr01
              Mar 29 at 12:20








            3




            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 at 11:20




            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 at 11:20




            1




            1




            @RichardNeumann Good point. I corrected my answer which was only partly correct.
            – dr01
            Mar 29 at 12:20




            @RichardNeumann Good point. I corrected my answer which was only partly correct.
            – dr01
            Mar 29 at 12:20













            9














            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.






            share|improve this answer























            • It does not work on some kernels other than Linux, of course.
              – JdeBP
              Mar 29 at 18:55
















            9














            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.






            share|improve this answer























            • It does not work on some kernels other than Linux, of course.
              – JdeBP
              Mar 29 at 18:55














            9












            9








            9






            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.






            share|improve this answer














            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 7 mins ago

























            answered Mar 29 at 16:09









            wizzwizz4

            283112




            283112












            • It does not work on some kernels other than Linux, of course.
              – JdeBP
              Mar 29 at 18:55


















            • It does not work on some kernels other than Linux, of course.
              – JdeBP
              Mar 29 at 18:55
















            It does not work on some kernels other than Linux, of course.
            – JdeBP
            Mar 29 at 18:55




            It does not work on some kernels other than Linux, of course.
            – JdeBP
            Mar 29 at 18:55











            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.






            share|improve this answer


























              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.






              share|improve this answer
























                2












                2








                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.






                share|improve this answer












                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 29 at 12:31









                ctrl-alt-delor

                10.7k41957




                10.7k41957






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f434278%2fwhy-i-cant-create-a-hard-link-from-device-file-in-other-than-dev-directory%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    サソリ

                    広島県道265号伴広島線

                    Setup Asymptote in Texstudio