Switch User in within a Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Switch User in within a Shell Script
# 8  
Old 10-07-2010
I think I will go with the SSH kyes exchange from one user to another so the process can be worked.

Correct me If I'm wrong. By exchanging ssh keys, it will do the su without the password prompt or it will only work if I do ssh command?
# 9  
Old 10-07-2010
One way if you don't want to execute the user's login profile.
Code:
su user -c "command"

Depending on your environment I find it works best using "remsh" with a suitable ".rhosts" file - even within the same computer.
Code:
remsh localhost -l username -n "command"

# 10  
Old 10-07-2010
Quote:
Originally Posted by Afi_Linux
I think I will go with the SSH kyes exchange from one user to another so the process can be worked.

Correct me If I'm wrong. By exchanging ssh keys, it will do the su without the password prompt or it will only work if I do ssh command?
The su needs a password for everyone but root.
  • The su logname - -c "commands" as root or with password interaction is equivalent to
  • old, security-dissed .rhosts entries with: 'rsh localhost -l logname [ -n if no input ] "commands"' is equivalent to
  • matching ssh keys and 'ssh localhost -l logname [ -n if no input ] "commands"' is equivalent to
  • matching ssh2 keys and 'ssh2 localhost -l logname [ -n if no input ] "commands"'.
The last is most defensible to a security auditor, and a bit more secure even if you do not read all the instructions! BTW, permissions on control files are critical to all the but su! Sometimes, give or take what shell runs "commands" (unless they all got to use $SHELL) and the running of the remote .profile or .kshrc or whatever.
# 11  
Old 10-27-2010
Hey DGPickett,

A question to clarify. I need to execute 4 different commands with 4 different users so it means that I have to switch the users 4 times. As per my understanding, I can use the following commands.

set LOG='ABC/ABC/ABC.LOG

1 sudo/su - user -c "command" >> $LOG
exit

2. sudo/su - user2 -c "comand" >> $LOG
exit

3. sudo/su - user3 -c "command" >> $LOG
exit

4. sudo/su - user4 -c "command" >> $LOG

By doing that, each user have it's own environment veriables so do I need to setup the envs or it will catch it own its own.

One of the commands, I want to add grep command to one of the processes. If it finds, kill it. how can I get a specific running process and kill it if finds?

Your help is really appreciated
Thanks,
Afi
# 12  
Old 10-27-2010
You don't need to use exit the sudo/su -c command will exit when the command completes:

Code:
sudo -u user1 ksh -c /usr/local/bin/myapp >> $LOG 2>&1
sudo -u user2 ksh -c "ps -ef | grep [m]yapp | awk '{print $2}' | xargs -r kill"

Code:
su user1 "-c /usr/local/bin/myapp" >> $LOG 2>&1
su user2 "-c ps -ef | grep [m]yapp | awk '{print \$2}' | xargs -r kill"


Last edited by Chubler_XL; 10-27-2010 at 07:50 PM..
# 13  
Old 10-27-2010
I'm guessing that the following lines are two different commands.

1. sudo -u user1 ksh -c /usr/local/bin/myapp >> $LOG 2>&1
2. sudo -u user2 ksh -c "ps -ef | grep [m]yapp | awk '{print $2}' | xargs -r kill"

Do I need to add (>> $LOG) for the 2nd line if I need to capture it in the logs?

Thanks,
# 14  
Old 10-27-2010
Yes these are two seperate commands (as examples of running single program or mini script as a pipeline).

You would need to capture the output of the kill command (typically kill dosn't output anything to stdout but best to redirect this and stderr to your logfile). BTW the ksh -c part is only needed when running script commands and the following is sufficient:

Code:
sudo -u user1 /usr/local/bin/myapp >> $LOG 2>&1
sudo -u user2 ksh -c "ps -ef | grep [m]yapp | awk '{print $2}' | xargs -r kill" >> $LOG 2>&1

Edit: Note that the redirection is happening outside of the command invoked by sudo (ie not within the quotes in the 2nd example) so the LOG file will be written by the logged in user (not user1 or user2). If you get anything much more complex than a 1 liner best to write a shell script and invoke that with sudo. Also this way you can restrict the use of sudo for this particular script and not allow a user to do anything they want as user2.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to switch user in shell script?

HI in a server we can't login with root user directly but i can login with different user and then i can switch to root user by su command Requirement is there anyway where i can write a script without mentioning password in file as mentioning the root password is not the... (3 Replies)
Discussion started by: scriptor
3 Replies

2. Shell Programming and Scripting

How to Switch from Local user to root user from a shell script?

Hi, I need to switch from local user to root user in a shell script. I need to make it automated so that it doesn't prompt for the root password. I heard the su command will do that work but it prompt for the password. and also can someone tell me whether su command spawns a new shell or... (1 Reply)
Discussion started by: Little
1 Replies

3. Shell Programming and Scripting

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

Hi everyone: 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 su - , I've tried everything but seems su doesn't accept input redirection, please help me, ... (4 Replies)
Discussion started by: ooilinlove
4 Replies

4. Shell Programming and Scripting

Switch user without password inside shell

I want to switch to another user without password inside shell. I used the below command and it is not working. sudo su - user1 user1 is not in the sudoers file. This incident will be reported. I'm getting the above message. If I want to add user1 into the sudoers file using... (5 Replies)
Discussion started by: Roozo
5 Replies

5. UNIX for Dummies Questions & Answers

How to switch the user before executing a shell script from web page??

hi, i want to execute a shell script as a different user. the flow is like this. there is a html web page from which i have to call a shell script. web server is apache. to call the shell script from html page, a perl script is required. so the html page calls the perl script and the perl... (2 Replies)
Discussion started by: Little
2 Replies

6. Shell Programming and Scripting

How to switch user using shell script ?

Hi, script1.sh script2.sh script3.sh From above, script1.sh is the main script which is executed from root user, creates installation directory, changing ownership and execution rights etc..etc.. and finally calls scripot2.sh and script3.sh to create the database as well as for post... (1 Reply)
Discussion started by: milink
1 Replies

7. Shell Programming and Scripting

Switch user inside shell script

Hi, I am trying to create one script where I have to login as another user inside the script to exeute some commands How can i achieve this? Many thanks in advance. (4 Replies)
Discussion started by: prarat
4 Replies

8. 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

9. Shell Programming and Scripting

switch user inside a script

Hi Is there any way to switch user inside a shell script? (4 Replies)
Discussion started by: ./hari.sh
4 Replies

10. Shell Programming and Scripting

switch user inside a script

Hi, I wrote a unix script that will perform differnt tasks on bahalf of number of users. I use "sudo" to run the script. The problem is when I execute the command: su - user -c "xxx " > output_file, I get the system output header frm the su command. Is there a way to get rid of it instdead of... (2 Replies)
Discussion started by: nimo
2 Replies
Login or Register to Ask a Question