In Linux, error ‘sudo: no tty present and no askpass program specified‘ occurs when sudo can’t find any tty (terminal) devices such as stdin. If this occurs, sudo will terminate with this error message. This article provides information about why sudo fails with this error message and how it can be fixed.
What are sudo, tty, and askpass?
Sudo stands for ‘Super User Do’. It is a command-line program for Unix-like computer systems that allow users to execute programs with the security privileges of another user.
In other words, it is a command that can be used by administrators to run commands as if they were the root user (a.k.a. “superuser”).
When you execute ‘sudo’ followed by a command or series of commands, then your process will have elevated privileges on Linux-based systems such as Ubuntu.
A TTY is an abbreviation for Teletype which is what we now call computer terminals with keyboard input/output devices. On modern operating systems, there isn’t really a need for these anymore because most people use their computers remotely through a terminal emulator.
An askpass program is a tool that can be used by the sudo command to prompt for your password without revealing it on the screen in plain text, which would defeat any security measures set up and could allow someone else to enter commands as if they were you with elevated privileges.
What is a sudoers file?
The sudoers file is a configuration that sudoers use to configure sudo in Linux. The sudoers configuration file is located at /etc/sudoers.
The sudoer configuration file is parsed in textual order, so it starts to parse from top to bottom of the document. The first match found will be used for sudo command request.
The sudoers file must be edited by running
$ sudo visudo
For example, suppose you want to give user larry permission to shutdown a machine. Without the sudo permission, larry would see this error message:
$ sudo shutdown [sudo] password for larry: Sorry, user larry is not allowed to execute '/usr/sbin/shutdown' as root on mars.planet.com.
To give user larry the ability to shutdown the machine, add the line below to the sudoers file:
larry ALL=(ALL) NOPASSWD: /sbin/shutdown
When user larry logs in to the machine, he can run the sudo command to shutdown the machine as shown below.
$ sudo shutdown now
The machine is shutdown automatically.
Why does sudo fail with this error message ‘sudo: no tty present and no askpass program specified’?
Error ‘sudo: no tty present and no askpass program specified’ means you cannot use sudo unless there is someone logged in as root or the user has been granted permission to run commands without a password for this session.
If you want to sudo without a password, then it is likely that your sudoers file does not have the NOPASSWD setting enabled. To resolve this, adjust your permissions as shown above.
If you have the NOPASSWD configured and you still get this error when executing a command, it might be because you are not running it with a tty. This might happen if you connect to your machine over SSH, or if you run the command from inside of a crontab.
If the error happens when you are using SSH, you need to use the ‘ssh -t‘ option. This will provide a shell with a ‘pseudo-tty’ and fix the problem.
$ ssh -t mars "sudo shutdown now"
In this example, you SSH to remote machine mars so you can do a shutdown. The -t option forces a pseudo-tty allocation which allows you to run arbitrary commands on a remote machine.