Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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

2 Answers 2

up vote 2 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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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