Last updated:

As you just finished to configure your sudoers to execute a remote command without a password through an ssh connection you are now getting the error sudo: sorry, you must have a tty to run sudo.

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

👉 Not your issue? Check my post on this other common error: sudo: no tty present and no askpass program specified.

What are sudo and tty?

sudo is a command line programm that allow users to run programms with the security privileges of another user or group which default to the superuser (i.e. root). The command name originally meant superuser do. You will often see the term sudoers which refers to your sudo users or the sudo configuration file which is generally located at /etc/sudoers.

TTY stand for teletypewriter which comes through a long history before the computer era. Nowadays, the tty command is used to provide the file name of the terminal connected to the standard input, example: /dev/ttys001.

What is ‘sudo: sorry, you must have a tty to run sudo’?

The error message sudo: sorry, you must have a tty to run sudo will occur when the sudo command is trying to execute a command that requires a tty. It is most-likely happening because you are running on a Linux distribution with sudo configured by default to require a tty. Requiring sudo commands to run from a real tty was generally done as a matter of security concern. In reality, this does not provide any real security benefit. This is generally enforced by setting Defaults requiretty in the /etc/sudoers.

Some Linux distributions have been known to have this as a default configuration. RedHat just recently removed this from Fedora and REHL, see Bug 1020147.

How to solve ‘sudo: sorry, you must have a tty to run sudo’?

Disable the Tty Requirement

You can either disable requiretty globally or for a single sudo user, group, or command.

To disable this feature globally, replace Defaults requiretty by Defaults !requiretty in your /etc/sudoers.

Alternatively, to change this configuration at a per user, per group or per command basis, you can suffix the Defaults word with the appropriate user, group, or command as in the example below.

Defaults!/path/to/my/bin !requiretty
Defaults:myuser !requiretty

Workaround a Tty Requirement by Using a Pseudo-tty

In order to use a pseudo-tty you will need to connect by ssh using the -t options. Example: ssh -t user@myhost sudo mycommand.

From man ssh:

     -t      Force pseudo-tty allocation.  This can be used to execute arbitrary screen-based programs on a remote
             machine, which can be very useful, e.g. when implementing menu services.  Multiple -t options force
             tty allocation, even if ssh has no local tty.

⚠️ If you encounter the error Pseudo-terminal will not be allocated because stdin is not a terminal., then you will need to use -tt to force a pseudo-terminal. If you don’t need a pseudo-terminal, then use -T which would simply disable pseudo-tty allocation.