Bash tips, tricks and code snippets

Running a script as another user

Based on this tip, the following setup allows for user user1 to run a script as user2.

First using visudo,

sudo visudo

add the following line, making sure the path to the script is absolute:

user1 ALL=(user2) NOPASSWD: /absolute/path/to/script.sh

Then the script is called from a user1 shell like this:

sudo -u user2 /absolute/path/to/script.sh