如何以没有密码的其他用户身份运行脚本? [英] How to run script as another user without password?

查看:142
本文介绍了如何以没有密码的其他用户身份运行脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有必须以user2身份运行的script.sh.但是,此脚本只能在我的应用程序的user1下运行.

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

我想运行以下命令:

su user2 -C script.sh

但无需密码即可运行.

我也希望这是非常严格的限制,因为在user1中只能在user2下运行script.sh,而不能执行其他操作.

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

我已经尝试过使用sudoers文件进行此操作,但是经过数小时的尝试,它才变得无穷无尽.

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

如果有人可以提供一个明确的示例说明如何实现此目的(而不是像使用sudoers这样的通用方法),将不胜感激.

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.

推荐答案

调用visudo并添加以下内容:

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

命令路径必须是绝对的!然后从user1 shell调用sudo -u user2 /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.

这篇关于如何以没有密码的其他用户身份运行脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆