A tty is a terminal required if a program needs to receive user input, such as when entering a password for sudo.
Running a script via SSH that asks for sudo's password will get you the following sudo: no tty present and no askpass program specified error, as no tty is allocated by default for an automated SSH session. You might also run into a terminal is required to read the password when doing the same.
$ ssh user@simplified.guide -- sudo ls user@simplified.guide's password: sudo: no tty present and no askpass program specified
askpass is a program that could automate password input and is normally used to automate SSH login.
sudo could also throw the following error, depending on the version.
sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
You can avoid both no tty present and no askpass program specified and a terminal is required to read the password errors when running sudo via SSH by configuring the SSH client and sudo.
Steps to fix sudo: no tty present and no askpass program specified error in SSH:
- Allocate pseudo terminal when connecting via SSH.
$ ssh -t user@example.com -- sudo id user@example.com's password: [sudo] password for user: uid=0(root) gid=0(root) groups=0(root) Connection to example.com closed.
-t Force pseudo-terminal allocation. This can be used to execute arbitrary screen- based programs on a remote machine, which can be very useful, e.g. when implement- ing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.
- Force sudo to read the password from stdin by using --stdin or -S switch.
$ ssh user@example.com -- sudo -S id user@example.com's password: [sudo] password for user: pass uid=0(root) gid=0(root) groups=0(root)
Your password will be displayed on the stdout and could pose a securitk risk. pass is the password in this example.
-S, --stdin Write the prompt to the standard error and read the password from the standard input instead of using the terminal device.
- Configure passwordless sudo for the user and command on the SSH server.
$ ssh user@example.com -- sudo id user@example.com's password: uid=0(root) gid=0(root) groups=0(root)

Mohd Shakir Zakaria is a skilled cloud architect with a background in development, entrepreneurship, and open-source advocacy. As the founder of Simplified Guide, he helps others understand the complexities of computing, making tech concepts accessible to all.
Comment anonymously. Login not required.