154

I have script.sh that must be run as user2. However, this script can only be run under user1 in my application.

I would like the following command to run:

su user2 -C script.sh 

but be able to run without password.

I also want this to be very restrictive, as in user1 can only run script.sh under user2 and nothing else.

I've tried doing this with sudoers file and just got endlessly confused after hours of trying.

If somebody can provide an explicit example of how this can be accomplished (instead of something generic like use sudoers), it would be greatly appreciated.

1
  • 16
    I don't particularly with the close. This is a question about how to program the system in a particular way; as such, it is about shell programming and is within scope of SO which is for questions about programming. It is clear that a number of people do not see this as so off-topic that it should be down-voted; the question and the answers all have a considerable number of up-votes. Commented Nov 22, 2014 at 18:26

3 Answers 3

195

try running:

su -c "Your command right here" -s /bin/sh username 

This will run the command as username given that you have permissions to sudo as that user.

Sign up to request clarification or add additional context in comments.

4 Comments

It worked as normal user with full sudo rights like this: sudo su -c "Your command right here" -s /bin/sh otheruser
Just a note for any mac users, apparently the syntax is a bit different: su username -c "command".
If your trying to start screen as another user then this might be helpfull - linuxquestions.org/questions/linux-software-2/…
if username (or in the question, user2) has a password itself, this will not allow to skip the prompt for it. the other answer does: no password needed, both user1 and user2.
132

Call visudo and add this:

user1 ALL=(user2) NOPASSWD: /home/user2/bin/test.sh 

The command paths must be absolute! Then call sudo -u user2 /home/user2/bin/test.sh from a user1 shell. Done.

3 Comments

There is a nice description of sudoers format on Ubuntu help. Man page for sudoers is ugly :(
so how would this scale for all users to run as user2, instead of just user1?
You are a freaking lifesaver! I trawled the Internet for like half an hour before I could find anyone giving the specific sudoers instructions (a few people mentioned it, but gave no details...one of my many pet peeves lol)!
1
`su -c "Your command right here" -s /bin/sh username` 

The above command is correct, but on Red Hat if selinux is enforcing it will not allow cron to execute scripts as another user. example; execl: couldn't exec /bin/sh execl: Permission denied

I had to install setroubleshoot and setools and run the following to allow it:

yum install setroubleshoot setools sealert -a /var/log/audit/audit.log grep crond /var/log/audit/audit.log | audit2allow -M mypol semodule -i mypol.p 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.