How to become non-root in the middle of a shell script run as root? Possible?

Say I have a shell script that runs a bunch of other sub-scripts. I decided that my script should be run as root, because the first few sub-scripts that it runs should be run as root. However, there are two sub-scripts executed by a super-script that cannot be executed as root. Assuming my script was run as root, how do I cancel it for the last two sub-scripts? Is it possible?

+3
source share
2 answers

You need a specific non-root user that sub-scripts can work with. We call this user fred. Then your root privilege script can just do

su fred /path/to/subscript-A
su fred /path/to/subscript-B

The answer of Contra nsayer, you probably can’t use nobodyfor this, because the whole point nobodyis that it has write rights to nothing. Sometimes this is exactly what you want, but I'm sure your sub-scripts should be written to the file system ...

+4
source

use suto run the command that you want to run as any other user.

su nobody ls /tmp
+1
source

Source: https://habr.com/ru/post/1754669/


All Articles