sudoers限制只有在使用“sudo”时才起作用,如何限制正常使用?

我在sudoers.d/nginx文件中有以下内容:

 deployer ALL=NOPASSWD: /etc/init.d/nginx reload 

这工作,如果我运行命令为sudo:

 $ sudo /etc/init.d/nginx start [sudo] password for deployer: Sorry, user deployer is not allowed to execute '/etc/init.d/nginx start' as root on graduation. 

但是,当我作为非sudo运行该命令时,似乎sudoers得到通过。 该命令仍然不起作用,但这是由于其他所有权的错误,而不是在sudoers的限制:

 $ /etc/init.d/nginx start nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied) 2012/12/30 20:59:49 [warn] 30054#0: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:12 2012/12/30 20:59:49 [emerg] 30054#0: open() "/var/log/nginx/access.log" failed (13: Permission denied) 

这是预期的行为? 如果是这样,为什么会这样呢? 我希望在运行命令之前进行sudoersvalidation。 sudoers规则只适用于以sudo运行的sudo吗? (有道理,因为它的名字)。

如果是这样的话,有没有其他方法来限制对命令的正常访问而不使用sudo

基本上我想我的部署用户只做两件事:

  • 拥有它的主目录/var/www
  • 能够执行/etc/init.d/nginx reload

所有其他的事情应该是禁止的(理想情况下甚至在主目录以外的cd)

这是预期的行为,sudoers文件只在运行sudo时被查阅,所以它拒绝用户访问命令并给出相关的错误信息。

当用户试图直接运行命令时,不会涉及sudo,因此用户缺less特权会导致单个命令产生的错误消息失败。

如果你想限制用户只有一个活动,那么你可以允许他们通过SSHlogin,并通过使用他们的键到单一的命令

 command="/usr/bin/sudo /etc/init.d/nginx reload" ssh-rsa AAAAB3NzaC1... 

有了这个configuration,当用户login然后重新加载被执行,他们被注销。

如果在以后的日子,您决定需要用户执行其他操作,那么您可以扩展这个想法 。