sudo su - user in a shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sudo su - user in a shell script
# 1  
Old 10-06-2010
sudo su - user in a shell script

Normally i would google, but I did not know how to google the problem I am facing now also being a newbie in shell scripting.
Okay, the requirement is
user1 has sudo rule to su - user2(NO PASSWORD) and user2 has will be able to sudo certain commands
so following works fine from command prompt
user1@host] sudo su - user2
the above command opens up a new shell, logging me in as user2
user2@host] sudo cmd1
user2@host] sudo cmd2
user2@host] sudo cmd3
user2@host] exit
user1@host]
How would I achieve the above using a shell script?
I have tried putting following commands in a shell script
sudo su - user2 -c "sudo cmd1" (this prompts me for a password though I have a sudo rule to not prompt for one.)
sudo su - user2 (this gets me out of the shell and takes me to "user2@host]")
none works for me.
Any help on this would be greatly appreciated.
# 2  
Old 10-06-2010
Why not allow user1 to call cmd1, cmd2 and cmd3 as user2 without password:
Code:
# Example of change to /etc/sudoers
Cmnd_Alias      U2CMDS = /usr/local/bin/cmd1, /usr/local/bin/cmd2, /usr/local/bin/cmd3

# Allow user1 to run cmd(1-3) as user2 without password
user1  ALL=(user2)  NOPASSWD: U2CMDS

then user1 can:
Code:
user1@host] sudo -u user2 cmd1
user1@host] sudo -u user2 cmd2
user1@host] sudo -u user2 cmd3

Or, if you do it a lot and always call cmd1 then cmd2 then cmd3 put these three calls into 1 script that user1 can call.

Last edited by Chubler_XL; 10-06-2010 at 07:46 PM.. Reason: Show example of sudoers change required
# 3  
Old 10-06-2010
set the sudo command to group, not to single user.

With that, you can easily manage the sudo list.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Require help in creating a Sudo/Shell script

Hello Friends, I have a scenario to create a script, I know many of you feel this as simple script. I am not much familiar with unix scripting, please help me out. Situation:- 1. I have a list of config files like 40+ would be getting deployed in the /app/abcd/src/Config/ (This will... (2 Replies)
Discussion started by: ganjvin
2 Replies

2. Shell Programming and Scripting

Shell sIs there something special I need to do when using sudo in a script?

I have a script in which I used "sudo -s" I notice some extremely strange behavior when executing this script. To investigate this I decided to recreate the problem in the following script. I notice that "sudo -s" is only being executed one time. Soon after completely falls apart. Is there... (4 Replies)
Discussion started by: busi386
4 Replies

3. Shell Programming and Scripting

Script to give a user sudo permissions

Can some one please let me know a script which gives the user sudo permissions? Thanks in advance.... (6 Replies)
Discussion started by: Revanth547
6 Replies

4. Shell Programming and Scripting

How to Login as another user through Shell script from current user[Not Root]

Hi Every body, I would need a shell script program to login as different user and perform some copy commands in the script. example: Supppose ora_toms is the active user ora_toms should be able to run a script where user: ftptomsp pass: XXX should login through and run the commands ... (9 Replies)
Discussion started by: ujjwal27
9 Replies

5. Shell Programming and Scripting

Help with Shell Script on sudo

I want to execute a script(generateReport.sh) which resides on root home directory from shell script(localtrigger.sh) as root by using sudo. The thing is i cant edit /etc/sudoers (i can but m not allowed to do it , ethically) i cant change ownership of generateReport.sh script When i try... (7 Replies)
Discussion started by: saket
7 Replies

6. Shell Programming and Scripting

How to switch user in shell scripting (without sudo)?

Hi everyone: I have a big trouble, I need create a script that must switch user and then must execute certain commands, sadly neither my user nor the second user have no privileges for sudo, I've tried everything but seems su doesn't accept input redirection, please help me, it's very... (8 Replies)
Discussion started by: edgarvm
8 Replies

7. Shell Programming and Scripting

root user command in shell script execute as normal user

Hi All I have written one shell script for GPRS route add is given below named GPRSRouteSet.sh URL="www.google.com" VBURL="10.5.2.211" echo "Setting route for $URL for GPRS" URL_Address=`nslookup $URL|grep Address:|grep -v "#"|awk -F " " '{print $2}'|head -1` echo "Executing ... (3 Replies)
Discussion started by: mnmonu
3 Replies

8. Shell Programming and Scripting

shell script problem , sudo mount command

cat test.sh sudo mount -t vfat /dev/sda7 /media/Ddrive If i double click the test.sh file and select run in terminal then the terminal prompts for password. How can i avoid typing password? Or if i double click test.sh file and select run then nothing happens. What i'm trying "Double... (3 Replies)
Discussion started by: cola
3 Replies

9. Shell Programming and Scripting

How do i change to super user then revert back to ordinary user ,using shell script?

Hi all, I am trying to eject the cdrom from a livecd after certain stage... Now assuming that it is possible to eject,please consider my issue!!! The OS boots into a regular user by default...so i am unable to use the eject command to push out the drive... However if i try pfexec eject it... (3 Replies)
Discussion started by: wrapster
3 Replies

10. UNIX for Dummies Questions & Answers

sudo in OS X shell script without password prompt??

I've written a shell script to alter a particular preference file on OS X (10.3.9), which works fine (tested by running the script from the terminal sat in front of the box). Problem is, I now have to run this script remotely across a number of machines via remote desktop, so where I've used the... (1 Reply)
Discussion started by: Brad_GNET
1 Replies
Login or Register to Ask a Question