Bash – change user within bash script

bashpermissionsscripting

I have a simple bash script which runs great as a regular user. When launched from postfix, it runs as nobody:nobody which prevents many commands from working.

Is it possible for the script to change to another user account while running, to allow these other programs to run?

Thanks

Best Answer

You will need to configure sudo so that you can run a specific command as nobody which will run as another user.

Typically, something like (in your sudoers file):

nobody ALL=(userTheScriptShouldRunAs) NOPASSWD: /home/userTheScriptShouldRunAs/test.sh

Then, get your script to execute:

sudo -u userTheScriptShouldRunAs /home/userTheScriptShouldRunAs/test.sh