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?



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
| improve this answer | |
  • I think this is the only way...I don't like to sudo a script callable from a mail message but no option. Thanks – TSG Feb 17 '13 at 5:04

You can set a program's permissions with the setuid bit:

chown usertorunas filename

chmod ug+s filename

That will make it run as the user who owns the file.

However, as @Jay reminded me, it doesn't work for scripts. To run a script as a different user you need to make a small program as explained here which CAN be setuid, and calls your script.

| improve this answer | |
  • You can't set setuid bits on scripts, too much of a security risk. – Jay Feb 16 '13 at 22:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.