Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 11-05-2003, 03:50 PM   #1
Registered: Sep 2003
Location: Winnipeg, MB, CA
Distribution: LinuxMint
Posts: 101

Rep: Reputation: 15
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.
Old 11-05-2003, 03:56 PM   #2
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 66
You can use:
su - x -c "/path/to/"

This runs the command as the user then returns to the previous user.
Old 11-05-2003, 03:59 PM   #3
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Rep: Reputation: 31

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
Old 11-05-2003, 05:03 PM   #4
Registered: Sep 2003
Location: Winnipeg, MB, CA
Distribution: LinuxMint
Posts: 101

Original Poster
Rep: Reputation: 15
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.
Old 11-05-2003, 05:35 PM   #5
Registered: Jun 2003
Location: Long Island, NY
Distribution: Gentoo Unstable (what a misnomer)
Posts: 380

Rep: Reputation: 30
I think linux ignores the setuid bit on scripts
it is a horrible security risk.
Old 11-05-2003, 05:57 PM   #6
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

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



Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Whats the command that allow you to run a script as another user? ziggie216 Linux - Software 3 11-10-2005 03:05 AM
Automaticly run script when user logs in? bigdog0007 Linux - Security 3 06-23-2005 03:14 PM
How to run a Script as root, after Login as User, in GDM MHOOO Red Hat 14 03-08-2005 08:41 AM
how do I make sure that the user that is going to run the script is a root user??? nikold01 Linux - General 3 09-10-2004 07:54 AM
Allow user to run their own cgi script samwong Linux - Newbie 5 05-25-2004 10:22 PM > Forums > Non-*NIX Forums > Programming

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

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