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

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

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

sudo: no tty present and no askpass program specified

第一次执行sudo命令时。

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

还有其他解决办法吗?


当前回答

使用管道:

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

这是所有。

我希望这对你们有帮助

这招对我很管用:

echo "myuser ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

你的用户是myuser

对于Docker映像,这将是:

RUN echo "myuser ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

如果你碰巧来这里是因为你不能在Windows10自带的Ubuntu中sudo

在Windows中编辑/etc/hosts文件(使用记事本),它将位于:%localappdata\lxss\rootfs\etc,添加127.0.0.1 WINDOWS8,这将消除无法找到主机的第一个错误。 要消除no tty present错误,请始终执行sudo -S <命令>

没有人告诉会导致这个错误的原因,如果从一个主机迁移到另一个主机,请记住在sudoers文件中检查主机名:

这是我的/etc/sudoers配置

User_Alias      POWERUSER = user_name
Cmnd_Alias SKILL = /root/bin/sudo_auth_wrapper.sh
POWERUSER hostname=(root:root) NOPASSWD: SKILL

如果不匹配

uname -a
Linux other_hostname 3.10.17 #1 SMP Wed Oct 23 16:28:33 CDT 2013 x86_64 Intel(R) Core(TM) i3-4130T CPU @ 2.90GHz GenuineIntel GNU/Linux

它将弹出以下错误:

没有tty present,也没有指定askpass程序

试试这个:

echo '' | sudo -S my_command