5

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?

7

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
  • 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
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.