Mourning the loss of our friend, WhitPhil.
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
 
Linux and Unix
Tag Cloud
access audio blue screen boot bsod connection crash dell desktop driver drivers dvd email error excel firefox hard drive hardware hdmi hijackthis internet keyboard laptop malware monitor motherboard network networking outlook problem ram recovery router screen slow sound spyware tdlwsp.dll trojan upgrade vba video virus vista vundo windows windows 7 windows vista windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > Linux and Unix >
switching user ids in a unix script

Tip: Click here to scan for System Errors and Optimize PC performance
[ Sponsored Link ]

Closed Thread
 
Thread Tools
cgjoker's Avatar
Senior Member with 205 posts.
 
Join Date: Aug 2003
27-May-2005, 11:09 AM #1
switching user ids in a unix script
How would I go about switching user ids in a script.

Reason for this would be that I have one process that is using one particular id, (say id1), and I would like it to run a script which would switch all following scripts to be run under another id, (say id2).

So, id1 runs a script which would switch to id2 so that all concurrent scripts run under id2.

Any ideas?

I thought this would do it but not quite.

#!/bin/ksh
echo "hello"
pwd
login id1
password1
pwd
touch newfile.dat
exit;
Squashman's Avatar
Distinguished Member with 14,983 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: IIAHYAYCESA,YAADA!
27-May-2005, 07:17 PM #2
Hmm, You would have to use the SU command to switch users but I am not sure if you can script the password.
tsunam's Avatar
Senior Member with 1,246 posts.
 
Join Date: Sep 2003
Experience: Linux~su
27-May-2005, 07:42 PM #3
at least not in a bash shell type script. In others you might be able to but still be tough.
codejockey's Avatar
Senior Member with 1,410 posts.
 
Join Date: Feb 2002
29-May-2005, 12:14 AM #4
Quote:
Hmm, You would have to use the SU command to switch users but I am not sure if you can script the password.
The Squashman has the right idea. The su command (used to become a different user) requires that when you invoke it, you are running from a terminal. Attempting to use a construct such as: echo password | su root will not work, because the su command checks to see if input is coming from a terminal (and input coming from a pipe does not qualify).

You don't say why you have two different IDs involved here (apparently, one ID updates a file used by another ID); the problem might be solved simply by changing the file permissions.

Hope this helps.
__________________
The slowest component still sits at the keyboard.
cgjoker's Avatar
Senior Member with 205 posts.
 
Join Date: Aug 2003
30-May-2005, 11:14 AM #5
The idea is that we'd have an external production id which would initially launch a script for us, but we'd want to switch the script process flow to our own production id.

From the sounds of your guys comments, it doesn't sound like it can be done. Ill keep looking into this though.

Thanks.
Squashman's Avatar
Distinguished Member with 14,983 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: IIAHYAYCESA,YAADA!
30-May-2005, 06:47 PM #6
I think your only option is to use SUDO and setup the sudo config file to not require a password.
codejockey's Avatar
Senior Member with 1,410 posts.
 
Join Date: Feb 2002
30-May-2005, 11:11 PM #7
I believe you could also do this fairly simply in a C program (which also makes me suspect that perl would support this as well).

Hope this helps.
cgjoker's Avatar
Senior Member with 205 posts.
 
Join Date: Aug 2003
31-May-2005, 09:32 AM #8
I was thinking of trying something like...

su - user -c "/directory/myscript.sh"
codejockey's Avatar
Senior Member with 1,410 posts.
 
Join Date: Feb 2002
01-Jun-2005, 12:39 AM #9
I suspect this would work if you were root when you started the script, since root can su to any ID without authentication (i.e., no prompt for password) -- but I haven't tested that suspicion, since I don't have access to my Linux system at the moment. However, if you were not root when you started the script, the su command will check to see if the process invoking it is associated with a terminal. Since you are running the script in background (most likely), you have dissociated yourself from a terminal (which is why cntl-C, cntl-pipe, etc. have no effect on background processes), and su will stop before you can complete the switch to another ID. Also, how were you expecting su to authenticate the attempt to switch to another user? Your code doesn't seem to provide the password for "user" anywhere ...

You definitely have the right idea, however. Unfortunately, you're running into some of the security inherent in Unix/Linux which is making things difficult.

Finally, I still wonder if some configuration tweaking wouldn't do the job for you. For example, the production ID starts your script and executes until it's time to change to the other id. Assuming permissions were set correctly, you could have the script source the .login (or similar) file for the second ID, thus re-setting the environment. The script could continue to execute without needing to do an su command at all. Is this what you have in mind?

Hope this helps -- additional info appreciated.
__________________
The slowest component still sits at the keyboard.
Closed Thread Bookmark and Share

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Smart Search

Find your solution!



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.

Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 01:13 AM.
Copyright © 1996 - 2009 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.
Powered by Cermak Technologies, Inc.