How can I change user inside function

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












1















I have a bash script on centos7, and I need to execute some commands as different user. But it seems sudo works as expected outside function and didn't work inside bash function. I run script as ssh centos@myhost.com 'bash -s' < script.sh



test()
sudo -Eu root bash
echo "inside $(whoami)"
# other commands ...


test

sudo -Eu root bash
echo "outside $(whoami)"


Running this as



ssh centos@myhost.com 'bash -s' < script.sh


Prints:



outside root
inside centos


root user is given as an example for reproducibility. What is the reason behind this results? How can I execute bunch of commands inside a function as different user?










share|improve this question
























  • sudo runs a command, when the command exits you are back as the user you started with. You can't switch users inside a shell script using sudo. Also test is the name of a standard utility. It would be advisable to use another name for your function. Not writing this as a proper answer as I can't demonstrate alternative solutions (I'm on a system without sudo).

    – Kusalananda
    Feb 21 at 20:19












  • @Kusalananda But I changed user to root outside function.

    – tsh
    Feb 21 at 20:26











  • Your script will start first one interactive bash session as root (the one started from inside the function). When that exits (by you terminating it), it starts another one (outside the function). It will not produce the output that you show.

    – Kusalananda
    Feb 21 at 20:33












  • @Kusalananda My bad, forgot to mention, I execute this script through ssh. ssh centos@myhost.com 'bash -s' < script.sh this way it produces result, shown above. I've edited my question.

    – tsh
    Feb 21 at 20:38
















1















I have a bash script on centos7, and I need to execute some commands as different user. But it seems sudo works as expected outside function and didn't work inside bash function. I run script as ssh centos@myhost.com 'bash -s' < script.sh



test()
sudo -Eu root bash
echo "inside $(whoami)"
# other commands ...


test

sudo -Eu root bash
echo "outside $(whoami)"


Running this as



ssh centos@myhost.com 'bash -s' < script.sh


Prints:



outside root
inside centos


root user is given as an example for reproducibility. What is the reason behind this results? How can I execute bunch of commands inside a function as different user?










share|improve this question
























  • sudo runs a command, when the command exits you are back as the user you started with. You can't switch users inside a shell script using sudo. Also test is the name of a standard utility. It would be advisable to use another name for your function. Not writing this as a proper answer as I can't demonstrate alternative solutions (I'm on a system without sudo).

    – Kusalananda
    Feb 21 at 20:19












  • @Kusalananda But I changed user to root outside function.

    – tsh
    Feb 21 at 20:26











  • Your script will start first one interactive bash session as root (the one started from inside the function). When that exits (by you terminating it), it starts another one (outside the function). It will not produce the output that you show.

    – Kusalananda
    Feb 21 at 20:33












  • @Kusalananda My bad, forgot to mention, I execute this script through ssh. ssh centos@myhost.com 'bash -s' < script.sh this way it produces result, shown above. I've edited my question.

    – tsh
    Feb 21 at 20:38














1












1








1








I have a bash script on centos7, and I need to execute some commands as different user. But it seems sudo works as expected outside function and didn't work inside bash function. I run script as ssh centos@myhost.com 'bash -s' < script.sh



test()
sudo -Eu root bash
echo "inside $(whoami)"
# other commands ...


test

sudo -Eu root bash
echo "outside $(whoami)"


Running this as



ssh centos@myhost.com 'bash -s' < script.sh


Prints:



outside root
inside centos


root user is given as an example for reproducibility. What is the reason behind this results? How can I execute bunch of commands inside a function as different user?










share|improve this question
















I have a bash script on centos7, and I need to execute some commands as different user. But it seems sudo works as expected outside function and didn't work inside bash function. I run script as ssh centos@myhost.com 'bash -s' < script.sh



test()
sudo -Eu root bash
echo "inside $(whoami)"
# other commands ...


test

sudo -Eu root bash
echo "outside $(whoami)"


Running this as



ssh centos@myhost.com 'bash -s' < script.sh


Prints:



outside root
inside centos


root user is given as an example for reproducibility. What is the reason behind this results? How can I execute bunch of commands inside a function as different user?







bash sudo function






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 21 at 21:08









ctrl-alt-delor

12k42461




12k42461










asked Feb 21 at 20:16









tshtsh

123




123












  • sudo runs a command, when the command exits you are back as the user you started with. You can't switch users inside a shell script using sudo. Also test is the name of a standard utility. It would be advisable to use another name for your function. Not writing this as a proper answer as I can't demonstrate alternative solutions (I'm on a system without sudo).

    – Kusalananda
    Feb 21 at 20:19












  • @Kusalananda But I changed user to root outside function.

    – tsh
    Feb 21 at 20:26











  • Your script will start first one interactive bash session as root (the one started from inside the function). When that exits (by you terminating it), it starts another one (outside the function). It will not produce the output that you show.

    – Kusalananda
    Feb 21 at 20:33












  • @Kusalananda My bad, forgot to mention, I execute this script through ssh. ssh centos@myhost.com 'bash -s' < script.sh this way it produces result, shown above. I've edited my question.

    – tsh
    Feb 21 at 20:38


















  • sudo runs a command, when the command exits you are back as the user you started with. You can't switch users inside a shell script using sudo. Also test is the name of a standard utility. It would be advisable to use another name for your function. Not writing this as a proper answer as I can't demonstrate alternative solutions (I'm on a system without sudo).

    – Kusalananda
    Feb 21 at 20:19












  • @Kusalananda But I changed user to root outside function.

    – tsh
    Feb 21 at 20:26











  • Your script will start first one interactive bash session as root (the one started from inside the function). When that exits (by you terminating it), it starts another one (outside the function). It will not produce the output that you show.

    – Kusalananda
    Feb 21 at 20:33












  • @Kusalananda My bad, forgot to mention, I execute this script through ssh. ssh centos@myhost.com 'bash -s' < script.sh this way it produces result, shown above. I've edited my question.

    – tsh
    Feb 21 at 20:38

















sudo runs a command, when the command exits you are back as the user you started with. You can't switch users inside a shell script using sudo. Also test is the name of a standard utility. It would be advisable to use another name for your function. Not writing this as a proper answer as I can't demonstrate alternative solutions (I'm on a system without sudo).

– Kusalananda
Feb 21 at 20:19






sudo runs a command, when the command exits you are back as the user you started with. You can't switch users inside a shell script using sudo. Also test is the name of a standard utility. It would be advisable to use another name for your function. Not writing this as a proper answer as I can't demonstrate alternative solutions (I'm on a system without sudo).

– Kusalananda
Feb 21 at 20:19














@Kusalananda But I changed user to root outside function.

– tsh
Feb 21 at 20:26





@Kusalananda But I changed user to root outside function.

– tsh
Feb 21 at 20:26













Your script will start first one interactive bash session as root (the one started from inside the function). When that exits (by you terminating it), it starts another one (outside the function). It will not produce the output that you show.

– Kusalananda
Feb 21 at 20:33






Your script will start first one interactive bash session as root (the one started from inside the function). When that exits (by you terminating it), it starts another one (outside the function). It will not produce the output that you show.

– Kusalananda
Feb 21 at 20:33














@Kusalananda My bad, forgot to mention, I execute this script through ssh. ssh centos@myhost.com 'bash -s' < script.sh this way it produces result, shown above. I've edited my question.

– tsh
Feb 21 at 20:38






@Kusalananda My bad, forgot to mention, I execute this script through ssh. ssh centos@myhost.com 'bash -s' < script.sh this way it produces result, shown above. I've edited my question.

– tsh
Feb 21 at 20:38











2 Answers
2






active

oldest

votes


















4














Don't do this.



Getting the two lines of output in the wrong order should have been a hint that something was wrong.



When you execute your script using



ssh centos@myhost.com 'bash -s' < script.sh


the following happens:



  1. The script is passed to bash -s on the shells standard input stream.

  2. The script defines the test function and calls it.

  3. The sudo bash command in the function starts a root shell.

  4. This shell inherits the standard input stream, which is the script.

  5. The root shell continues reading the script from the point after the function call. It does this because this is where the stream is at at this point.

Now you have a root shell started from within the test function, which is executing instructions after the test call.



  1. It starts a second root shell, which inherits the standard input stream (i.e. the shell script stream).

You now have a centos shell executing a root shell, executing a root shell.



  1. The second root shell executes echo "outside $(whoami)" outputting outside root, which is the last line of the script.

  2. There is nothing more to read, so the second root shell terminates.

  3. So does the first root shell.

  4. The original bash -s shell executes echo "inside $(whoami)" (because it's part of the function that it started to execute earlier), outputting inside centos.

  5. The shell function call exits and since the rest of the script has already been read by the two root shells, the original shell has nothing more to read and terminates.

sudo is strictly for executing another command (or starting an interactive shell). The change in user is for that other command only. When the command terminates, you are back as the original user. You can not use sudo to "switch to another user" in the middle of a script and then run a part of that script as that other user (unless, of course, you deliberately write your script to be executed in the bizarre manner untangled above, but that sort of coding belongs in an obfuscated code contest).



To execute a set of commands as root in a script, you must give those commands to the sudo invocation. For example:



sudo bash -c 'echo "Now running as $USER"; echo "whoami outputs $(whoami)"'


After the sudo bash -c command exits, you are back as your original user. Always.






share|improve this answer
































    1














    You need to execute the commands in the shell that you start with sudo.



    $ f() 
    sudo -Eu root bash -c whoami

    $ f
    root





    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%2f502167%2fhow-can-i-change-user-inside-function%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









      4














      Don't do this.



      Getting the two lines of output in the wrong order should have been a hint that something was wrong.



      When you execute your script using



      ssh centos@myhost.com 'bash -s' < script.sh


      the following happens:



      1. The script is passed to bash -s on the shells standard input stream.

      2. The script defines the test function and calls it.

      3. The sudo bash command in the function starts a root shell.

      4. This shell inherits the standard input stream, which is the script.

      5. The root shell continues reading the script from the point after the function call. It does this because this is where the stream is at at this point.

      Now you have a root shell started from within the test function, which is executing instructions after the test call.



      1. It starts a second root shell, which inherits the standard input stream (i.e. the shell script stream).

      You now have a centos shell executing a root shell, executing a root shell.



      1. The second root shell executes echo "outside $(whoami)" outputting outside root, which is the last line of the script.

      2. There is nothing more to read, so the second root shell terminates.

      3. So does the first root shell.

      4. The original bash -s shell executes echo "inside $(whoami)" (because it's part of the function that it started to execute earlier), outputting inside centos.

      5. The shell function call exits and since the rest of the script has already been read by the two root shells, the original shell has nothing more to read and terminates.

      sudo is strictly for executing another command (or starting an interactive shell). The change in user is for that other command only. When the command terminates, you are back as the original user. You can not use sudo to "switch to another user" in the middle of a script and then run a part of that script as that other user (unless, of course, you deliberately write your script to be executed in the bizarre manner untangled above, but that sort of coding belongs in an obfuscated code contest).



      To execute a set of commands as root in a script, you must give those commands to the sudo invocation. For example:



      sudo bash -c 'echo "Now running as $USER"; echo "whoami outputs $(whoami)"'


      After the sudo bash -c command exits, you are back as your original user. Always.






      share|improve this answer





























        4














        Don't do this.



        Getting the two lines of output in the wrong order should have been a hint that something was wrong.



        When you execute your script using



        ssh centos@myhost.com 'bash -s' < script.sh


        the following happens:



        1. The script is passed to bash -s on the shells standard input stream.

        2. The script defines the test function and calls it.

        3. The sudo bash command in the function starts a root shell.

        4. This shell inherits the standard input stream, which is the script.

        5. The root shell continues reading the script from the point after the function call. It does this because this is where the stream is at at this point.

        Now you have a root shell started from within the test function, which is executing instructions after the test call.



        1. It starts a second root shell, which inherits the standard input stream (i.e. the shell script stream).

        You now have a centos shell executing a root shell, executing a root shell.



        1. The second root shell executes echo "outside $(whoami)" outputting outside root, which is the last line of the script.

        2. There is nothing more to read, so the second root shell terminates.

        3. So does the first root shell.

        4. The original bash -s shell executes echo "inside $(whoami)" (because it's part of the function that it started to execute earlier), outputting inside centos.

        5. The shell function call exits and since the rest of the script has already been read by the two root shells, the original shell has nothing more to read and terminates.

        sudo is strictly for executing another command (or starting an interactive shell). The change in user is for that other command only. When the command terminates, you are back as the original user. You can not use sudo to "switch to another user" in the middle of a script and then run a part of that script as that other user (unless, of course, you deliberately write your script to be executed in the bizarre manner untangled above, but that sort of coding belongs in an obfuscated code contest).



        To execute a set of commands as root in a script, you must give those commands to the sudo invocation. For example:



        sudo bash -c 'echo "Now running as $USER"; echo "whoami outputs $(whoami)"'


        After the sudo bash -c command exits, you are back as your original user. Always.






        share|improve this answer



























          4












          4








          4







          Don't do this.



          Getting the two lines of output in the wrong order should have been a hint that something was wrong.



          When you execute your script using



          ssh centos@myhost.com 'bash -s' < script.sh


          the following happens:



          1. The script is passed to bash -s on the shells standard input stream.

          2. The script defines the test function and calls it.

          3. The sudo bash command in the function starts a root shell.

          4. This shell inherits the standard input stream, which is the script.

          5. The root shell continues reading the script from the point after the function call. It does this because this is where the stream is at at this point.

          Now you have a root shell started from within the test function, which is executing instructions after the test call.



          1. It starts a second root shell, which inherits the standard input stream (i.e. the shell script stream).

          You now have a centos shell executing a root shell, executing a root shell.



          1. The second root shell executes echo "outside $(whoami)" outputting outside root, which is the last line of the script.

          2. There is nothing more to read, so the second root shell terminates.

          3. So does the first root shell.

          4. The original bash -s shell executes echo "inside $(whoami)" (because it's part of the function that it started to execute earlier), outputting inside centos.

          5. The shell function call exits and since the rest of the script has already been read by the two root shells, the original shell has nothing more to read and terminates.

          sudo is strictly for executing another command (or starting an interactive shell). The change in user is for that other command only. When the command terminates, you are back as the original user. You can not use sudo to "switch to another user" in the middle of a script and then run a part of that script as that other user (unless, of course, you deliberately write your script to be executed in the bizarre manner untangled above, but that sort of coding belongs in an obfuscated code contest).



          To execute a set of commands as root in a script, you must give those commands to the sudo invocation. For example:



          sudo bash -c 'echo "Now running as $USER"; echo "whoami outputs $(whoami)"'


          After the sudo bash -c command exits, you are back as your original user. Always.






          share|improve this answer















          Don't do this.



          Getting the two lines of output in the wrong order should have been a hint that something was wrong.



          When you execute your script using



          ssh centos@myhost.com 'bash -s' < script.sh


          the following happens:



          1. The script is passed to bash -s on the shells standard input stream.

          2. The script defines the test function and calls it.

          3. The sudo bash command in the function starts a root shell.

          4. This shell inherits the standard input stream, which is the script.

          5. The root shell continues reading the script from the point after the function call. It does this because this is where the stream is at at this point.

          Now you have a root shell started from within the test function, which is executing instructions after the test call.



          1. It starts a second root shell, which inherits the standard input stream (i.e. the shell script stream).

          You now have a centos shell executing a root shell, executing a root shell.



          1. The second root shell executes echo "outside $(whoami)" outputting outside root, which is the last line of the script.

          2. There is nothing more to read, so the second root shell terminates.

          3. So does the first root shell.

          4. The original bash -s shell executes echo "inside $(whoami)" (because it's part of the function that it started to execute earlier), outputting inside centos.

          5. The shell function call exits and since the rest of the script has already been read by the two root shells, the original shell has nothing more to read and terminates.

          sudo is strictly for executing another command (or starting an interactive shell). The change in user is for that other command only. When the command terminates, you are back as the original user. You can not use sudo to "switch to another user" in the middle of a script and then run a part of that script as that other user (unless, of course, you deliberately write your script to be executed in the bizarre manner untangled above, but that sort of coding belongs in an obfuscated code contest).



          To execute a set of commands as root in a script, you must give those commands to the sudo invocation. For example:



          sudo bash -c 'echo "Now running as $USER"; echo "whoami outputs $(whoami)"'


          After the sudo bash -c command exits, you are back as your original user. Always.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 21 at 21:25

























          answered Feb 21 at 20:53









          KusalanandaKusalananda

          136k17257425




          136k17257425























              1














              You need to execute the commands in the shell that you start with sudo.



              $ f() 
              sudo -Eu root bash -c whoami

              $ f
              root





              share|improve this answer



























                1














                You need to execute the commands in the shell that you start with sudo.



                $ f() 
                sudo -Eu root bash -c whoami

                $ f
                root





                share|improve this answer

























                  1












                  1








                  1







                  You need to execute the commands in the shell that you start with sudo.



                  $ f() 
                  sudo -Eu root bash -c whoami

                  $ f
                  root





                  share|improve this answer













                  You need to execute the commands in the shell that you start with sudo.



                  $ f() 
                  sudo -Eu root bash -c whoami

                  $ f
                  root






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 21 at 20:24









                  TomaszTomasz

                  10.2k53068




                  10.2k53068



























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f502167%2fhow-can-i-change-user-inside-function%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

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

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?