LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 08-02-2012, 01:21 PM   #1
Brandon9000
Member
 
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111

Rep: Reputation: Disabled
Running a Command as Another User


I need to have a program run as root issue a command as a non-root user. It could be any command whatsoever. I am trying to make sudo do this, but I can't seem to make it behave exactly like the specified user. Testing from the command line:

$ sudo -i -u GWashington env | grep HOME
$ HOME=/home/GWashington

but:

$ sudo -i -u GWashington echo $HOME
/root

How can I use sudo (or anything) to enable root to behave more like the specified user?

Thanks in advance.
 
Old 08-02-2012, 01:34 PM   #2
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,057

Rep: Reputation: Disabled
I would use "su - other_user".
 
Old 08-02-2012, 01:44 PM   #3
Roken
Member
 
Registered: Oct 2011
Location: Warrington, UK
Distribution: Arch local, Debian on VPS, several RPIs.
Posts: 300
Blog Entries: 1

Rep: Reputation: 55
I have a script started as a normal user at boot using:

Code:
/usr/bin/sudo -u user -n -i command
which works just fine.
 
Old 08-02-2012, 01:46 PM   #4
Brandon9000
Member
 
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111

Original Poster
Rep: Reputation: Disabled
Thanks, Didier. I would too, but it is my understanding that su isn't universal or at least that the syntax is distribution dependent, and this has to work on as many distributions as possible, and with the very same syntax. Isn't there a problem with su on Ubuntu?

Last edited by Brandon9000; 08-02-2012 at 01:49 PM.
 
Old 08-02-2012, 01:51 PM   #5
Brandon9000
Member
 
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Roken View Post
I have a script started as a normal user at boot using:

Code:
/usr/bin/sudo -u user -n -i command
which works just fine.
Perhaps it will, but it doesn't seem to receive the user's whole environment, as I show above, and I can count on my users to issue every conceivable command with it. I just wondered if there were something more I could do.
 
Old 08-02-2012, 01:52 PM   #6
Roken
Member
 
Registered: Oct 2011
Location: Warrington, UK
Distribution: Arch local, Debian on VPS, several RPIs.
Posts: 300
Blog Entries: 1

Rep: Reputation: 55
I wonder if your problem is because the root user will launch the user command as a child process, and env is picking up $HOME from the parent process. If you wrap env | grep HOME and HOME= in a bash script, and then launch that using sudo -u does it return the correct result?
 
Old 08-02-2012, 01:54 PM   #7
Brandon9000
Member
 
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Roken View Post
I wonder if your problem is because the root user will launch the user command as a child process, and env is picking up $HOME from the parent process. If you wrap env | grep HOME and HOME= in a bash script, and then launch that using sudo -u does it return the correct result?
I put the command in a script and it still came up with "/root."

Edit - I'm wrong. Doing exactly what you say, it comes up with "/home/GWashington."

Last edited by Brandon9000; 08-02-2012 at 01:55 PM.
 
Old 08-02-2012, 01:59 PM   #8
Roken
Member
 
Registered: Oct 2011
Location: Warrington, UK
Distribution: Arch local, Debian on VPS, several RPIs.
Posts: 300
Blog Entries: 1

Rep: Reputation: 55
Then my work here is done

Glad it worked.
 
Old 08-02-2012, 02:02 PM   #9
Brandon9000
Member
 
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111

Original Poster
Rep: Reputation: Disabled
This is very helpful. Of course, to make this work, my program would have to spit out a temp bash script, run it with sudo -u, and then delete it. That is certainly possible, and there are probably no negative consequences, although it would be nice if I could do it in some way without creating temp files. I hate to seem ungrateful and this will certainly work.
 
Old 08-03-2012, 09:26 AM   #10
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by Brandon9000 View Post
it is my understanding that su isn't universal or at least that the syntax is distribution dependent, and this has to work on as many distributions as possible, and with the very same syntax. Isn't there a problem with su on Ubuntu?
I think su is more universal than sudo: su is in coreutils. Ubuntu doesn't give root a password so you can't su to root, however since you are suing from root this isn't a problem for you.
 
Old 08-03-2012, 09:28 AM   #11
Brandon9000
Member
 
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111

Original Poster
Rep: Reputation: Disabled
Okay. That makes sense.
 
Old 08-06-2012, 04:17 PM   #12
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 08-06-2012, 04:41 PM   #13
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by ntubski View Post
I think su is more universal than sudo: su is in coreutils. Ubuntu doesn't give root a password so you can't su to root, however since you are suing from root this isn't a problem for you.
I agree

su is everywhere, even on embedded systems. sudo is a bit more flaky.
 
  


Reply



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
[SOLVED] Running a single command/script as root user through Apache/PHP nonshatter Ubuntu 1 02-06-2012 12:34 AM
[SOLVED] Config file on user login, running command as root? el_b Debian 2 09-04-2009 03:11 PM
Running a command as another user in a bash script ran as root? camphor Programming 2 03-29-2009 03:11 PM
Running A Single Command In A Script As A Different User Woodsman Slackware 4 06-16-2008 06:08 PM
problem in running the command as a normal user junaid18183 Linux - General 2 06-25-2005 06:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:00 PM.

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