限制使用`sudo -s`

我正在一些Linux服务器上设置Nagios,并遇到了一些问题。 check_ide_smart插件需要root权限才能运行系统。 要运行它,我使用check_by_ssh插件ssh到远程主机上的nagios帐户,然后使用sudo运行check_ide_smart

我最初/etc/sudoers添加到/etc/sudoers以允许程序工作:

 nagios ALL=NOPASSWD: /usr/lib/nagios/plugins/check_ide_smart 

虽然在本地运行时工作得很好,但是从Nagios运行时遇到问题:没有生成TTY,导致插件无法工作。

我在sudo的man页面中挖了一下,发现了-s选项,它生成一个shell并在那里执行程序。 当我尝试使用sudo -s ,由于-s显然将命令更改为/bin/bash -c /usr/lib/nagios/plugins/check_ide_smart ,这是sudoers文件不允许的,所以我遇到了权限问题。 我尝试改变sudoers文件来使用这个命令,但是这不起作用,并且使用引号是一个语法错误。

我最终通过在/etc/sudoers使用以下行来完成工作:

 nagios ALL=/bin/bash 

这对我来说真的是错误的,因为我允许nagios用户产生一个root shell,他们可以做任何事情。

在这一点上,我虽然也许,通过将命令放在一个shell脚本,nagios用户具有只读权限将工作,所以我创build了一个shell脚本:

 #!/bin/sh /bin/bash -c /usr/lib/nagios/plugins/check_ide_plugin $@ 

不幸的是,我永远不能得到传递的参数( $@ )正确地使用插件,所以我不知道这是否会工作。 编辑:我需要引用$@它的工作。 感谢@derobert和@pjz。 我仍然不知道这是否会工作,因为我使用@Mike Arthur的解决scheme来工作。

有没有一种方法可以让sudo -s在不允许生成根shell的情况下工作?

回答:

将以下行添加到/etc/sudoers

 nagios ALL=NOPASSWD: /bin/bash -c /usr/lib/nagios/plugins/check_ide_smart * 

注意尾部星号; 没有它,这是行不通的。 感谢@Mike亚瑟的答案。

nagios ALL=NOPASSWD: /bin/bash -c /usr/lib/nagios/plugins/check_ide_smart *

这应该工作,并允许参数。

仅供参考,您需要在shell脚本中引用$ @才能正常工作:

 #!/bin/sh /bin/bash -c /usr/lib/nagios/plugins/check_ide_plugin "$@" 

$@是魔法。 从bash的manpage中,

@从一个开始扩展到位置参数。 当扩展出现在双引号内时,每个参数将扩展为一个单独的单词。 也就是说,“$ @”相当于“$ 1”“$ 2”…如果双引号扩展出现在一个单词内,则第一个参数的扩展与原始单词的开头部分相连,最后一个参数的最后一部分与原始单词连接。 当没有位置参数时,“$ @”和$ @展开为空(即,它们被移除)。

另外,开始bash不会产生一个pty; 虽然我很困惑,为什么你的nagios插件需要一个terminal运行。 它不应该。 也许真正的问题是sudo的环境卫生?

而不是使用sudo -s并启动一个root shell,只需让你的nagios用户使用sudo而不用tty使用!requiretty 。 你的/etc/sudoers应该有以下内容:

 # Allow Nagios extra privs Defaults:nagios !requiretty nagios ALL=NOPASSWD: /usr/lib/nagios/plugins/check_ide_plugin 

…这将允许直接sudo访问,没有密码,没有tty。 如果你想sudo访问所有的插件,你可以closures“check_ide_plugin”。

我们也使用NRPE,这似乎比check_by_ssh安全些,但它需要更多的设置。 在/ etc / sudoers tho中也是这样,只要将nagios与nrpe交换即可。 🙂

〜汤米

再次尝试脚本,但在你的$ @周围加双引号:

 #!/bin/sh /bin/bash -c /usr/lib/nagios/plugins/check_ide_plugin "$@" 

我不认为有可能不使用-s生成一个根shell(假设你需要root权限)。 -s选项专门用来产生一个shell。 这就是 – 什么意思。 从sudo(8):

 -s [command] The -s (shell) option runs the shell specified by the SHELL environment variable if it is set or the shell as specified in passwd(5). If a command is specified, it is passed to the shell for execution. Otherwise, an interactive shell is executed.