how to read shadow file in linux using python script









up vote
0
down vote

favorite












file = open('/etc/shadow', 'r')
print(file)



getting error like this
file = open('/etc/shadow', 'r')
IOError: [Errno 13] Permission denied: '/etc/shadow'










share|improve this question

























    up vote
    0
    down vote

    favorite












    file = open('/etc/shadow', 'r')
    print(file)



    getting error like this
    file = open('/etc/shadow', 'r')
    IOError: [Errno 13] Permission denied: '/etc/shadow'










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      file = open('/etc/shadow', 'r')
      print(file)



      getting error like this
      file = open('/etc/shadow', 'r')
      IOError: [Errno 13] Permission denied: '/etc/shadow'










      share|improve this question













      file = open('/etc/shadow', 'r')
      print(file)



      getting error like this
      file = open('/etc/shadow', 'r')
      IOError: [Errno 13] Permission denied: '/etc/shadow'







      file-io






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jun 22 '14 at 8:02









      Poke

      1115




      1115






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          On most systems /etc/shadow is owned by root with rw permissions.



          $ ls -la /etc/shadow


          -rw------- 1 root root 692 Jun 10 19:24 /etc/shadow
          You need to either:



          1. Change the permissons (don't do this it is not safe)
            but you could this by running 'chmod o+r /etc/shadow' as root. This will give the 'other' users read rights to


          2. Run your program as root. Either by
            a. Starting it as root
            su -c 'python myPython.py' //you will be asked to provide the root password.



            b. Starting it with sudo [1]
            sudo python myPython.py this all depends on you sudo configuration but is your best bet other then just starting python as root.



          Also an example to call sudo from within python[5].



          c. Set setuid bit on the program [2]
          This will most likely not work as Python is an interpreted language and most modern Unix systems will disallow (exception being Perl) setuid on interpreted programs as opposed to compiled/binaries.



          chown root programName # Set owner to be root



          chmod +s programName # This gives the program itself the right to run as root.



          Regardless of whom starts it.



          [1]http://en.wikipedia.org/wiki/Sudo



          [2]http://en.wikipedia.org/wiki/Setuid



          [3]Open a file as superuser in python



          [4]Setuid bit on python script : Linux vs Solaris



          [5]Using sudo with Python script



          The problem is not with the source code or python. But with not having the correct file system rights to the '/etc/shadow' file.






          share|improve this answer






















          • please post the code
            – Poke
            Jun 22 '14 at 8:13










          • thank u it's working...
            – Poke
            Jun 22 '14 at 11:37

















          up vote
          0
          down vote













          In python 3 you can do:



          import spwd
          spwd.getspnam('username')


          More information about the spwd module can be found here: https://docs.python.org/3/library/spwd.html#module-spwd






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            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',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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%2fstackoverflow.com%2fquestions%2f24349293%2fhow-to-read-shadow-file-in-linux-using-python-script%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            On most systems /etc/shadow is owned by root with rw permissions.



            $ ls -la /etc/shadow


            -rw------- 1 root root 692 Jun 10 19:24 /etc/shadow
            You need to either:



            1. Change the permissons (don't do this it is not safe)
              but you could this by running 'chmod o+r /etc/shadow' as root. This will give the 'other' users read rights to


            2. Run your program as root. Either by
              a. Starting it as root
              su -c 'python myPython.py' //you will be asked to provide the root password.



              b. Starting it with sudo [1]
              sudo python myPython.py this all depends on you sudo configuration but is your best bet other then just starting python as root.



            Also an example to call sudo from within python[5].



            c. Set setuid bit on the program [2]
            This will most likely not work as Python is an interpreted language and most modern Unix systems will disallow (exception being Perl) setuid on interpreted programs as opposed to compiled/binaries.



            chown root programName # Set owner to be root



            chmod +s programName # This gives the program itself the right to run as root.



            Regardless of whom starts it.



            [1]http://en.wikipedia.org/wiki/Sudo



            [2]http://en.wikipedia.org/wiki/Setuid



            [3]Open a file as superuser in python



            [4]Setuid bit on python script : Linux vs Solaris



            [5]Using sudo with Python script



            The problem is not with the source code or python. But with not having the correct file system rights to the '/etc/shadow' file.






            share|improve this answer






















            • please post the code
              – Poke
              Jun 22 '14 at 8:13










            • thank u it's working...
              – Poke
              Jun 22 '14 at 11:37














            up vote
            1
            down vote













            On most systems /etc/shadow is owned by root with rw permissions.



            $ ls -la /etc/shadow


            -rw------- 1 root root 692 Jun 10 19:24 /etc/shadow
            You need to either:



            1. Change the permissons (don't do this it is not safe)
              but you could this by running 'chmod o+r /etc/shadow' as root. This will give the 'other' users read rights to


            2. Run your program as root. Either by
              a. Starting it as root
              su -c 'python myPython.py' //you will be asked to provide the root password.



              b. Starting it with sudo [1]
              sudo python myPython.py this all depends on you sudo configuration but is your best bet other then just starting python as root.



            Also an example to call sudo from within python[5].



            c. Set setuid bit on the program [2]
            This will most likely not work as Python is an interpreted language and most modern Unix systems will disallow (exception being Perl) setuid on interpreted programs as opposed to compiled/binaries.



            chown root programName # Set owner to be root



            chmod +s programName # This gives the program itself the right to run as root.



            Regardless of whom starts it.



            [1]http://en.wikipedia.org/wiki/Sudo



            [2]http://en.wikipedia.org/wiki/Setuid



            [3]Open a file as superuser in python



            [4]Setuid bit on python script : Linux vs Solaris



            [5]Using sudo with Python script



            The problem is not with the source code or python. But with not having the correct file system rights to the '/etc/shadow' file.






            share|improve this answer






















            • please post the code
              – Poke
              Jun 22 '14 at 8:13










            • thank u it's working...
              – Poke
              Jun 22 '14 at 11:37












            up vote
            1
            down vote










            up vote
            1
            down vote









            On most systems /etc/shadow is owned by root with rw permissions.



            $ ls -la /etc/shadow


            -rw------- 1 root root 692 Jun 10 19:24 /etc/shadow
            You need to either:



            1. Change the permissons (don't do this it is not safe)
              but you could this by running 'chmod o+r /etc/shadow' as root. This will give the 'other' users read rights to


            2. Run your program as root. Either by
              a. Starting it as root
              su -c 'python myPython.py' //you will be asked to provide the root password.



              b. Starting it with sudo [1]
              sudo python myPython.py this all depends on you sudo configuration but is your best bet other then just starting python as root.



            Also an example to call sudo from within python[5].



            c. Set setuid bit on the program [2]
            This will most likely not work as Python is an interpreted language and most modern Unix systems will disallow (exception being Perl) setuid on interpreted programs as opposed to compiled/binaries.



            chown root programName # Set owner to be root



            chmod +s programName # This gives the program itself the right to run as root.



            Regardless of whom starts it.



            [1]http://en.wikipedia.org/wiki/Sudo



            [2]http://en.wikipedia.org/wiki/Setuid



            [3]Open a file as superuser in python



            [4]Setuid bit on python script : Linux vs Solaris



            [5]Using sudo with Python script



            The problem is not with the source code or python. But with not having the correct file system rights to the '/etc/shadow' file.






            share|improve this answer














            On most systems /etc/shadow is owned by root with rw permissions.



            $ ls -la /etc/shadow


            -rw------- 1 root root 692 Jun 10 19:24 /etc/shadow
            You need to either:



            1. Change the permissons (don't do this it is not safe)
              but you could this by running 'chmod o+r /etc/shadow' as root. This will give the 'other' users read rights to


            2. Run your program as root. Either by
              a. Starting it as root
              su -c 'python myPython.py' //you will be asked to provide the root password.



              b. Starting it with sudo [1]
              sudo python myPython.py this all depends on you sudo configuration but is your best bet other then just starting python as root.



            Also an example to call sudo from within python[5].



            c. Set setuid bit on the program [2]
            This will most likely not work as Python is an interpreted language and most modern Unix systems will disallow (exception being Perl) setuid on interpreted programs as opposed to compiled/binaries.



            chown root programName # Set owner to be root



            chmod +s programName # This gives the program itself the right to run as root.



            Regardless of whom starts it.



            [1]http://en.wikipedia.org/wiki/Sudo



            [2]http://en.wikipedia.org/wiki/Setuid



            [3]Open a file as superuser in python



            [4]Setuid bit on python script : Linux vs Solaris



            [5]Using sudo with Python script



            The problem is not with the source code or python. But with not having the correct file system rights to the '/etc/shadow' file.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 23 '17 at 12:27









            Community

            11




            11










            answered Jun 22 '14 at 8:07









            Albert-Jan Stevens

            1062




            1062











            • please post the code
              – Poke
              Jun 22 '14 at 8:13










            • thank u it's working...
              – Poke
              Jun 22 '14 at 11:37
















            • please post the code
              – Poke
              Jun 22 '14 at 8:13










            • thank u it's working...
              – Poke
              Jun 22 '14 at 11:37















            please post the code
            – Poke
            Jun 22 '14 at 8:13




            please post the code
            – Poke
            Jun 22 '14 at 8:13












            thank u it's working...
            – Poke
            Jun 22 '14 at 11:37




            thank u it's working...
            – Poke
            Jun 22 '14 at 11:37












            up vote
            0
            down vote













            In python 3 you can do:



            import spwd
            spwd.getspnam('username')


            More information about the spwd module can be found here: https://docs.python.org/3/library/spwd.html#module-spwd






            share|improve this answer


























              up vote
              0
              down vote













              In python 3 you can do:



              import spwd
              spwd.getspnam('username')


              More information about the spwd module can be found here: https://docs.python.org/3/library/spwd.html#module-spwd






              share|improve this answer
























                up vote
                0
                down vote










                up vote
                0
                down vote









                In python 3 you can do:



                import spwd
                spwd.getspnam('username')


                More information about the spwd module can be found here: https://docs.python.org/3/library/spwd.html#module-spwd






                share|improve this answer














                In python 3 you can do:



                import spwd
                spwd.getspnam('username')


                More information about the spwd module can be found here: https://docs.python.org/3/library/spwd.html#module-spwd







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 9 at 20:46









                Bill DeRose

                5111521




                5111521










                answered Nov 9 at 18:20









                Andreas

                13




                13



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24349293%2fhow-to-read-shadow-file-in-linux-using-python-script%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

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo