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

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

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

sudo: no tty present and no askpass program specified

第一次执行sudo命令时。

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

还有其他解决办法吗?


当前回答

命令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.

当您试图从一些非shell脚本运行终端命令(需要root密码)时,也可能出现此错误,例如从Ruby程序运行sudo ls(反引号)。在这种情况下,您可以使用Expect实用程序(http://en.wikipedia.org/wiki/Expect)或其替代方案。 例如,在Ruby中执行sudo ls而没有得到sudo: no tty present,也没有指定askpass程序,你可以这样运行:

require 'ruby_expect'

exp = RubyExpect::Expect.spawn('sudo ls', :debug => true)
exp.procedure do
    each do
        expect "[sudo] password for _your_username_:" do
            send _your_password_
        end
    end
end

[这使用了Expect TCL扩展的一个替代方案:ruby_expect gem]。

我不确定这是否是最近的更改,但我刚刚遇到了这个问题,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 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"