When trying to execute a command with sudo
, either through a cronjob or remotely, you may be 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 error.
👉 If this is 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).
%admin ALL=(ALL) NOPASSWD:ALL
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 sudo
to 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.