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

在使用NetBeans编译项目时遇到'sudo:不存在tty且未指定AskPass程序'的错误。解决方案包括使用AskPass程序替代密码输入,或者修改sudoers文件以允许用户无密码执行特定命令。此外,还可以考虑在非Shell脚本中使用Expect工具处理需要root权限的命令。
摘要由CSDN通过智能技术生成

本文翻译自: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 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. 在屏幕底部查看需要在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行,我的意思是:

     jenkins ALL=(ALL) NOPASSWD: ALL 
  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]。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这个错误提示是因为在执行 sudo 命令时,终端不存在或者没有指定 askpass 程序。可能是因为您在非终端环境下执行sudo 命令,或者您的系统中缺少必要的组件。建议您检查一下终端环境和系统组件是否正常,或者尝试在终端中执行 sudo 命令。 ### 回答2: sudoLinux/Unix 操作系统中的一个命令,它允许普通用户在执行某些需要管理员权限才能完成的操作时获取临时提升权限。在执行 sudo 命令时,系统会提示用户输入密码,以确认其身份和授权权限。 在出现 "sudo: 没有终端存在,且未指定 askpass 程序" 的报错信息时,通常是由于用户在使用 sudo 命令时没能正确设置终端或使用 askpass 程序所导致的。 以终端设置为例,如果用户在执行 sudo 命令时出现该报错信息,可能是由于用户所使用的终端不支持 sudo 命令。此时解决方法是使用支持 sudo 命令的终端来执行 sudo 命令,或者在执行命令时指定终端名称。 对于 askpass 程序的指定问题,如果用户在执行 sudo 命令时没有指定 askpass 程序,那么系统会默认使用 gnome 或者 kde 等桌面环境提供的 askpass 程序。如果用户没有安装桌面环境或者安装的桌面环境缺少 askpass 程序,那么就会出现 "sudo: 没有终端存在,且未指定 askpass 程序" 的错误信息。解决方法是安装桌面环境或者手动指定 askpass 程序。具体的操作可以参考系统文档或者在线资料。 总之,在使用 sudo 命令时,用户需要确认自己所使用的终端是否支持该命令,并在必要的时候设置 askpass 程序。只有这样,才能避免出现 "sudo: 没有终端存在,且未指定 askpass 程序" 的问题。 ### 回答3: sudo 这个命令是 Linux 系统中常用的一个命令,可以让普通用户以超级管理员的身份来运行某些命令或者访问某些文件。然而,当使用 sudo 命令的时候,有时会遇到 “sudo: 没有终端存在,且未指定 askpass 程序” 的错误提示。 这个错误提示的原因是因为在当前环境下,sudo 没有找到合适的 askpass 程序来获取用户的密码。askpass 程序是一个用来获取用户密码的程序,通常情况下,sudo 会自动选择合适的 askpass 程序,比如 gtk-ask-pass、kde-ask-pass 或者是 x11-ask-pass 等。但是,在某些情况下,sudo 无法找到合适的 askpass 程序,就会出现 “sudo: 没有终端存在,且未指定 askpass 程序” 的错误提示。 如果出现这个错误提示,可以尝试以下方法解决: 1. 检查系统中是否安装了合适的 askpass 程序,如果没有安装,可以使用包管理器安装。 2. 在运行 sudo 命令的时候,使用 -A 选项来明确指定 askpass 程序的路径,比如: sudo -A /usr/libexec/openssh/gnome-ssh-askpass ls 其中 /usr/libexec/openssh/gnome-ssh-askpass 就是指定的 askpass 程序的路径。 3. 禁用 sudo 命令的 tty 验证,使用 -S 选项来禁用 sudotty 之间的交互,比如: echo "your_password" | sudo -S ls 其中 "your_password" 就是你的账户密码,这样 sudo 就能够通过标准输入流获取到用户的密码,而不需要与 tty 进行交互。 总之,出现“sudo: 没有终端存在,且未指定 askpass 程序” 的错误提示,通常是由于系统环境配置问题引起的。可以按照上述方法来解决这个问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值