how to write a script that will run commands after su, without using -c

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











up vote
5
down vote

favorite
3












I'm trying to automate a standard activity on a system I do not have root access on. The administrator of the systems have given me authority to run sudo su - 'user' command, and only that command. I can not add a -c argument or sudo will fail.



My script will have to start with my permissions, and midway through change to running with the user's permissions. I'm trying to figure out if there is a way to make a script do this for me in a single command?



Before anyone asks, trying to get my visudo permissions extended is quite difficult. While I could probably su using the password I do not know the password of the user, don't want to change it, and really should hardcode it in my script anyways, so regular su without sudo isn't really an option. It seems like there has to be some way to work with the command I'm authorized to use?










share|improve this question



























    up vote
    5
    down vote

    favorite
    3












    I'm trying to automate a standard activity on a system I do not have root access on. The administrator of the systems have given me authority to run sudo su - 'user' command, and only that command. I can not add a -c argument or sudo will fail.



    My script will have to start with my permissions, and midway through change to running with the user's permissions. I'm trying to figure out if there is a way to make a script do this for me in a single command?



    Before anyone asks, trying to get my visudo permissions extended is quite difficult. While I could probably su using the password I do not know the password of the user, don't want to change it, and really should hardcode it in my script anyways, so regular su without sudo isn't really an option. It seems like there has to be some way to work with the command I'm authorized to use?










    share|improve this question

























      up vote
      5
      down vote

      favorite
      3









      up vote
      5
      down vote

      favorite
      3






      3





      I'm trying to automate a standard activity on a system I do not have root access on. The administrator of the systems have given me authority to run sudo su - 'user' command, and only that command. I can not add a -c argument or sudo will fail.



      My script will have to start with my permissions, and midway through change to running with the user's permissions. I'm trying to figure out if there is a way to make a script do this for me in a single command?



      Before anyone asks, trying to get my visudo permissions extended is quite difficult. While I could probably su using the password I do not know the password of the user, don't want to change it, and really should hardcode it in my script anyways, so regular su without sudo isn't really an option. It seems like there has to be some way to work with the command I'm authorized to use?










      share|improve this question















      I'm trying to automate a standard activity on a system I do not have root access on. The administrator of the systems have given me authority to run sudo su - 'user' command, and only that command. I can not add a -c argument or sudo will fail.



      My script will have to start with my permissions, and midway through change to running with the user's permissions. I'm trying to figure out if there is a way to make a script do this for me in a single command?



      Before anyone asks, trying to get my visudo permissions extended is quite difficult. While I could probably su using the password I do not know the password of the user, don't want to change it, and really should hardcode it in my script anyways, so regular su without sudo isn't really an option. It seems like there has to be some way to work with the command I'm authorized to use?







      sudo su






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 12 '14 at 21:14









      HalosGhost

      3,62592035




      3,62592035










      asked Sep 12 '14 at 19:54









      dsollen

      381315




      381315




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          Put the commands that you want to run as the other user into a separate file,
          user2commands, and then do



          sudo su - user < user2commands


          If you don’t want to have a separate file, consider:




          sudo su - user << EOF
          commands to be run as the other user
          ︙
          EOF





          share|improve this answer
















          • 2




            this has weird behavior if the commands need to interact with standard input. For example, if you use cat this won't work (e.g., try echo cat | sudo su - user)
            – pqnet
            Sep 12 '14 at 23:11










          • How to escape the < character when we are putting the entire sudo command within an argument, inside an xml file. I am using phing, so I want to do something like this - <exec command="sudo su auto_deploy << EOF echo 'Logged in user' whoami EOF" dir="$dir.scratchpad" />. The << before the EOF is throwing XML validation error.
            – Sandeepan Nath
            Aug 19 '16 at 11:31


















          up vote
          1
          down vote













          From my personal experience this never worked. Figuring out a workaround creates more work and it may not work properly if you don't have root permissions.






          share|improve this answer



























            up vote
            0
            down vote













            I also have the same question. using





            share








            New contributor




            arpan gupta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.

















              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%2f155326%2fhow-to-write-a-script-that-will-run-commands-after-su-without-using-c%23new-answer', 'question_page');

              );

              Post as a guest






























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              6
              down vote



              accepted










              Put the commands that you want to run as the other user into a separate file,
              user2commands, and then do



              sudo su - user < user2commands


              If you don’t want to have a separate file, consider:




              sudo su - user << EOF
              commands to be run as the other user
              ︙
              EOF





              share|improve this answer
















              • 2




                this has weird behavior if the commands need to interact with standard input. For example, if you use cat this won't work (e.g., try echo cat | sudo su - user)
                – pqnet
                Sep 12 '14 at 23:11










              • How to escape the < character when we are putting the entire sudo command within an argument, inside an xml file. I am using phing, so I want to do something like this - <exec command="sudo su auto_deploy << EOF echo 'Logged in user' whoami EOF" dir="$dir.scratchpad" />. The << before the EOF is throwing XML validation error.
                – Sandeepan Nath
                Aug 19 '16 at 11:31















              up vote
              6
              down vote



              accepted










              Put the commands that you want to run as the other user into a separate file,
              user2commands, and then do



              sudo su - user < user2commands


              If you don’t want to have a separate file, consider:




              sudo su - user << EOF
              commands to be run as the other user
              ︙
              EOF





              share|improve this answer
















              • 2




                this has weird behavior if the commands need to interact with standard input. For example, if you use cat this won't work (e.g., try echo cat | sudo su - user)
                – pqnet
                Sep 12 '14 at 23:11










              • How to escape the < character when we are putting the entire sudo command within an argument, inside an xml file. I am using phing, so I want to do something like this - <exec command="sudo su auto_deploy << EOF echo 'Logged in user' whoami EOF" dir="$dir.scratchpad" />. The << before the EOF is throwing XML validation error.
                – Sandeepan Nath
                Aug 19 '16 at 11:31













              up vote
              6
              down vote



              accepted







              up vote
              6
              down vote



              accepted






              Put the commands that you want to run as the other user into a separate file,
              user2commands, and then do



              sudo su - user < user2commands


              If you don’t want to have a separate file, consider:




              sudo su - user << EOF
              commands to be run as the other user
              ︙
              EOF





              share|improve this answer












              Put the commands that you want to run as the other user into a separate file,
              user2commands, and then do



              sudo su - user < user2commands


              If you don’t want to have a separate file, consider:




              sudo su - user << EOF
              commands to be run as the other user
              ︙
              EOF






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Sep 12 '14 at 22:05









              G-Man

              12.1k92860




              12.1k92860







              • 2




                this has weird behavior if the commands need to interact with standard input. For example, if you use cat this won't work (e.g., try echo cat | sudo su - user)
                – pqnet
                Sep 12 '14 at 23:11










              • How to escape the < character when we are putting the entire sudo command within an argument, inside an xml file. I am using phing, so I want to do something like this - <exec command="sudo su auto_deploy << EOF echo 'Logged in user' whoami EOF" dir="$dir.scratchpad" />. The << before the EOF is throwing XML validation error.
                – Sandeepan Nath
                Aug 19 '16 at 11:31













              • 2




                this has weird behavior if the commands need to interact with standard input. For example, if you use cat this won't work (e.g., try echo cat | sudo su - user)
                – pqnet
                Sep 12 '14 at 23:11










              • How to escape the < character when we are putting the entire sudo command within an argument, inside an xml file. I am using phing, so I want to do something like this - <exec command="sudo su auto_deploy << EOF echo 'Logged in user' whoami EOF" dir="$dir.scratchpad" />. The << before the EOF is throwing XML validation error.
                – Sandeepan Nath
                Aug 19 '16 at 11:31








              2




              2




              this has weird behavior if the commands need to interact with standard input. For example, if you use cat this won't work (e.g., try echo cat | sudo su - user)
              – pqnet
              Sep 12 '14 at 23:11




              this has weird behavior if the commands need to interact with standard input. For example, if you use cat this won't work (e.g., try echo cat | sudo su - user)
              – pqnet
              Sep 12 '14 at 23:11












              How to escape the < character when we are putting the entire sudo command within an argument, inside an xml file. I am using phing, so I want to do something like this - <exec command="sudo su auto_deploy << EOF echo 'Logged in user' whoami EOF" dir="$dir.scratchpad" />. The << before the EOF is throwing XML validation error.
              – Sandeepan Nath
              Aug 19 '16 at 11:31





              How to escape the < character when we are putting the entire sudo command within an argument, inside an xml file. I am using phing, so I want to do something like this - <exec command="sudo su auto_deploy << EOF echo 'Logged in user' whoami EOF" dir="$dir.scratchpad" />. The << before the EOF is throwing XML validation error.
              – Sandeepan Nath
              Aug 19 '16 at 11:31













              up vote
              1
              down vote













              From my personal experience this never worked. Figuring out a workaround creates more work and it may not work properly if you don't have root permissions.






              share|improve this answer
























                up vote
                1
                down vote













                From my personal experience this never worked. Figuring out a workaround creates more work and it may not work properly if you don't have root permissions.






                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  From my personal experience this never worked. Figuring out a workaround creates more work and it may not work properly if you don't have root permissions.






                  share|improve this answer












                  From my personal experience this never worked. Figuring out a workaround creates more work and it may not work properly if you don't have root permissions.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 12 '14 at 21:07









                  unixmiah

                  326110




                  326110




















                      up vote
                      0
                      down vote













                      I also have the same question. using





                      share








                      New contributor




                      arpan gupta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.





















                        up vote
                        0
                        down vote













                        I also have the same question. using





                        share








                        New contributor




                        arpan gupta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.



















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          I also have the same question. using





                          share








                          New contributor




                          arpan gupta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          I also have the same question. using






                          share








                          New contributor




                          arpan gupta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.








                          share


                          share






                          New contributor




                          arpan gupta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          answered 2 mins ago









                          arpan gupta

                          1




                          1




                          New contributor




                          arpan gupta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.





                          New contributor





                          arpan gupta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






                          arpan gupta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f155326%2fhow-to-write-a-script-that-will-run-commands-after-su-without-using-c%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