sudo script in my $HOME/bin folder

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











up vote
1
down vote

favorite












I have a script in my $HOME/bin folder that must be executed with sudo.



If I run



sudo <nameofthescript>


I get



sudo: <nameofthescript>: command not found


What is the most preferable way to add this script to the $PATH of sudo?



Should I move it in /usr/local/sbin?



Should I create a /root/bin folder?







share|improve this question


























    up vote
    1
    down vote

    favorite












    I have a script in my $HOME/bin folder that must be executed with sudo.



    If I run



    sudo <nameofthescript>


    I get



    sudo: <nameofthescript>: command not found


    What is the most preferable way to add this script to the $PATH of sudo?



    Should I move it in /usr/local/sbin?



    Should I create a /root/bin folder?







    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a script in my $HOME/bin folder that must be executed with sudo.



      If I run



      sudo <nameofthescript>


      I get



      sudo: <nameofthescript>: command not found


      What is the most preferable way to add this script to the $PATH of sudo?



      Should I move it in /usr/local/sbin?



      Should I create a /root/bin folder?







      share|improve this question














      I have a script in my $HOME/bin folder that must be executed with sudo.



      If I run



      sudo <nameofthescript>


      I get



      sudo: <nameofthescript>: command not found


      What is the most preferable way to add this script to the $PATH of sudo?



      Should I move it in /usr/local/sbin?



      Should I create a /root/bin folder?









      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 11 at 11:14









      Jeff Schaller

      31.1k846105




      31.1k846105










      asked Apr 11 at 11:12









      check-emee

      418614




      418614




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          sudo sets up a limited PATH, so it will not find your local script. You can configure it to not env_reset for a given user and so on, but perhaps the simplest thing is to write your own one-line mysudo script that does:



          #!/bin/sh
          exec sudo -s PATH="$PATH" exec "$@"


          and then say mysudo somecommand ... instead of sudo somecommand .... It will ask sudo to run a shell, set the PATH back to what it was, then run your command found in that original path.






          share|improve this answer



























            up vote
            0
            down vote













            PATH is an environment variable on operating systems like Linux, Windows which has specific set of directories where executable programs are located. According to your question, your executable script is placed under $HOME/bin directory. So if you want to run a script which is not in PATH, you need to give full file structural path to let OS reach the file to execute it. For example,



            sudo $HOME/bin/<nameofthescript>


            If you do not want to give full file path all the time, you can put that file path in environment veritable using below command. Consedering, you have kept your script in $HOME/bin directory.



            PATH=$PATH:$HOME/bin


            Above command will add your bin directory to the environmental path and then you can use <nameofscript> from anywhere on the system.
            Also, this will only work till the time your bash is running, if you try the same in new terminal, your PATH will change to default. To make it permanent, you need to add below line either in $HOME/.profile or $HOME/.bashrc file.



            export PATH=$PATH:$HOME/bin


            To allow only root user to execute the <nameofscript> from anywhere, you can either put in under /sbin or /usr/sbin directory.






            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%2f436983%2fsudo-script-in-my-home-bin-folder%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote



              accepted










              sudo sets up a limited PATH, so it will not find your local script. You can configure it to not env_reset for a given user and so on, but perhaps the simplest thing is to write your own one-line mysudo script that does:



              #!/bin/sh
              exec sudo -s PATH="$PATH" exec "$@"


              and then say mysudo somecommand ... instead of sudo somecommand .... It will ask sudo to run a shell, set the PATH back to what it was, then run your command found in that original path.






              share|improve this answer
























                up vote
                1
                down vote



                accepted










                sudo sets up a limited PATH, so it will not find your local script. You can configure it to not env_reset for a given user and so on, but perhaps the simplest thing is to write your own one-line mysudo script that does:



                #!/bin/sh
                exec sudo -s PATH="$PATH" exec "$@"


                and then say mysudo somecommand ... instead of sudo somecommand .... It will ask sudo to run a shell, set the PATH back to what it was, then run your command found in that original path.






                share|improve this answer






















                  up vote
                  1
                  down vote



                  accepted







                  up vote
                  1
                  down vote



                  accepted






                  sudo sets up a limited PATH, so it will not find your local script. You can configure it to not env_reset for a given user and so on, but perhaps the simplest thing is to write your own one-line mysudo script that does:



                  #!/bin/sh
                  exec sudo -s PATH="$PATH" exec "$@"


                  and then say mysudo somecommand ... instead of sudo somecommand .... It will ask sudo to run a shell, set the PATH back to what it was, then run your command found in that original path.






                  share|improve this answer












                  sudo sets up a limited PATH, so it will not find your local script. You can configure it to not env_reset for a given user and so on, but perhaps the simplest thing is to write your own one-line mysudo script that does:



                  #!/bin/sh
                  exec sudo -s PATH="$PATH" exec "$@"


                  and then say mysudo somecommand ... instead of sudo somecommand .... It will ask sudo to run a shell, set the PATH back to what it was, then run your command found in that original path.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 11 at 12:25









                  meuh

                  29.3k11649




                  29.3k11649






















                      up vote
                      0
                      down vote













                      PATH is an environment variable on operating systems like Linux, Windows which has specific set of directories where executable programs are located. According to your question, your executable script is placed under $HOME/bin directory. So if you want to run a script which is not in PATH, you need to give full file structural path to let OS reach the file to execute it. For example,



                      sudo $HOME/bin/<nameofthescript>


                      If you do not want to give full file path all the time, you can put that file path in environment veritable using below command. Consedering, you have kept your script in $HOME/bin directory.



                      PATH=$PATH:$HOME/bin


                      Above command will add your bin directory to the environmental path and then you can use <nameofscript> from anywhere on the system.
                      Also, this will only work till the time your bash is running, if you try the same in new terminal, your PATH will change to default. To make it permanent, you need to add below line either in $HOME/.profile or $HOME/.bashrc file.



                      export PATH=$PATH:$HOME/bin


                      To allow only root user to execute the <nameofscript> from anywhere, you can either put in under /sbin or /usr/sbin directory.






                      share|improve this answer


























                        up vote
                        0
                        down vote













                        PATH is an environment variable on operating systems like Linux, Windows which has specific set of directories where executable programs are located. According to your question, your executable script is placed under $HOME/bin directory. So if you want to run a script which is not in PATH, you need to give full file structural path to let OS reach the file to execute it. For example,



                        sudo $HOME/bin/<nameofthescript>


                        If you do not want to give full file path all the time, you can put that file path in environment veritable using below command. Consedering, you have kept your script in $HOME/bin directory.



                        PATH=$PATH:$HOME/bin


                        Above command will add your bin directory to the environmental path and then you can use <nameofscript> from anywhere on the system.
                        Also, this will only work till the time your bash is running, if you try the same in new terminal, your PATH will change to default. To make it permanent, you need to add below line either in $HOME/.profile or $HOME/.bashrc file.



                        export PATH=$PATH:$HOME/bin


                        To allow only root user to execute the <nameofscript> from anywhere, you can either put in under /sbin or /usr/sbin directory.






                        share|improve this answer
























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          PATH is an environment variable on operating systems like Linux, Windows which has specific set of directories where executable programs are located. According to your question, your executable script is placed under $HOME/bin directory. So if you want to run a script which is not in PATH, you need to give full file structural path to let OS reach the file to execute it. For example,



                          sudo $HOME/bin/<nameofthescript>


                          If you do not want to give full file path all the time, you can put that file path in environment veritable using below command. Consedering, you have kept your script in $HOME/bin directory.



                          PATH=$PATH:$HOME/bin


                          Above command will add your bin directory to the environmental path and then you can use <nameofscript> from anywhere on the system.
                          Also, this will only work till the time your bash is running, if you try the same in new terminal, your PATH will change to default. To make it permanent, you need to add below line either in $HOME/.profile or $HOME/.bashrc file.



                          export PATH=$PATH:$HOME/bin


                          To allow only root user to execute the <nameofscript> from anywhere, you can either put in under /sbin or /usr/sbin directory.






                          share|improve this answer














                          PATH is an environment variable on operating systems like Linux, Windows which has specific set of directories where executable programs are located. According to your question, your executable script is placed under $HOME/bin directory. So if you want to run a script which is not in PATH, you need to give full file structural path to let OS reach the file to execute it. For example,



                          sudo $HOME/bin/<nameofthescript>


                          If you do not want to give full file path all the time, you can put that file path in environment veritable using below command. Consedering, you have kept your script in $HOME/bin directory.



                          PATH=$PATH:$HOME/bin


                          Above command will add your bin directory to the environmental path and then you can use <nameofscript> from anywhere on the system.
                          Also, this will only work till the time your bash is running, if you try the same in new terminal, your PATH will change to default. To make it permanent, you need to add below line either in $HOME/.profile or $HOME/.bashrc file.



                          export PATH=$PATH:$HOME/bin


                          To allow only root user to execute the <nameofscript> from anywhere, you can either put in under /sbin or /usr/sbin directory.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Apr 11 at 11:56

























                          answered Apr 11 at 11:50









                          Nitesh B.

                          3711417




                          3711417






















                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f436983%2fsudo-script-in-my-home-bin-folder%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