Quote:
Originally Posted by aravindts
Hi All,
I want to switch between users from within a shell script.
I am executing a shell script as user1 and I need to switch to user2, execute some commands in user2’s envt and return back to user1.
Both user1 and user2 do not have root privileges.
Any pointers would be helpful.
Thanks in advance,
Aravind.T.S.
|
===========================
1. As user1, you would need to know the password of user2
su - <user2> -c "command/you/want/to_execute -options ; ./another_command | filtered output >> withredirection"
2. create an ssh key (without passphrase) for user1, put the public key into user2 ~/.ssh/authorized_keys, command now looks like this - to be run as user1 - (and does not require user1 to know user2 password)
ssh <user2>@localhost "command/you/want/to_execute -options ; ./another_command | filtered output >> withredirection"
3. if you want the output of command to be owned by user1, change to:
ssh <user2>@localhost "command/you/want/to_execute -options ; ./another_command | filtered output" >> withredirection
4. you can go and setup sudo as well (beyond the scope of this) , but assuming you dont have root, use the above, if you have root or can get root to add for you, variations of:
sudo -- su - <user2> -c "command/you/want/to_execute -options ; ./another_command | filtered output >> withredirection"
Have fun