Runuser

From Notes_Wiki
Revision as of 05:43, 12 March 2013 by Saurabh (talk | contribs) (Created page with "<yambe:breadcrumb>Shell_scripting|Shell scripting</yambe:breadcrumb> =runuser= A normal user can run commands with root privileges with help of sudo. Similarly if a root use...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

<yambe:breadcrumb>Shell_scripting|Shell scripting</yambe:breadcrumb>

runuser

A normal user can run commands with root privileges with help of sudo. Similarly if a root user needs to run commands as a normal user it can use 'runuser' as follows:

runuser -l redmine -c "cd; touch a.txt"

This would cause creation of fine a.txt in user redmines home folder with user owner and group owner as redmine and redmines primary group.

Similarly

runuser -l redmine -c "sleep 100"

causes sleep command to be executed with user redmine's privileges.

To just get a redmine users shell one can use both runuser or su as

runuesr -l redmine  #OR
su - redmine

Note that strangely the output of

runuser -l redmine -c "cd ~; echo $USER; echo $PWD"

seems to indicate commands running as root user, which is very weird. Explanation for this strange behavior is not yet known.

This strangeness is present even while using su to run commands as non-root user. That is although

su - redmine -c "sleep 100"

runs sleep as redmine user. The following command

su - redmine -c "cd ~; echo $USER; echo $PWD"

runs echo with root user privileges.


Some steps learned from http://www.cyberciti.biz/open-source/command-line-hacks/linux-run-command-as-different-user/

<yambe:breadcrumb>Shell_scripting|Shell scripting</yambe:breadcrumb>