How to run script as another user without password?

柔情痞子 提交于 2019-12-27 10:30:02

问题


I have script.sh that must be run as user2. However, this script can only be run under user1 in my application.

I would like the following command to run:

su user2 -C script.sh

but be able to run without password.

I also want this to be very restrictive, as in user1 can only run script.sh under user2 and nothing else.

I've tried doing this with sudoers file and just got endlessly confused after hours of trying.

If somebody can provide an explicit example of how this can be accomplished (instead of something generic like use sudoers), it would be greatly appreciated.


回答1:


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.




回答2:


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.




回答3:


`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/sh execl: Permission denied

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

yum install setroubleshoot setools
sealert -a /var/log/audit/audit.log
grep crond /var/log/audit/audit.log | audit2allow -M mypol
semodule -i mypol.p


来源:https://stackoverflow.com/questions/6905697/how-to-run-script-as-another-user-without-password

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!