Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have access to another user's account using command:

sudo su - other_user

I do not have enter password. It seems that this is the only command that sudo allows me.

After that I'm able to perform several tasks like file copying and server restarting. I would like to automate those tasks so that a script can be executed by me, which would get other_user's rights and do what it has to do. However, sudo spawns another shell, which until I exit it blocks script execution. Is it possible to control that shell and to send commands into it from my script?

Update 2011-11-01:

I tried suggestions from the answers and both these commands:

sudo -u other_user whoami
sudo su - other_user -c "whoami"

ask for password.

Unfortunately, I could not find sudoers to look at settings. The situation annoys me a bit because a have privileges to do what I want to, but can't automate that.

share|improve this question
add comment

2 Answers 2

Use the -coption of the su command to execute commands as another user.
If needed, you can also use the output of those commands in your script, or in your shell.

Example:

[infeligo@server ~] cat /home/kenny/secretfile.txt  
cat: /home/kenny/secretfile.txt: Permission denied  
[infeligo@server ~] sudo su - kenny -c "cat /home/kenny/secretfile.txt"  
Dear diary: Today was a boring day at work. Good thing it's a friday!  
[infeligo@server ~]
share|improve this answer
add comment

The use of su is not entirely necessary.

$ sudo -u other_user whoami
other_user
share|improve this answer
add comment

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.