天天看点

如何修复“ sudo:不存在tty且未指定AskPass程序”错误?

本文翻译自:How to fix 'sudo: no tty present and no askpass program specified' error?

I am trying to compile some sources using a makefile.

我正在尝试使用makefile编译一些源。

In the makefile there is a bunch of commands that need to be ran as

sudo

.

在makefile中,有一堆需要作为

sudo

运行的

sudo

When I compile the sources from a terminal all goes fine and the make is paused the first time a

sudo

command is ran waiting for password.

当我从终端编译源代码时,一切正常,第一次运行

sudo

命令等待密码时,make暂停。

Once I type in the password, make resumes and completes.

输入密码后,make即可恢复并完成操作。

But I would like to be able to compile the sources in NetBeans.

但是我希望能够在NetBeans中编译源代码。

So, I started a project and showed netbeans where to find the sources, but when I compile the project it gives the error:

因此,我启动了一个项目,并向netbeans展示了在哪里可以找到源代码,但是当我编译该项目时会出现错误:
sudo: no tty present and no askpass program specified
           

The first time it hits a

sudo

command.

第一次命中

sudo

命令。

I have looked up the issue on the internet and all the solutions I found point to one thing: disabling the password for this user.

我在互联网上查找了该问题,发现的所有解决方案都指向一件事:禁用该用户的密码。

Since the user in question here is root.

由于此处讨论的用户是root。

I do not want to do that.

我不想那样做。

Is there any other solution?

还有其他解决方案吗?

#1楼

参考:https://stackoom.com/question/1Ssez/如何修复-sudo-不存在tty且未指定AskPass程序-错误

#2楼

sudo

by default will read the password from the attached terminal.

默认情况下,

sudo

将从连接的终端读取密码。

Your problem is that there is no terminal attached when it is run from the netbeans console.

您的问题是从netbeans控制台运行时没有连接终端。

So you have to use an alternative way to enter the password: that is called the askpass program.

因此,您必须使用另一种方式输入密码:称为AskPass程序。

The askpass program is not a particular program, but any program that can ask for a password.

askpass程序不是特定程序,而是可以要求输入密码的任何程序。

For example in my system

x11-ssh-askpass

works fine.

例如,在我的系统中,

x11-ssh-askpass

可以正常工作。

In order to do that you have to specify what program to use, either with the environment variable

SUDO_ASKPASS

or in the

sudo.conf

file (see

man sudo

for details).

为此,您必须使用环境变量

SUDO_ASKPASS

或在

sudo.conf

文件中指定要使用的程序(有关详细信息,请参见

man sudo

)。

You can force

sudo

to use the askpass program by using the option

-A

.

您可以通过使用

-A

选项强制

sudo

使用askpass程序。

By default it will use it only if there is not an attached terminal.

默认情况下,仅当没有连接的终端时,它才会使用它。

#3楼

Try:

尝试:
ssh -t remotehost "sudo <cmd>"
           

This will remove the above errors.

这将消除上述错误。

#4楼

Granting the user to use that command without prompting for password should resolve the problem.

授予用户使用该命令而不提示输入密码的权限,应该可以解决该问题。

First open a shell console and type:

首先打开一个shell控制台并输入:
sudo visudo
           

Then edit that file to add to the very end:

然后编辑该文件以添加到最后:
username ALL = NOPASSWD: /fullpath/to/command, /fullpath/to/othercommand
           

eg

例如
john ALL = NOPASSWD: /sbin/poweroff, /sbin/start, /sbin/stop
           

will allow user

john

to sudo

poweroff

,

start

and

stop

without being prompted for password.

将允许用户

john

sudo

poweroff

start

stop

而无需提示输入密码。

Look at the bottom of the screen for the keystrokes you need to use in visudo - this is not vi by the way - and exit without saving at the first sign of any problem.

在屏幕底部查看需要在visudo中使用的击键-顺便说一句,这不是vi-并退出而不保存任何问题的第一个迹象。

Health warning: corrupting this file will have serious consequences, edit with care!

健康警告:损坏此文件将有严重后果,请谨慎编辑!

#5楼

Try:

尝试:
  1. Use

    NOPASSWD

    line for all commands, I mean: 对所有命令使用

    NOPASSWD

    行,我的意思是:
  2. Put the line after all other lines in the

    sudoers

    file. 将行放在

    sudoers

    文件中的所有其他行之后。

That worked for me (Ubuntu 14.04).

这对我有用(Ubuntu 14.04)。

#6楼

This error may also arise when you are trying to run a terminal command (that requires root password) from some non-shell script, eg

sudo ls

(in backticks) from a Ruby program.

当您尝试从某些非Shell脚本运行终端命令(需要root密码)时,例如在Ruby程序中的

sudo ls

(在反引号中),也会出现此错误。

In this case, you can use Expect utility ( http://en.wikipedia.org/wiki/Expect ) or its alternatives.

在这种情况下,您可以使用Expect实用程序( http://en.wikipedia.org/wiki/Expect )或其替代方法。

For example, in Ruby to execute

sudo ls

without getting

sudo: no tty present and no askpass program specified

, you can run this:

例如,在Ruby中执行

sudo ls

而不获取

sudo: no tty present and no askpass program specified

,您可以运行以下命令:
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
           

[this uses one of the alternatives to Expect TCL extension: ruby_expect gem].

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

继续阅读