sudo: Sorry, you must have a tty to run sudo Error on a Linux and Unix

I‘m trying to run the following command:
ssh sudo command1 /path/to/file
But, an error is displayed follows:

sudo: sorry, you must have a tty to run sudo


sudo: no tty present and no askpass program specified

How do I fix this problem on Linux or Unix based systems?

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements ssh on Linux or Unix
Est. reading time 2m
This is done in Fedora, RHEL, CentOS, many other Linux distribution, and Unix-like systems for security concern as it will show the password in clear text format.

What is the fix on a Linux or Unix bash shell?

You have to run your ssh command as follows to avoid error that read as sudo: Sorry, you must have a tty to run sudo Error:

ssh -t hostname sudo command
ssh -t user@hostname sudo command
ssh -t sudo command1 /path/to/file

Sample session:

Fig.01: Linux and Unix tty to run sudo if I can sudo without a password over ssh

Fig.01: Linux and Unix tty to run sudo if I can sudo without a password over ssh

The -t option 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.

The requiretty option in sudoers file

The requiretty if set in sudo config file sudoers, sudo will only run when the user is logged in to a real tty. When this flag is set, sudo can only be run from a login session and not via other means such as cron, shell/perl/python or cgi-bin scripts. This flag is set on many distores by default. Edit /etc/sudoers, file, enter:
# visudo
Find line that read as follows:

Defaults    requiretty

Either comment it out the line or delete the line:

#Defaults    requiretty

Save and close the file.

How Do I run command without using the -t option?

You can use the su command instead of the sudo command as follows:

## NOTE: RHEL/CentOS specific syntax ##
su --session-command="/path/to/command1 arg1 arg2"
## others ##
su -c '/path/to/command1 arg1 arg2'


ssh su --session-command="/path/to/command1 arg1 arg2" 
ssh su -c '/path/to/command1 arg1 arg2'

You can run /scripts/job5143 as vivek user using the same syntax:

ssh su --session-command="/scripts/job1 /nas" vivek
ssh su vivek -c "/scripts/job1 /nas"

Another option is to use the following syntax (see below in comments):

echo -e "\n"|sudo -S command

See man pages for more info – visudo(8)

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 15 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
15 comments… add one
  • Geoff Aug 25, 2010 @ 18:26

    Next response:
    “Pseudo-terminal will not be allocated because stdin is not a terminal”
    Your thoughts?

  • Geoff Aug 25, 2010 @ 18:33

    — Remove and ampersand after the command and it works fine.

  • acid child Apr 17, 2012 @ 19:57

    This worked perfectly for me thanks for the tip!

  • nkiran Nov 1, 2012 @ 5:04

    One more way of doing this –
    Open /etc/sudoers
    comment out: #Default requiretty


    • benwing Oct 23, 2013 @ 18:52

      modifying sudoers can be taken a step further and only be made a requirement for specific users.
      ie. leave Defaults requiretty uncommented and add

      Defaults:username    !requiretty
  • Abhinav Dec 27, 2013 @ 7:16

    Good one. Needed to do it today. and that error was irritating the hell out of me. Thanks for the post.

  • andy Apr 1, 2014 @ 7:51

    I think we can try sudo with -S parameter. As from man page of sudo:

    -S : The -S (stdin) option causes sudo to read the password from the standard input instead of the terminal device. The password must be followed by a newline character. So maybe:

    echo -e "\n"|sudo -S 

    And here’s more info about this.

  • AngelDeaD Feb 9, 2015 @ 20:28

    I work with linux for more than 2 years. But on dedicated servers, that linux does not require to use sudo. Sudo is not even installed. And I have never had issues with permission, except chmod. How to use linux with out sudo, like it is on dedicated servers? (I’m talking about Debian, I use it since Debian 5) When I download debian from official website and install it on my laptop it require sudo almost for everything. And this Debian require an user to use Debian, while on dedicated servers, you do everything with root, you log in to root, you create users with root, and everything with OUT sudo. Is there server version and client version of linux’s or I’m missing something.
    Thanks in advance for reply.

  • Prakash Ashokan Oct 22, 2015 @ 18:20

    It helps to resolve the “[sshexec] sorry, you must have a tty to run sudo” issue.

  • Prakash Ashokan Oct 22, 2015 @ 18:21

    Thanks a lot for the commands.

  • Prakash Ashokan Oct 22, 2015 @ 21:06

    Is there any option without Comment this #Defaults requiretty because my team is don’t want to edit the SUDO file. So I have to resolve with my own script. Please help me to resolve this issue. Kindly provide me if you have any solutions.

  • Shubham Feb 3, 2016 @ 12:06

    ssh -t hostname sudo command

    Above syntax made my day… Thanks a lot guys.. :)

  • Mohd Meesam Feb 15, 2016 @ 13:19

    Disable ‘requiretty’ in sudoers file

  • Ashvini Sep 5, 2016 @ 19:14

    Above commands are helpful, but my problem is somewhat different:

    I am trying to launch a linux instance using Redhat linux ami and got the error “sudo: Sorry, you must have a tty to run sudo” to run user data script. Can anyone help how to get rid of this error with CloudFormation template? Any help is appreciated. Thank you!

  • Minakshi Feb 15, 2017 @ 23:43

    Thank you. My solution was to edit /etc/sudoers
    Either comment it out the line or delete the line:
    #Defaults requiretty

    But, I am still puzzled that why did it change? I had executed 10 different commands before that as an ansible user and then ran couple of playbooks as “root” and all of a sudden, it stopped working with error message: | FAILED | rc=0 >>

    Thanks for posting the solution.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum