我正在尝试使用makefile编译一些源代码。在makefile中,有一堆命令需要作为sudo运行。

当我从终端编译源代码时,一切都很好,并且在第一次运行sudo命令等待密码时,make会暂停。一旦我输入密码,做简历和完成。

但是我希望能够在NetBeans中编译源代码。所以,我开始了一个项目,并向netbeans展示了在哪里可以找到源代码,但当我编译项目时,它给出了错误:

sudo: no tty present and no askpass program specified

第一次执行sudo命令时。

我在网上查了这个问题,我找到的所有解决方案都指向一件事:禁用这个用户的密码。因为这里讨论的用户是根用户。我不想那样做。

还有其他解决办法吗?


当前回答

作为参考,以防其他人遇到同样的问题,我在一个小时内被这个错误卡住了,因为我使用的是NOPASSWD参数,所以不应该发生这个错误。

我不知道的是,当没有tty并且用户尝试启动的命令不在/etc/sudoers文件中允许的命令的一部分时,sudo可能会引发完全相同的错误消息。

这里有一个简化的例子,我的文件内容与我的问题:

bguser ALL = NOPASSWD: \
    command_a arg_a, \
    command_b arg_b \
    command_c arg_c

当bguser尝试在没有任何tty的情况下启动“sudo command_b arg_b”(bguser被用于某些守护进程)时,他将遇到错误“no tty present and no askpass program specified”。

Why?

因为在/etc/sudoers文件的末尾缺少一个逗号…

(我甚至怀疑这是否是预期的行为,而不是sudo中的错误,因为这种情况下正确的错误消息应该是“对不起,用户bguser不允许执行等”)

其他回答

我想我能帮到别人。

首先,我修改了/etc/sudoers中的用户设置,参考上面的答案。但它仍然没有工作。

myuser   ALL=(ALL) NOPASSWD: ALL
%mygroup  ALL=(ALL:ALL) ALL

在我的例子中,myuser在mygroup中。

我不需要小组。删除了这一行。

(不应该像我一样删除这一行,只是标记注释。)

myuser   ALL=(ALL) NOPASSWD: ALL

它的工作原理!

命令sudo失败,因为它试图提示输入root密码,并且没有分配伪tty(因为它是脚本的一部分)。

您需要以root身份登录运行此命令,或者在/etc/sudoers中设置以下规则 (或:sudo visudo):

# Members of the admin group may gain root privileges.
%admin  ALL=(ALL) NOPASSWD:ALL

然后确保您的用户属于admin组(或wheel)。

理想情况下(更安全),将根权限限制为只允许指定为%admin ALL=(ALL) NOPASSWD:/path/to/program的特定命令

其他选项,不基于NOPASSWD:

Start Netbeans with root privilege ((sudo netbeans) or similar) which will presumably fork the build process with root and thus sudo will automatically succeed. Make the operations you need to do suexec -- make them owned by root, and set mode to 4755. (This will of course let any user on the machine run them.) That way, they don't need sudo at all. Creating virtual hard disk files with bootsectors shouldn't need sudo at all. Files are just files, and bootsectors are just data. Even the virtual machine shouldn't necessarily need root, unless you do advanced device forwarding.

Try:

ssh -t remotehost "sudo <cmd>"

这将消除上述错误。

当我试图挂载sshfs时,我有同样的错误消息,需要sudo:命令是这样的:

sshfs -o sftp_server="/usr/bin/sudo /usr/lib/openssh/sftp-server" user@my.server.tld:/var/www /mnt/sshfs/www

通过添加选项-o debug

sshfs -o debug -o sftp_server="/usr/bin/sudo /usr/lib/openssh/sftp-server" user@my.server.tld:/var/www /mnt/sshfs/www

对于这个问题,我也有同样的意思:

sudo: no tty present and no askpass program specified

所以通过阅读其他人的答案,我开始在/etc/sudoer中创建一个文件D /user在my.server.tld

user ALL=NOPASSWD: /usr/lib/openssh/sftp-server

现在我可以挂载驱动器,而不给我的用户太多额外的权利。