sudo: no tty present and no askpass program specified

  • HOME
  • >
  • >
  • sudo: no tty present and no askpass program specified
Last Updated: 

As you try to execute a command with sudo, either through a cronjob or remotely, you are now getting the error sudo: no tty present and no askpass program specified.

This post covers the basics of sudo, tty, and askpass meaning before getting into how to fix or workaround this sudo and askpass related errors.

👉 Not your issue? Check my post on this other common error: sudo: sorry, you must have a tty to run sudo.

What are sudo, tty, and askpass?

The sudo command line programm that can be used to impersonate another user or group in order to run a command with appropriate level of permissions which default to the superuser (i.e. root). The sudo permissions rules are generally defiend with the file located at /etc/sudoers. The keyword NOPASSWD is used in the sudoers to identify command, users, or group that can be run without password requirements.

TTY caries a lot of history but nowadays the tty command is used to identify a terminal through with a file descriptor to access its standard input, example: /dev/ttys001.

askpass refers to whatever programm that should be used to prompt a user for credentials or a passphrase. A common one will be ssh-askpass which is an X11-based passphrase dialog for openssh.

What is 'sudo: no tty present and no askpass program specified'?

The error message sudo: no tty present and no askpass program specified will occur when the sudo command is trying to execute a command that requires a password but sudo does not have access to a tty to prompt the user for a passphrase. As it can't find a tty, sudo fall back to an askpass method but can't find an askpass command configured.

Depending on what you are trying to achieve, you may have different ways to solve the problem.

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

Execute a command with sudo and no password requirements

If your intend is to run sudo with no password requirements, then you probably haven't configured your sudoers file with NOPASSWD. See the below examples and adjust to your permissions needs (limit to user or commands).

jdoe ALL=(root) NOPASSWD:/bin/myCommand

If you have the NOPASSWD properly configured and you are getting this error while executing the command, then you may be running the command without tty. This may occur when connecting through ssh, running the command from a crontab (i.e. cron), or any environement that is lacking a tty.

If the issue occure through an ssh connection, then you simply need to use the ssh -t option to provide a pseudo-tty and fix the error.

In some cases, the pseudo-tty option may not be working as expected, especially in a chrooted environment. Also, if the problem occurs while running in a crontab or similar then you may have to rely on the sudo -n option that will run sudo in a non-interactive mode and avoid any user prompt and skip the askpass issue. Your sudo command may still fail as your user or command won't be configured for NOPASSWD in your sudoers, hence the password prompt.

Execute a command with sudo and prompt for a password

If you expect a credential or password prompt, then you may need to define the askpass application used by sudo. You can either use the environment variable SUDO_ASKPASS or set it directly in the /etc/sudoers and point it to the command line to use, for example, ssh-askpass.

If you don't want to use an askpass command or cannot use one, then you can use sudo -S which will direct sudoto read the password from the standard input (stdin) instead of prompting the user for it with an askpass command. Note that sudo -S will still send the credential prompt to the standard error (stderr) and it will expect the password sent to stdin to terminate with a newline character.

Related linux posts that you may like...
The Complete How To Guide of Bash Functions
Learn how to write shell scripts with bash functions. This guide includes examples and best practices on how to define, call, and debug functions in bash.
How To Create Simple Menu with the Shell Select Loop?
The select loop is not a regular shell loop. It can be used in Bash to generate a simple menu from which a user can select numbered options.
What is the Right Way to do Bash Loops?
Looping over a list of numbers or words is a building block in shell scripts. Learn how to write Bash loops, including for loop, while loop, and until loop.
What is the Best Way to Count Files in a Directory?
Learn how to count the number of files in a directory using the Linux command line ls, find, and a native bash shell solution with globs and arrays.