Run script as another user on Linux
bash, linux, shell
Solution
The answer is change from su to sudo.
`su` is primarily for switching users, while `sudo` is for executing commands as other users. The `-u` flag lets you specify which user to execute the command as:
sudo -u wayne '/home/wayne/script2.sh'
gives `Sorry user is not allowed to execute`
Problem
I am trying to create a Linux terminal menu by way of a simple script. Within the script it will have some commands that will submit a job (a shell script for example) as another user without password prompt. I was able to find the following post and this worked. how to run script as another user without password However, there was one side affect. It appears the user can run other scripts in that users folder which I don't want. Any suggestions/help welcome. For the sake of this. Here is what I have: Username `temp1`, which is the user that will be running the menu. uid=1001(temp1), gid=1001(temp1), groups=1001(temp1) Username `wayne`, which is the user that the script must be submitted as to run the job uid=1000(wayne), gid=1000(wayne),groups=1000(wayne),4(adm),24(cdrom),27(sudo),30(dip)... Script `script1.sh`, `script2.sh` owned by `wayne`. ``` -rwxr-xr-x script1.sh -rwxr-xr-x script2.sh ``` If I try to go to `/home/wayne` as `temp1` user I get permission denied (expected) I set the scripts to chmod 700 for `wayne`. So technically no one can run them other than `wayne`. I have edited sudo file and have the following entry: ``` temp1 ALL(wayne) NOPASSWD: /home/wayne/script1.sh ``` When I run command `su -c "/home/wayne/script1.sh" -s /bin/sh wayne` the script runs (as expected) When I run command `su -c "/home/wayne/script2.sh" -s /bin/sh wayne` the script runs (not expected). Any ideas?