sudoers NOPASSWD:sudo:没有tty存在且没有指定askpass程序

时间:2014-12-29 23:08:21

标签: linux bash sudo tty

我添加了这样的用户:

$ adduser --system --home /no/home --no-create-home --group --disabled-password --disabled-login testuser

为群组添加了用户:

$ adduser testuser testgroup

为sudoers添加了行(visudo):

testuser    ALL=(ALL) NOPASSWD: ALL
%testgroup         ALL=(ALL:ALL) NOPASSWD: ALL

当我尝试使用以下内容运行bash脚本时:

#!/bin/sh
sudo -u testuser /usr/bin/php /usr/local/bin/script.php

但是当我运行这个脚本时,我在日志中收到错误:

sudo: no tty present and no askpass program specified

编辑:requiretty不在sudoers文件中。

3 个答案:

答案 0 :(得分:8)

sudo权限是关于您正在从更改的用户/组,而不是您正在将更改为的用户。

这些权限行也允许testuser用户和testgroup组在没有密码的情况下运行任何命令(任何人)。

您需要向运行该脚本的用户授予 权限,以便 testuser用户运行命令

假设你的意思是允许的那样。

答案 1 :(得分:4)

当您的sudoers文件指定requiretty时,会发生该错误。来自sudoers联机帮助页:

   requiretty      If set, sudo will only run when the user is 
                   logged in to a real tty.  When this flag is set, 
                   sudo can only be run from a login session and not 
                   via other means such as cron(8) or cgi-bin scripts.  
                   This flag is off by default.

要解决您的错误,请从您的sudoers文件中删除requiretty

答案 2 :(得分:0)

我通过登录服务器对其进行了修复,并将以下几行添加到ssh-server配置中:

> vim /etc/ssh/sshd_config

Match User <your user name>
    PermitTTY yes

因此,我不需要永久使用-t选项。