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

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

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

sudo: no tty present and no askpass program specified

第一次执行sudo命令时。

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

还有其他解决办法吗?


当前回答

这个问题的解决办法是

如果您在Jenkins实例之外的任何地方遇到了这个问题,请从第二步开始。第一步是针对遇到Jenkins实例问题的用户。

转到谷歌云控制台的Jenkins实例。 输入命令 sudo苏

Visudo -f /etc/sudoers

在末尾添加以下一行

jenkins所有= NOPASSWD:所有

在这里签出以了解此问题的根本原因

其他回答

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

作为参考,以防其他人遇到同样的问题,我在一个小时内被这个错误卡住了,因为我使用的是NOPASSWD参数,所以不应该发生这个错误。

我不知道的是,当没有tty并且用户尝试启动的命令不在/etc/sudoers文件中允许的命令的一部分时,sudo可能会引发完全相同的错误消息。

这里有一个简化的例子,我的文件内容与我的问题:

bguser ALL = NOPASSWD: \
    command_a arg_a, \
    command_b arg_b \
    command_c arg_c

当bguser尝试在没有任何tty的情况下启动“sudo command_b arg_b”(bguser被用于某些守护进程)时,他将遇到错误“no tty present and no askpass program specified”。

Why?

因为在/etc/sudoers文件的末尾缺少一个逗号…

(我甚至怀疑这是否是预期的行为,而不是sudo中的错误,因为这种情况下正确的错误消息应该是“对不起,用户bguser不允许执行等”)

我能够做到这一点,但请确保按照正确的步骤。 这是为任何得到导入错误的人准备的。

步骤1:检查文件和文件夹是否有执行权限问题。 Linux用户使用:

chmod 777 filename

步骤2:检查哪个用户有执行它的权限。

Step3:打开终端键入此命令。

sudo visudo

将这些行添加到下面的代码中

www-data ALL=(ALL) NOPASSWD:ALL
nobody ALL=(ALL) NOPASSWD:/ALL

这是授予执行脚本的权限,并允许它使用所有的库。用户通常是“nobody”或“www-data”。

现在将代码编辑为

echo shell_exec('sudo -u the_user_of_the_file python your_file_name.py 2>&1');

进入终端查看进程是否正在运行 在这里输入这个…

ps aux | grep python

这将输出在python中运行的所有进程。

附加元件: 使用下面的代码检查系统中的用户

cut -d: -f1 /etc/passwd

谢谢你!

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

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