Linux 如何修复“sudo: no tty present and no askpass program specified”错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21659637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:59:07  来源:igfitidea点击:

How to fix 'sudo: no tty present and no askpass program specified' error?

linuxsudotty

提问by hebbo

I am trying to compile some sources using a makefile. In the makefile there is a bunch of commands that need to be ran as sudo.

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

When I compile the sources from a terminal all goes fine and the make is paused the first time a sudocommand is ran waiting for password. Once I type in the password, make resumes and completes.

当我从终端编译源代码时一切正常,并且在第一次sudo运行命令等待密码时 make 暂停。输入密码后,制作简历并完成。

But I would like to be able to compile the sources in 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 中编译源代码。所以,我启动了一个项目并向 netbeans 展示了在哪里可以找到源代码,但是当我编译该项目时,它给出了错误:

sudo: no tty present and no askpass program specified

The first time it hits a sudocommand.

它第一次遇到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. I do not want to do that.

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

Is there any other solution?

还有其他解决方案吗?

回答by rodrigo

sudoby default will read the password from the attached terminal. Your problem is that there is no terminal attached when it is run from the netbeans console. So you have to use an alternative way to enter the password: that is called the askpassprogram.

sudo默认情况下将从连接的终端读取密码。您的问题是从 netbeans 控制台运行时没有附加终端。因此,您必须使用另一种方式来输入密码:称为askpass程序。

The askpassprogram is not a particular program, but any program that can ask for a password. For example in my system x11-ssh-askpassworks fine.

askpass程序不是一个特定的程序,但可以要求提供密码的任何程序。例如在我的系统中x11-ssh-askpass工作正常。

In order to do that you have to specify what program to use, either with the environment variable SUDO_ASKPASSor in the sudo.conffile (see man sudofor details).

为此,您必须使用环境变量SUDO_ASKPASS或在sudo.conf文件中指定要使用的程序(请参阅man sudo有关详细信息)。

You can force sudoto use the askpass program by using the option -A. By default it will use it only if there is not an attached terminal.

您可以sudo使用选项强制使用 askpass 程序-A。默认情况下,只有在没有附加终端时才会使用它。

回答by user262129

Try:

尝试:

ssh -t remotehost "sudo <cmd>"

This will remove the above errors.

这将消除上述错误。

回答by nicdaniau

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 johnto sudo poweroff, startand stopwithout being prompted for password.

将允许用户johnsudo poweroffstartstop不会提示输入密码。

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. Health warning: corrupting this file will have serious consequences, edit with care!

在屏幕底部查看您需要在 visudo 中使用的击键 - 顺便说一下,这不是 vi - 并在出现任何问题的第一个迹象时退出而不保存。健康警告:破坏此文件会造成严重后果,请谨慎编辑!

回答by Fatemeh Jabbari

Try:

尝试:

  1. Use NOPASSWDline for all commands, I mean:

    jenkins ALL=(ALL) NOPASSWD: ALL
    
  2. Put the line after all other lines in the sudoersfile.

  1. NOPASSWD对所有命令使用line,我的意思是:

    jenkins ALL=(ALL) NOPASSWD: ALL
    
  2. 将该行放在sudoers文件中的所有其他行之后。

That worked for me (Ubuntu 14.04).

这对我有用(Ubuntu 14.04)。

回答by lakesare

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. In this case, you can use Expectutility (http://en.wikipedia.org/wiki/Expect) or its alternatives.
For example, in Ruby to execute sudo lswithout getting sudo: no tty present and no askpass program specified, you can run this:

当您尝试从某些非 shell 脚本(例如sudo ls(在反引号中)从 Ruby 程序)运行终端命令(需要 root 密码)时,也可能出现此错误。在这种情况下,您可以使用Expect实用程序 ( http://en.wikipedia.org/wiki/Expect) 或其替代品。
例如,在 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 ExpectTCL extension: ruby_expectgem].

[这使用了ExpectTCL 扩展的替代方案之一:ruby_expectgem]。

回答by kenorb

Command sudofails as it is trying to prompt on root password and there is no pseudo-tty allocated (as it's part of the script).

命令sudo失败,因为它试图提示 root 密码,并且没有分配伪 tty(因为它是脚本的一部分)。

You need to either log-in as root to run this command or set-up the following rules in your /etc/sudoers (or: sudo visudo):

您需要以 root 身份登录以运行此命令或在您的/etc/sudoers (或:) 中设置以下规则sudo visudo

# Members of the admin group may gain root privileges.
%admin  ALL=(ALL) NOPASSWD:ALL

Then make sure that your user belongs to admingroup (or wheel).

然后确保您的用户属于admin组(或wheel)。

Ideally (safer) it would be to limit root privileges only to specific commands which can be specified as %admin ALL=(ALL) NOPASSWD:/path/to/program

理想情况下(更安全)将 root 权限限制为仅可指定为的特定命令 %admin ALL=(ALL) NOPASSWD:/path/to/program

回答by sNICkerssss

Try this one:

试试这个:

echo '' | sudo -S my_command

回答by WhiteWinterWolf

For the reference, in case someone else encounter the same issue, I was stuck during a good hour with this error which should not happen since I was using the NOPASSWD parameter.

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

What I did NOT know was that sudo may raise the exact same error message when there is no tty and the command the user try to launch is not part of the allowed command in the /etc/sudoers file.

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

Here a simplified example of my file content with my issue:

这是我的问题的文件内容的简化示例:

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

When bguser will try to launch "sudo command_b arg_b" without any tty (bguser being used for some daemon), then he will encounter the error "no tty present and no askpass program specified".

当 bguser 尝试在没有任何 tty 的情况下启动“sudo command_b arg_b”(bguser 用于某些守护程序)时,他将遇到错误“没有 tty 存在且没有指定 askpass 程序”。

Why?

为什么?

Because a comma is missing at the end of line in the /etc/sudoers file...

因为 /etc/sudoers 文件中的行尾缺少逗号...

(I even wonder if this is an expected behavior and not a bug in sudo since the correct error message for such case shoud be "Sorry, user bguser is not allowed to execute etc.")

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

回答by omribahumi

Make sure the command you're sudoing is part of your PATH.

确保您正在执行的命令sudo是您的PATH.

If you have a single (or multi, but not ALL) command sudoersentry, you'll get the sudo: no tty present and no askpass program specifiedwhen the command is not part of your path (and the full path is not specified).

如果您有一个(或多个,但不是全部)命令sudoers条目,sudo: no tty present and no askpass program specified当该命令不是路径的一部分(并且未指定完整路径)时,您将获得。

You can fix it by either adding the command to your PATHor invoking it with an absolute path, i.e.

您可以通过将命令添加到您的命令PATH或使用绝对路径调用它来修复它,即

sudo /usr/sbin/ipset

sudo /usr/sbin/ipset

Instead of

代替

sudo ipset

sudo ipset

回答by Abc Xyz

No one told what could cause this error, in case of migration from one host to another, remember about checking hostname in sudoers file:

没有人知道是什么导致了这个错误,在从一台主机迁移到另一台主机的情况下,记得检查 sudoers 文件中的主机名:

So this is my /etc/sudoers config

所以这是我的 /etc/sudoers 配置

User_Alias      POWERUSER = user_name
Cmnd_Alias SKILL = /root/bin/sudo_auth_wrapper.sh
POWERUSER hostname=(root:root) NOPASSWD: SKILL

if it doesn't match

如果不匹配

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

it will pop up this error:

它会弹出这个错误:

no tty present and no askpass program specified

不存在 tty 且未指定 askpass 程序