How to run script as another user without password? How to run script as another user without password? bash bash

How to run script as another user without password?


try running:

su -c "Your command right here" -s /bin/sh username

This will run the command as username given that you have permissions to sudo as that user.


Call visudo and add this:

user1 ALL=(user2) NOPASSWD: /home/user2/bin/test.sh

The command paths must be absolute! Then call sudo -u user2 /home/user2/bin/test.sh from a user1 shell. Done.


`su -c "Your command right here" -s /bin/sh username`

The above command is correct, but on Red Hat if selinux is enforcing it will not allow cron to execute scripts as another user. example; execl: couldn't exec /bin/shexecl: Permission denied

I had to install setroubleshoot and setools and run the following to allow it:

yum install setroubleshoot setoolssealert -a /var/log/audit/audit.loggrep crond /var/log/audit/audit.log | audit2allow -M mypolsemodule -i mypol.p