LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-12-2007, 05:27 AM   #1
aravindts
LQ Newbie
 
Registered: Sep 2006
Posts: 5

Rep: Reputation: 0
UNIX : Switching users in a shell script?


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.
 
Old 10-12-2007, 08:52 AM   #2
santana
Member
 
Registered: Sep 2004
Distribution: FC, ubuntu, OpenSuse
Posts: 112

Rep: Reputation: 16
Since no one helped you I offer an idea...

You could leverage ssh to do something like this. Make sure you have sshd installed and running on the system. ssh can run a single command on a remote(or local) system and get the output back to your local system.

Syntax:
ssh user@systemname 'command_and_arguments'

You must put the entire remote command and its arguments in quotes if it contains any special characters that the local shell might otherwise try to interpret (you want them to be interpreted by the remote shell). Doing this alone may not work since ssh will expect you to enter a password to login however you can work around this by setting up automatic logins by exporting ssh keys to get around the password prompt. specifically follow these instructions:

http://www.bluegum.com/Software/ssh-auth.html

good luck, and if you fuind a better way please share!!!
 
Old 10-12-2007, 09:25 AM   #3
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Quote:
Originally Posted by aravindts
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.
How about setting up user1 as a sudoer (so that he can execute all or certain commands as user2)?
 
Old 10-12-2007, 09:36 AM   #4
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

I am able to do this interactively -- provided I know the alternate user's password:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate switching user in script.

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions used in this script displayed with local utility "version")"
version bash

echo

echo " Starting user:"
whoami

echo
echo " Prepare to enter password for tiller, and run in sh:"
su - tiller sh

echo
echo " Final user:"
whoami

exit 0
Producing:
Code:
% ./s1

(Versions used in this script displayed with local utility version)
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)

 Starting user:
makyo

 Prepare to enter password for tiller, and run in sh:
Password:
sh-2.05b$ whoami
tiller
sh-2.05b$ exit
exit

 Final user:
makyo
cheers, makyo
 
Old 10-12-2007, 11:08 AM   #5
student04
Member
 
Registered: Jan 2004
Location: USA
Distribution: macOS, OpenBSD
Posts: 669

Rep: Reputation: 34
Try playing with the command "login". I read it can do that for you in the man pages, but couldn't work it in my terminal because I run bash straight out, and not sh (what it's asking for as the "login shell").

-AM
 
Old 09-07-2012, 03:24 AM   #6
ph0rty
LQ Newbie
 
Registered: Sep 2012
Distribution: OpenSUSE, CentOS, RHEL
Posts: 8

Rep: Reputation: Disabled
Lightbulb All you will need to resolve your issue

Quote:
Originally Posted by aravindts View Post
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Nifty shell tricks for new UNIX users LXer Syndicated Linux News 1 02-22-2007 07:22 PM
unix shell script cxy0481 Programming 9 11-20-2005 08:15 AM
telnet windows m/c from unix shell script ann124 Programming 1 11-10-2004 01:26 PM
shell script switching user question compupurdy Linux - Newbie 1 11-22-2003 09:03 AM
switching users in a script NGraphiX Linux - General 7 09-25-2002 04:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration