如何在没有密码的情况下以另一个用户身份运行脚本? [英] How to run script as another user without password?

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

问题描述

我有必须以 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天全站免登陆