How to become non-root in the middle of a shell script run as root? Possible?
Let's say I have a shell script that runs a bunch of other sub-scripts. I decide that my script must be run as root because the first several sub-scripts that it runs need to be run as root. Ho开发者_StackOverflow中文版wever, there are two sub-scripts run by the super-script that cannot be run as root. Assuming my script was run as root, how do I de-root it for the last two sub-scripts? Is this even possible?
You need a specific non-root user that your sub-scripts can run under. Let us call that user fred
. Then your script with root privileges can simply do
su fred /path/to/subscript-A
su fred /path/to/subscript-B
Contra nsayer's answer, you probably can NOT use nobody
for this, because the entire point of nobody
is that it has write privileges on nothing. Sometimes that's exactly what you want, but I'm betting your sub-scripts need to write to the file system...
use su
to run a command you want to run as some other user.
su nobody ls /tmp
精彩评论