Steven
Steven

Reputation: 31

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. However, 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?

Upvotes: 3

Views: 1107

Answers (2)

zwol
zwol

Reputation: 140569

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...

Upvotes: 4

nsayer
nsayer

Reputation: 17047

use su to run a command you want to run as some other user.

su nobody ls /tmp

Upvotes: 1

Related Questions