sudoers 限制仅在使用“sudo”时有效,如何限制经常使用?

Jea*_*rtz 1 linux sudo

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

deployer ALL=NOPASSWD: /etc/init.d/nginx reload
Run Code Online (Sandbox Code Playgroud)

如果我以 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.
Run Code Online (Sandbox Code Playgroud)

但是,当我以非 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)
Run Code Online (Sandbox Code Playgroud)

这是预期的行为吗?如果是这样,为什么会发生这种情况?我希望sudoers在运行命令之前进行验证。sudoers 规则是否仅适用于作为 运行的命令sudo?(有道理,鉴于它的名字)。

如果是这种情况,是否还有其他方法可以在不使用的情况下限制对命令的常规访问sudo

基本上我希望我的部署者用户只做两件事:

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

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

use*_*517 9

这是预期的行为,仅当您运行 sudo 时才会查​​阅 sudoers 文件,因此它拒绝用户访问该命令并为您提供相关的错误消息。

当用户尝试直接运行命令时,不涉及 sudo,因此您会收到由于用户缺乏权限而导致单个命令失败所产生的错误消息。

如果您想将用户限制在一项活动中,那么您可以允许他们通过 ssh 登录并使用以下命令将他们的密钥绑定到单个命令

command="/usr/bin/sudo /etc/init.d/nginx reload" ssh-rsa AAAAB3NzaC1...
Run Code Online (Sandbox Code Playgroud)

使用此配置,当用户登录时,将执行重新加载并注销。

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