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:24

Granting the user to use that command without prompting for password should resolve the problem. First open a shell console and type:

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.

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!

查看更多
ら面具成の殇う
3楼-- · 2018-12-31 10:26

Try this one:

echo '' | sudo -S my_command
查看更多
深知你不懂我心
4楼-- · 2018-12-31 10:28

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

So this is my /etc/sudoers config

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

查看更多
人间绝色
5楼-- · 2018-12-31 10:30

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.

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.

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

Why?

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

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

查看更多
无色无味的生活
6楼-- · 2018-12-31 10:31

Other options, not based on 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.
查看更多
梦该遗忘
7楼-- · 2018-12-31 10:32

If by any chance you came here because you can't sudo inside the Ubuntu that comes with Windows10

  1. Edit the /etc/hosts file from Windows (with Notepad), it'll be located at: %localappdata\lxss\rootfs\etc, add 127.0.0.1 WINDOWS8, this will get rid of the first error that it can't find the host.

  2. To get rid of the no tty present error, always do sudo -S <command>

查看更多
登录 后发表回答