LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Run a script as a different user (https://www.linuxquestions.org/questions/programming-9/run-a-script-as-a-different-user-112795/)

dtheorem 11-05-2003 03:50 PM

Run a script as a different user
 
I am writing a bash script. I run it as root, and inside the script I create a user x and
"su - x". I then want to continue running the script as the user x. This seemed impossible as the script stopped until I exited back to root, and then it continued. I've figured out a way around this by creating a .bash_profile for x before I "su - x" that tells it to run automatically a script that I create beforehand. If I haven't lost you yet, then you're nuts. In essence, I would like to do one of the following:

1) Run a script as user and then have it automatically exit back to root. Something like "Run scipt as x", and then when it's done, I'm root again.

OR (by solving the following problem, I have figured a way around the first one)

2) If I'm currently user x, is there a command I can type that will guarantee exiting back out to root? I've tried to put "exit" at the end of my script, but it doesn't seem to run it. Similarly with logout. I currently have a script running as root, so if I put "su - root" at the end of the script I'm running as x, will it continue the original root script, or will this new "root environment" be different from the original one?

If you can help me, that would be great. If you're wondering what the heck I'm trying to do, I'll tell you. I'm attempting to create an automatic LFS (Linux from Scratch) install script for Knoppix. I guess another, more simple question to ask is this : "Is it necessary to su - lfs to install chapter 4 in knoppix?

If you don't know what LFS is, I'd still appreciate an answer to my problems above. Thank-you in advance.

david_ross 11-05-2003 03:56 PM

You can use:
su - x -c "/path/to/script.sh"

This runs the command as the user then returns to the previous user.

Bebo 11-05-2003 03:59 PM

Hi,

What about setuid? By setting this bit (with chmod) you can make a program run as a specified user. I think, anyway :) But it seems a bit strange to change a file's permissions when it is executed... Maybe you could split your script in two parts?

Hope this helps somewhat :)

dtheorem 11-05-2003 05:03 PM

Oh, believe me, the script is already into 4 parts. I'm not exactly sure what you mean by setuid and chmod. I mean, I know what the commands are, but they don't really solve my problem. Thanks anyway though. I'm going to try "su - x -c "stuff". That seems so simple, I can't believe I didn't think to look at the su man page. Thanks everybody! I'll let you know if it works.

LogicG8 11-05-2003 05:35 PM

Bebo:
I think linux ignores the setuid bit on scripts
it is a horrible security risk.

Bebo 11-05-2003 05:57 PM

Aha, I didn't now that - but it is definitely a security risk :)

Cheers!


All times are GMT -5. The time now is 04:18 PM.