Launch process as another user/group (in init.d script)

initprivilegesprocess

I am editing a init.d script. The init.d script runs a utility script which then runs an process. From either bash scripts how would I make it launch the main process as a specific user and group?

Best Answer

The simplest way is to use the su(1) command, it has an option that allows you to run a command via the user's shell, example:

su foo -c ls

This will switch to the user foo and run the ls command. If the user you want to use does not have a valid shell (ie it's not in /etc/shells, like /bin/false or /sbin/nologin) you will also have to specify a shell on the command line. Example with output:

# su nobody -s /bin/bash -c id
uid=99(nobody) gid=99(nobody) groups=99(nobody) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023