sudo-模拟用户


8

我试图正确使用sudo命令和sudoers文件,以便我可以以其他用户身份运行命令。

我的sudoers文件设置如下:

beans ALL = (root,apache) NOPASSWD: /opt/renovations/var/script-*.sh

根,apache和bean是该组bean的一部分。
另外/ opt具有755权限,而/ opt / renovations目录及其子目录归bean用户和组所有。

我试图以bean身份运行的命令是:

sudo -n -u apache -i /opt/renovations/var/script-test.sh  

-n:是,这样就不会提示我输入密码,因为它将由cron运行
-u:是,以便我可以模拟apache用户
-i:是,以便模拟登录,而我的.profile已加载。我需要这样做,以便访问.profile中的环境变量。

问题是,当我运行sudo命令时,收到以下消息:

sudo:对不起,运行sudo需要密码

我试过在AIX和Ubuntu上都运行它,但是问题出在两个系统上。如果我运行,这可以工作:

sudo -n -u apache /opt/renovations/var/script-test.sh

但是,如果没有-i,则我的环境不会包含我需要存在的所有环境变量。

我需要在sudoers文件中进行一些更新以使之成为可能吗?


visudo -c返回什么(检查sudoers是否有错误)。另外,您是否可以确认AIX的完整版本(oslevel -s)和已安装的sudo版本?
EightBitTony

1
此外,如果您以sudo而不是通配符显式列出脚本名称(文件名是符号链接吗?),它是否起作用?
EightBitTony

谢谢@EightBitTone。因此,我找到了解决此sudoers配置问题的解决方案:当使用-i开关时,实际命令为 beans ALL = (root,apache) NOPASSWD: /usr/bin/ksh -c /opt/renovations/var/script-*.shbeans ALL = (root,apache) NOPASSWD: /usr/bin/bash -c /opt/renovations/var/script-*.sh但是现在我得到了新的错误,例如:ksh: /opt/renovations/var/script-test.sh: cannot execute
krzyszto 2014年

分析/ var / log / sudolog日志文件对于调试上述问题(在AIX上)非常有帮助。
krzyszto 2014年

Answers:


4

因此,就像您自己说@krzysto一样,解决方案是将以下内容添加到sudoers文件中

beans ALL = (root,apache) NOPASSWD: /usr/bin/ksh -c /opt/renovations/var/script-*.sh 
beans ALL = (root,apache) NOPASSWD: /usr/bin/bash -c /opt/renovations/var/script-*.sh

缺少的下一部分是确保该组对脚本具有执行权限,以便您可以执行它们。


是的,这是正确的解决方案。ksh错误仅在为调查模拟问题而创建的测试环境中发生;实际上,生产都可以正常工作
krzyszto 2014年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.