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.

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.


[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 ~]

The use of su is not entirely necessary.

$ sudo -u other_user whoami

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.