How do I force the user to become root

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite
2












I have written a bash script for use on my ubuntu box. Now I would like to prevent running this script under my own user and only able to run it as root (sudo).



Is there a possibility to force this. Can I somehow let my script ask for root permissions if I run it under my own username?










share|improve this question













migrated from stackoverflow.com Jan 6 '12 at 14:56


This question came from our site for professional and enthusiast programmers.


















    up vote
    6
    down vote

    favorite
    2












    I have written a bash script for use on my ubuntu box. Now I would like to prevent running this script under my own user and only able to run it as root (sudo).



    Is there a possibility to force this. Can I somehow let my script ask for root permissions if I run it under my own username?










    share|improve this question













    migrated from stackoverflow.com Jan 6 '12 at 14:56


    This question came from our site for professional and enthusiast programmers.
















      up vote
      6
      down vote

      favorite
      2









      up vote
      6
      down vote

      favorite
      2






      2





      I have written a bash script for use on my ubuntu box. Now I would like to prevent running this script under my own user and only able to run it as root (sudo).



      Is there a possibility to force this. Can I somehow let my script ask for root permissions if I run it under my own username?










      share|improve this question













      I have written a bash script for use on my ubuntu box. Now I would like to prevent running this script under my own user and only able to run it as root (sudo).



      Is there a possibility to force this. Can I somehow let my script ask for root permissions if I run it under my own username?







      bash permissions






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 22 '10 at 10:55









      Peter Smit

      4792927




      4792927




      migrated from stackoverflow.com Jan 6 '12 at 14:56


      This question came from our site for professional and enthusiast programmers.






      migrated from stackoverflow.com Jan 6 '12 at 14:56


      This question came from our site for professional and enthusiast programmers.






















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          8
          down vote



          accepted










          I have a standard function I use in bash for this very purpose:



          # Check if we're root and re-execute if we're not.
          rootcheck ()
          if [ $(id -u) != "0" ]
          then
          sudo "$0" "$@" # Modified as suggested below.
          exit $?
          fi



          There's probably more elegant ways (I wrote this AGES ago!), but this works fine for me.



          To use this function consistently in a script, you should pass it the arguments received by the script originally.



          Usage: rootcheck "$@" (rootcheck alone will not pass any arguments to the rootcheck function)






          share|improve this answer


















          • 1




            Possibly exec sudo "$0" "$@" to avoid problems with spaces in arguments, and avoid have the first execution hanging around?
            – Douglas Leeder
            Sep 23 '10 at 9:33










          • Yeah, that would be a decided improvement. I'll update the answer to reflect it.
            – JUST MY correct OPINION
            Sep 23 '10 at 10:11










          • Note that if it's run as a function like this, you need to pass in the script's parameters (i.e. run it with rootcheck "$@"). Or you can just put the if block inline rather than in a function. Also, this should be done very early in the script, as anything before it will be run twice (once normally, once as root).
            – Gordon Davisson
            Sep 23 '10 at 19:18










          • I do it first thing in the script, so yeah. Early is right.
            – JUST MY correct OPINION
            Sep 24 '10 at 0:11










          • It's sometimes good to run sanity checks (e.g. does the file in question exist?) before sudo'ing -- I tend to do this by starting the script with something like if [[ $1 == "--no-sanity-checks" ]]; then shift; else run sanity checks; fi and then modify the sudo command to exec sudo "$0" --no-sanity-checks "$@"
            – Gordon Davisson
            Sep 25 '10 at 17:28

















          up vote
          4
          down vote













          i am using this single line version, easy to copy paste on top of scripts



          [ `whoami` = root ] || sudo "$0" "$@"; exit $?; 





          share|improve this answer
















          • 1




            This would work, but it creates a dependency on sudo. It's probably better form to just check $EUID and quit if not root.
            – HalosGhost
            Sep 2 '14 at 21:15

















          up vote
          2
          down vote













          sudo chown root yourcode
          sudo chmod 500 yourcode


          Voila!






          share|improve this answer



























            up vote
            1
            down vote













            You can change the permissions, so that only root can execute it.
            Or you could use whoami command and in case that it isn't root force sudo.






            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',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              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%2f28454%2fhow-do-i-force-the-user-to-become-root%23new-answer', 'question_page');

              );

              Post as a guest






























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              8
              down vote



              accepted










              I have a standard function I use in bash for this very purpose:



              # Check if we're root and re-execute if we're not.
              rootcheck ()
              if [ $(id -u) != "0" ]
              then
              sudo "$0" "$@" # Modified as suggested below.
              exit $?
              fi



              There's probably more elegant ways (I wrote this AGES ago!), but this works fine for me.



              To use this function consistently in a script, you should pass it the arguments received by the script originally.



              Usage: rootcheck "$@" (rootcheck alone will not pass any arguments to the rootcheck function)






              share|improve this answer


















              • 1




                Possibly exec sudo "$0" "$@" to avoid problems with spaces in arguments, and avoid have the first execution hanging around?
                – Douglas Leeder
                Sep 23 '10 at 9:33










              • Yeah, that would be a decided improvement. I'll update the answer to reflect it.
                – JUST MY correct OPINION
                Sep 23 '10 at 10:11










              • Note that if it's run as a function like this, you need to pass in the script's parameters (i.e. run it with rootcheck "$@"). Or you can just put the if block inline rather than in a function. Also, this should be done very early in the script, as anything before it will be run twice (once normally, once as root).
                – Gordon Davisson
                Sep 23 '10 at 19:18










              • I do it first thing in the script, so yeah. Early is right.
                – JUST MY correct OPINION
                Sep 24 '10 at 0:11










              • It's sometimes good to run sanity checks (e.g. does the file in question exist?) before sudo'ing -- I tend to do this by starting the script with something like if [[ $1 == "--no-sanity-checks" ]]; then shift; else run sanity checks; fi and then modify the sudo command to exec sudo "$0" --no-sanity-checks "$@"
                – Gordon Davisson
                Sep 25 '10 at 17:28














              up vote
              8
              down vote



              accepted










              I have a standard function I use in bash for this very purpose:



              # Check if we're root and re-execute if we're not.
              rootcheck ()
              if [ $(id -u) != "0" ]
              then
              sudo "$0" "$@" # Modified as suggested below.
              exit $?
              fi



              There's probably more elegant ways (I wrote this AGES ago!), but this works fine for me.



              To use this function consistently in a script, you should pass it the arguments received by the script originally.



              Usage: rootcheck "$@" (rootcheck alone will not pass any arguments to the rootcheck function)






              share|improve this answer


















              • 1




                Possibly exec sudo "$0" "$@" to avoid problems with spaces in arguments, and avoid have the first execution hanging around?
                – Douglas Leeder
                Sep 23 '10 at 9:33










              • Yeah, that would be a decided improvement. I'll update the answer to reflect it.
                – JUST MY correct OPINION
                Sep 23 '10 at 10:11










              • Note that if it's run as a function like this, you need to pass in the script's parameters (i.e. run it with rootcheck "$@"). Or you can just put the if block inline rather than in a function. Also, this should be done very early in the script, as anything before it will be run twice (once normally, once as root).
                – Gordon Davisson
                Sep 23 '10 at 19:18










              • I do it first thing in the script, so yeah. Early is right.
                – JUST MY correct OPINION
                Sep 24 '10 at 0:11










              • It's sometimes good to run sanity checks (e.g. does the file in question exist?) before sudo'ing -- I tend to do this by starting the script with something like if [[ $1 == "--no-sanity-checks" ]]; then shift; else run sanity checks; fi and then modify the sudo command to exec sudo "$0" --no-sanity-checks "$@"
                – Gordon Davisson
                Sep 25 '10 at 17:28












              up vote
              8
              down vote



              accepted







              up vote
              8
              down vote



              accepted






              I have a standard function I use in bash for this very purpose:



              # Check if we're root and re-execute if we're not.
              rootcheck ()
              if [ $(id -u) != "0" ]
              then
              sudo "$0" "$@" # Modified as suggested below.
              exit $?
              fi



              There's probably more elegant ways (I wrote this AGES ago!), but this works fine for me.



              To use this function consistently in a script, you should pass it the arguments received by the script originally.



              Usage: rootcheck "$@" (rootcheck alone will not pass any arguments to the rootcheck function)






              share|improve this answer














              I have a standard function I use in bash for this very purpose:



              # Check if we're root and re-execute if we're not.
              rootcheck ()
              if [ $(id -u) != "0" ]
              then
              sudo "$0" "$@" # Modified as suggested below.
              exit $?
              fi



              There's probably more elegant ways (I wrote this AGES ago!), but this works fine for me.



              To use this function consistently in a script, you should pass it the arguments received by the script originally.



              Usage: rootcheck "$@" (rootcheck alone will not pass any arguments to the rootcheck function)







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 11 mins ago









              yosefrow

              1768




              1768










              answered Sep 22 '10 at 11:02









              JUST MY correct OPINION

              1962




              1962







              • 1




                Possibly exec sudo "$0" "$@" to avoid problems with spaces in arguments, and avoid have the first execution hanging around?
                – Douglas Leeder
                Sep 23 '10 at 9:33










              • Yeah, that would be a decided improvement. I'll update the answer to reflect it.
                – JUST MY correct OPINION
                Sep 23 '10 at 10:11










              • Note that if it's run as a function like this, you need to pass in the script's parameters (i.e. run it with rootcheck "$@"). Or you can just put the if block inline rather than in a function. Also, this should be done very early in the script, as anything before it will be run twice (once normally, once as root).
                – Gordon Davisson
                Sep 23 '10 at 19:18










              • I do it first thing in the script, so yeah. Early is right.
                – JUST MY correct OPINION
                Sep 24 '10 at 0:11










              • It's sometimes good to run sanity checks (e.g. does the file in question exist?) before sudo'ing -- I tend to do this by starting the script with something like if [[ $1 == "--no-sanity-checks" ]]; then shift; else run sanity checks; fi and then modify the sudo command to exec sudo "$0" --no-sanity-checks "$@"
                – Gordon Davisson
                Sep 25 '10 at 17:28












              • 1




                Possibly exec sudo "$0" "$@" to avoid problems with spaces in arguments, and avoid have the first execution hanging around?
                – Douglas Leeder
                Sep 23 '10 at 9:33










              • Yeah, that would be a decided improvement. I'll update the answer to reflect it.
                – JUST MY correct OPINION
                Sep 23 '10 at 10:11










              • Note that if it's run as a function like this, you need to pass in the script's parameters (i.e. run it with rootcheck "$@"). Or you can just put the if block inline rather than in a function. Also, this should be done very early in the script, as anything before it will be run twice (once normally, once as root).
                – Gordon Davisson
                Sep 23 '10 at 19:18










              • I do it first thing in the script, so yeah. Early is right.
                – JUST MY correct OPINION
                Sep 24 '10 at 0:11










              • It's sometimes good to run sanity checks (e.g. does the file in question exist?) before sudo'ing -- I tend to do this by starting the script with something like if [[ $1 == "--no-sanity-checks" ]]; then shift; else run sanity checks; fi and then modify the sudo command to exec sudo "$0" --no-sanity-checks "$@"
                – Gordon Davisson
                Sep 25 '10 at 17:28







              1




              1




              Possibly exec sudo "$0" "$@" to avoid problems with spaces in arguments, and avoid have the first execution hanging around?
              – Douglas Leeder
              Sep 23 '10 at 9:33




              Possibly exec sudo "$0" "$@" to avoid problems with spaces in arguments, and avoid have the first execution hanging around?
              – Douglas Leeder
              Sep 23 '10 at 9:33












              Yeah, that would be a decided improvement. I'll update the answer to reflect it.
              – JUST MY correct OPINION
              Sep 23 '10 at 10:11




              Yeah, that would be a decided improvement. I'll update the answer to reflect it.
              – JUST MY correct OPINION
              Sep 23 '10 at 10:11












              Note that if it's run as a function like this, you need to pass in the script's parameters (i.e. run it with rootcheck "$@"). Or you can just put the if block inline rather than in a function. Also, this should be done very early in the script, as anything before it will be run twice (once normally, once as root).
              – Gordon Davisson
              Sep 23 '10 at 19:18




              Note that if it's run as a function like this, you need to pass in the script's parameters (i.e. run it with rootcheck "$@"). Or you can just put the if block inline rather than in a function. Also, this should be done very early in the script, as anything before it will be run twice (once normally, once as root).
              – Gordon Davisson
              Sep 23 '10 at 19:18












              I do it first thing in the script, so yeah. Early is right.
              – JUST MY correct OPINION
              Sep 24 '10 at 0:11




              I do it first thing in the script, so yeah. Early is right.
              – JUST MY correct OPINION
              Sep 24 '10 at 0:11












              It's sometimes good to run sanity checks (e.g. does the file in question exist?) before sudo'ing -- I tend to do this by starting the script with something like if [[ $1 == "--no-sanity-checks" ]]; then shift; else run sanity checks; fi and then modify the sudo command to exec sudo "$0" --no-sanity-checks "$@"
              – Gordon Davisson
              Sep 25 '10 at 17:28




              It's sometimes good to run sanity checks (e.g. does the file in question exist?) before sudo'ing -- I tend to do this by starting the script with something like if [[ $1 == "--no-sanity-checks" ]]; then shift; else run sanity checks; fi and then modify the sudo command to exec sudo "$0" --no-sanity-checks "$@"
              – Gordon Davisson
              Sep 25 '10 at 17:28












              up vote
              4
              down vote













              i am using this single line version, easy to copy paste on top of scripts



              [ `whoami` = root ] || sudo "$0" "$@"; exit $?; 





              share|improve this answer
















              • 1




                This would work, but it creates a dependency on sudo. It's probably better form to just check $EUID and quit if not root.
                – HalosGhost
                Sep 2 '14 at 21:15














              up vote
              4
              down vote













              i am using this single line version, easy to copy paste on top of scripts



              [ `whoami` = root ] || sudo "$0" "$@"; exit $?; 





              share|improve this answer
















              • 1




                This would work, but it creates a dependency on sudo. It's probably better form to just check $EUID and quit if not root.
                – HalosGhost
                Sep 2 '14 at 21:15












              up vote
              4
              down vote










              up vote
              4
              down vote









              i am using this single line version, easy to copy paste on top of scripts



              [ `whoami` = root ] || sudo "$0" "$@"; exit $?; 





              share|improve this answer












              i am using this single line version, easy to copy paste on top of scripts



              [ `whoami` = root ] || sudo "$0" "$@"; exit $?; 






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Sep 2 '14 at 20:31









              fsh

              411




              411







              • 1




                This would work, but it creates a dependency on sudo. It's probably better form to just check $EUID and quit if not root.
                – HalosGhost
                Sep 2 '14 at 21:15












              • 1




                This would work, but it creates a dependency on sudo. It's probably better form to just check $EUID and quit if not root.
                – HalosGhost
                Sep 2 '14 at 21:15







              1




              1




              This would work, but it creates a dependency on sudo. It's probably better form to just check $EUID and quit if not root.
              – HalosGhost
              Sep 2 '14 at 21:15




              This would work, but it creates a dependency on sudo. It's probably better form to just check $EUID and quit if not root.
              – HalosGhost
              Sep 2 '14 at 21:15










              up vote
              2
              down vote













              sudo chown root yourcode
              sudo chmod 500 yourcode


              Voila!






              share|improve this answer
























                up vote
                2
                down vote













                sudo chown root yourcode
                sudo chmod 500 yourcode


                Voila!






                share|improve this answer






















                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  sudo chown root yourcode
                  sudo chmod 500 yourcode


                  Voila!






                  share|improve this answer












                  sudo chown root yourcode
                  sudo chmod 500 yourcode


                  Voila!







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 22 '10 at 11:00









                  mkoistinen

                  1234




                  1234




















                      up vote
                      1
                      down vote













                      You can change the permissions, so that only root can execute it.
                      Or you could use whoami command and in case that it isn't root force sudo.






                      share|improve this answer
























                        up vote
                        1
                        down vote













                        You can change the permissions, so that only root can execute it.
                        Or you could use whoami command and in case that it isn't root force sudo.






                        share|improve this answer






















                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          You can change the permissions, so that only root can execute it.
                          Or you could use whoami command and in case that it isn't root force sudo.






                          share|improve this answer












                          You can change the permissions, so that only root can execute it.
                          Or you could use whoami command and in case that it isn't root force sudo.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Sep 22 '10 at 11:00









                          Nuz

                          1111




                          1111



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f28454%2fhow-do-i-force-the-user-to-become-root%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay