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

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

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

sudo: no tty present and no askpass program specified

第一次执行sudo命令时。

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

还有其他解决办法吗?


当前回答

其他选项,不基于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:

对所有命令使用NOPASSWD行,我的意思是: NOPASSWD:所有 将这一行放在sudoers文件中的所有其他行之后。

这对我来说很有效(Ubuntu 14.04)。

作为参考,以防其他人遇到同样的问题,我在一个小时内被这个错误卡住了,因为我使用的是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不允许执行等”)

允许用户使用该命令而不提示输入密码应该可以解决这个问题。首先打开一个shell控制台,输入:

sudo visudo

然后编辑该文件添加到最后:

username ALL = NOPASSWD: /fullpath/to/command, /fullpath/to/othercommand

eg

john ALL = NOPASSWD: /sbin/poweroff, /sbin/start, /sbin/stop

将允许用户约翰sudo关机,启动和停止没有提示密码。

查看屏幕底部你需要在visudo中使用的击键——顺便说一下,这不是vi——并在出现任何问题的第一个迹象时不保存就退出。健康警告:损坏此文件将导致严重后果,请小心编辑!

使用管道:

echo your_pswd | sudo -S your_cmd

使用here文档:

sudo -S cmd <<eof
pwd
eof
#remember to put the above two lines without "any" indentations.

打开终端询问密码(以任何可行的方式):

gnome-terminal -e "sudo cmd"
xterm -e "sudo cmd"

我在Ubuntu 20.04服务器上工作时遇到了这个问题。

我试图从远程机器运行sudo命令,将应用程序部署到服务器。然而,当我运行这个命令时,我得到了错误:

sudo: no tty present and no askpass program specified
The remote script failed with exit code 1

以下是我的解决方法:

该问题是由执行试图请求密码的sudo命令引起的,但sudo没有访问tty来提示用户输入密码短语。由于无法找到tty, sudo就会退回到askpass方法,但无法找到已配置的askpass命令,因此sudo命令会失败。

要解决这个问题,您需要能够在不要求密码的情况下为特定用户运行sudo。无密码要求在/etc/sudoers文件中配置。要配置它,运行下面的命令:

sudo nano /etc/sudoers

OR

sudo visudo

注意:这将使用默认编辑器打开/etc/sudoers文件。

接下来,在文件底部添加以下一行:

# Allow members to run all commands without a password
my_user ALL=(ALL) NOPASSWD:ALL

注意:将my_user替换为您的实际用户

如果您希望用户运行特定的命令,您可以指定它们

# Allow members to run specific commands without a password
my_user ALL=(ALL) NOPASSWD:/bin/myCommand

OR

# Allow members to run specific commands without a password
my_user ALL=(ALL) NOPASSWD: /bin/myCommand, /bin/myCommand, /bin/myCommand

保存更改并退出文件。

有关更多帮助,请阅读此链接中的资源:sudo: no tty present and no askpass program specified

这是所有。

我希望这对你们有帮助