有很多关于如何使sudo不提示input密码的信息(NOPASSWD)。
比方说,我有用户alice ,在我的sudoers这行:
alice ALL=(root) NOPASSWD: /bin/myadmintool
这工作得很好。 用户可以运行sudo /bin/myadmintool并执行一些操作。
但是,如果用户要这样做:
sudo /bin/bash
这是发生了什么事情:
$ sudo /bin/bash We trust you have received the usual lecture from the local System Administrator. It usually boils down to these three things: #1) Respect the privacy of others. #2) Think before you type. #3) With great power comes great responsibility. Password: Sorry, user alice is not allowed to execute '/bin/bash' as root on myhostname.
我想要发生的是sudo直接跳转到:
Sorry, user alice is not allowed to execute '/bin/bash' as root on myhostname.
没有密码提示
我怎样才能在sudoers文件中expression这个,只是为了这个用户?
经过仔细阅读sudoers手册页后 ,我find了一个可行的解决scheme!
我的sudoersconfiguration为alice看起来像这样:
Defaults:alice !authenticate alice ALL=(root) NOPASSWD: /bin/myadmintool
请记住,这将基本上为alice的sudo系列设置NOPASSWD。 就我而言,这就是意图。
authenticate选项在手册中定义为:
authentication
如果设置,用户必须通过密码(或其他authentication方式)进行身份validation,然后才能运行命令。 这个默认值可以通过PASSWD和NOPASSWD标签覆盖。 此标志默认打开。
Defaults选项可以让你定义一个用户,组,主机列表等,如下所示:
Default_Type ::= 'Defaults' | 'Defaults' '@' Host_List | 'Defaults' ':' User_List | 'Defaults' '!' Cmnd_List | 'Defaults' '>' Runas_List
由于authenticate是一个布尔configuration选项,因此可以通过执行!authenticate来取消/设置为false。
作为解决方法,您可以使用非交互式模式:
sudo -n回显testing
sudo:需要密码
您可以为此用户创build别名:
别名sudo ='sudo -n'