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

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

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

sudo: no tty present and no askpass program specified

第一次执行sudo命令时。

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

还有其他解决办法吗?


当前回答

Try:

ssh -t remotehost "sudo <cmd>"

这将消除上述错误。

其他回答

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

命令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的特定命令

当您试图从一些非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]。

詹金斯:

echo '<your-password>' | sudo -S command

Eg:-

echo '******' | sudo -S service nginx restart

你可以使用掩码密码插件来隐藏你的密码

Try:

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

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