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 acer asus bios bsod computer crash driver drivers error ethernet excel freeze gaming google gpu hard drive hardware hdmi internet laptop malware memory missing monitor motherboard network operating system printer problem ram registry router slow software sound trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > Linux and Unix >
switching user ids in a unix script

Reply  
Thread Tools
cgjoker's Avatar
Member with 205 posts.
 
Join Date: Aug 2003
27-May-2005, 12:09 PM #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
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
27-May-2005, 08: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, 08: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, 01: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
Member with 205 posts.
 
Join Date: Aug 2003
30-May-2005, 12:14 PM #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
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
30-May-2005, 07: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
31-May-2005, 12:11 AM #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
Member with 205 posts.
 
Join Date: Aug 2003
31-May-2005, 10: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, 01: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.
Reply

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.

Search Tech Support Guy

Find the solution to your
computer problem!




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



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 09:35 AM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.