A tty or basically a terminal is required if a program need to receive user input such as password for sudo. If you run a script via SSH that asks for sudo's password, you'll get the following sudo: no tty present and no askpass program specified error as no tty is allocated by default for an automated SSH session;

$ ssh [email protected] -- sudo ls
[email protected]'s password:
sudo: no tty present and no askpass program specified

askpass is a program that could automate password input.

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 configure both SSH and sudo to fix no tty present and no askpass program specified and a terminal is required to read the password error when running sudo via SSH.

Steps to fix sudo: no tty present and no askpass program specified error in SSH:

  1. Allocate pseudo terminal when connecting via SSH.
    $ ssh -t [email protected] -- sudo id
    [email protected]'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.
  2. Force sudo to read the password from stdin by using --stdin or -S switch.
    $ ssh [email protected] -- sudo -S id
    [email protected]'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.
  3. Configure passwordless sudo for the user and command on the SSH server.
    $ ssh [email protected] -- sudo id
    [email protected]'s password:
    uid=0(root) gid=0(root) groups=0(root)
Discuss the article:

Comment anonymously. Login not required.

Share!