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

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

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

sudo: no tty present and no askpass program specified

第一次执行sudo命令时。

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

还有其他解决办法吗?


当前回答

Try:

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

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

其他回答

试试这个:

echo '' | sudo -S my_command

允许用户使用该命令而不提示输入密码应该可以解决这个问题。首先打开一个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——并在出现任何问题的第一个迹象时不保存就退出。健康警告:损坏此文件将导致严重后果,请小心编辑!

运行包含来自jenkins的sudo命令的shell脚本可能无法按预期运行。要解决这个问题,请遵循以下步骤

简单的步骤:

在基于ubuntu的系统中,运行" $ sudo visudo " 这将打开/etc/sudoers文件。 如果jenkins用户已经在该文件中,那么修改如下:

詹金斯

保存文件 重新开始你的jenkins工作 你不应该再看到错误信息:)

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

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