 | Senior Member with 205 posts. | | | | 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; | | Distinguished Member with 14,983 posts. | | Join Date: Apr 2003 Location: 1265 Lombardi Ave Experience: IIAHYAYCESA,YAADA! | | Hmm, You would have to use the SU command to switch users but I am not sure if you can script the password. | | Senior Member with 1,246 posts. | | Join Date: Sep 2003 Experience: Linux~su | | at least not in a bash shell type script. In others you might be able to but still be tough. | | Senior Member with 1,410 posts. | | | | 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. | | Senior Member with 205 posts. | | | | 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. | | Distinguished Member with 14,983 posts. | | Join Date: Apr 2003 Location: 1265 Lombardi Ave Experience: IIAHYAYCESA,YAADA! | | I think your only option is to use SUDO and setup the sudo config file to not require a password. | | Senior Member with 1,410 posts. | | | | 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. | | Senior Member with 205 posts. | | | | I was thinking of trying something like...
su - user -c "/directory/myscript.sh" | | Senior Member with 1,410 posts. | | | | 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. |  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.
| You Are Using: |
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. | |
|