ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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.
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.
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 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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.