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

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

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

sudo: no tty present and no askpass program specified

第一次执行sudo命令时。

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

还有其他解决办法吗?


当前回答

1 打开 /etc/sudoers

输入sudo vi /etc/sudoers.这将以编辑模式打开您的文件。

2添加/修改linux用户

查找Linux用户的条目。如果找到,则按以下方式修改或添加新行。

<USERNAME> ALL=(ALL) NOPASSWD: ALL

3保存并退出编辑模式

其他回答

虽然这个问题很老了,但它仍然与我的最新系统相关。在启用sudo的调试模式(调试sudo /var/log/sudo_debug all@info in /etc/sudo.conf)后,我被指向/dev: "/dev is world writable"。因此,您可能需要检查tty文件的权限,特别是tty/pts节点所在目录的权限。

如果你将这一行添加到你的/etc/sudoers(通过visudo),它将解决这个问题,而不必禁用输入密码,当sudo的别名-S不起作用时(脚本调用sudo):

Defaults visiblepw

当然,自己阅读手册来理解它,但我认为对于我通过lxc exec实例—/bin/bash在LXD容器中运行的用例来说,它非常安全,因为它不会通过网络打印密码。

没有人告诉会导致这个错误的原因,如果从一个主机迁移到另一个主机,请记住在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程序

默认情况下,Sudo将从连接的终端读取密码。您的问题是,从netbeans控制台运行时没有附加终端。因此,您必须使用另一种方法来输入密码:即所谓的askpass程序。

askpass程序不是一个特定的程序,而是任何可以要求密码的程序。例如,在我的系统中,x11-ssh-askpass工作正常。

为了做到这一点,你必须指定要使用的程序,可以使用环境变量SUDO_ASKPASS,也可以在sudo.conf文件中指定(详情请参阅man sudo)。

您可以使用选项-A强制sudo使用askpass程序。默认情况下,它只在没有附加终端时使用它。

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