How to fix 'sudo: no tty present and no askpas

2018-12-31 09:58发布

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.

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. Once I type in the password, make resumes and completes.

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:

sudo: no tty present and no askpass program specified

The first time it hits a sudo command.

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.

Is there any other solution?

标签: linux sudo tty
21条回答
ら面具成の殇う
2楼-- · 2018-12-31 10:32

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

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].

查看更多
明月照影归
3楼-- · 2018-12-31 10:33

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

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

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

sudo /usr/sbin/ipset

Instead of

sudo ipset

查看更多
余欢
4楼-- · 2018-12-31 10:38

Maybe the question is unclear that why no answer was matching it but I had the same error message when I was trying to mount sshfs which required sudo : the command is something like this :

sshfs -o sftp_server="/usr/bin/sudo /usr/lib/openssh/sftp-server" user@my.server.tld:/var/www /mnt/sshfs/www

by adding the option -o debug

sshfs -o debug -o sftp_server="/usr/bin/sudo /usr/lib/openssh/sftp-server" user@my.server.tld:/var/www /mnt/sshfs/www

I had the same message of this question :

sudo: no tty present and no askpass program specified

So by reading others answer I became to make a file in /etc/sudoer.d/user on my.server.tld with :

user ALL=NOPASSWD: /usr/lib/openssh/sftp-server

and now I able to mount the drive without giving too much extra right to my user.

查看更多
有味是清欢
5楼-- · 2018-12-31 10:39

Try:

ssh -t remotehost "sudo <cmd>"

This will remove the above errors.

查看更多
无与为乐者.
6楼-- · 2018-12-31 10:41

sudo by 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 askpass program.

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

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).

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

查看更多
高级女魔头
7楼-- · 2018-12-31 10:41

For Ubuntu 16.04 users

There is a file you have to read with:

cat /etc/sudoers.d/README

Placing a file with mode 0440 in /etc/sudoers.d/myuser with following content:

myuser  ALL=(ALL) NOPASSWD: ALL

Should fix the issue.

Do not forget to:

chmod 0440 /etc/sudoers.d/myuser
查看更多
登录 后发表回答