如何在 sudoers 中指定命令参数?作为背景,aws
命令实际上是通向一大堆子系统的网关,我想限制用户只能运行aws s3 cp ...any other args...
当我尝试以下 /etc/sudoers
Cmnd_Alias AWSS3_CMD = /usr/local/bin/aws s3 cp, /usr/local/aws/bin/aws s3 cp
gbt1 ALL=(gbt-ops) NOPASSWD: AWSS3_CMD
Run Code Online (Sandbox Code Playgroud)
不幸的是,shell 提示输入密码
$ sudo -u gbt-ops aws s3 cp helloworld s3://my-bucket/hw.1
gbt-ops's password:
Run Code Online (Sandbox Code Playgroud)
如果我删除 Cmnd_Alias 中的命令参数,那么它会按需要流动(没有密码提示),但授权范围太广。那么,限制仅某些类型的命令调用的正确方法是什么。
Cmnd_Alias AWSS3_CMD = /usr/local/bin/aws, /usr/local/aws/bin/aws
Run Code Online (Sandbox Code Playgroud)
然后
$ sudo -u gbt-ops aws s3 cp helloworld s3://my-bucket/hw.1
...happy
Run Code Online (Sandbox Code Playgroud)
非常感谢。
mur*_*uru 52
您没有使用任何通配符,但提供了两个参数。因此sudo
,完全按照所写的方式查找命令(路径查找除外)(来自man 5 sudoers
):
If a Cmnd has associated command line arguments, then the arguments in
the Cmnd must match exactly those given by the user on the command line
(or match the wildcards if there are any).
Run Code Online (Sandbox Code Playgroud)
尝试类似:
Cmnd_Alias AWSS3_CMD = /usr/local/bin/aws s3 cp *, /usr/local/aws/bin/aws s3 cp *
Run Code Online (Sandbox Code Playgroud)
注意:
Wildcards in command line arguments should be used with care. Because
command line arguments are matched as a single, concatenated string, a
wildcard such as ‘?’ or ‘*’ can match multiple words.
Run Code Online (Sandbox Code Playgroud)
因此,每个命令只需要一个通配符。
Grz*_*cki 12
与@muru 相同,但对于那些喜欢完整工作示例的人:
# Cmnd alias specification
Cmnd_Alias ECHO_CMD=/bin/echo A *,/bin/echo B *
# Make USER run specific commands on given HOSTNAME
# USER_NAME
#userx
userx ALL=(root) NOPASSWD: /sbin/cmd1,/sbin/cmd2,ECHO_CMD
Run Code Online (Sandbox Code Playgroud)
而/sbin/cmd1
,/sbin/cmd2
可以是任何其他命令。
的目的ECHO_CMD
是sudo echo X A
展示会要求密码,而sudo echo A X
不会要求的,并让您通过这样简单的实验获得信心。
(假设 echo 位于/bin/echo
,以检查它在系统上的位置,请尝试whereis echo
)
归档时间: |
|
查看次数: |
78432 次 |
最近记录: |