Home > IT Architecture, Linux, Programming, SHELL, Systems > resolved – sudo: sorry, you must have a tty to run sudo

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

April 1st, 2014

The error message below sometimes will occur when you run a sudo <command>:

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

To resolve this, you may comment out "Defaults requiretty" in /etc/sudoers(revoked by running visudo). Here is more info about this method.

However, sometimes it's not convenient or even not possible to modify /etc/sudoers, then you can consider the following:

echo -e "<password>\n"|sudo -S <sudo command>

For -S parameter of sudo, you may refer to sudo man page:

-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 here -S bypass tty(terminal device) to read the password from the standard input. And by this, we can now pipe password to sudo.


From comments, you may also try below:

1. Comment out Defaults requiretty in /etc/sudoers

2. Defaults:[username] !requiretty #change [username]

3. You can use ssh -t to force pseudo-tty allocation. e.g. ssh -t user1@hostname1 "sudo df -h"

4. If you met error "PTY allocation request failed on channel 0" when SSH, then you can increase pty number

sysctl -a|grep -i pty

kernel.pty.max = 4096
kernel.pty.nr = 237

vi /etc/sysctl.conf #kernel.pty.max = 10000

sysctl -p;sysctl -a|grep pty

Good Luck!

  1. David
    April 8th, 2014 at 19:18 | #1

    Didn’t work for me, had to use su -s /bin/sh -c ‘command’ user

    • April 10th, 2014 at 07:46 | #2

      Hi David,
      If you use su, then you have to know the password for root, and the more secure /etc/sudoers will not functioning by that way. Seems there’s no way to resolve the “sudo: sorry, you must have a tty to run sudo” issue using sudo.

  2. diane martin
    November 14th, 2014 at 17:27 | #3

    if you comment out the line
    Defaults requiretty in /etc/sudoers
    you can bypass this.

  3. January 23rd, 2015 at 02:08 | #4

    you may also try to bypass tty requirement on a per-user basis:
    Defaults:[username] !requiretty

    i have tried this on a CentOS6 host. it works. not sure if it will for other Linux flavors.

  4. raj verma
    January 9th, 2016 at 09:55 | #5

    Hi can any on tell how to use this …in below script
    #Defaults!/bin !requiretty
    #Defaults:production !requiretty
    ssh -t production@nsuat.magnasoft.com
    cd /home/NorthStar/Dhruva/server-specific-scripts/crons/MachineSize

    echo -e “production123 \n” | sudo -S ./size.sh >/home/NorthStar/Dhruva/server-specific-scripts/crons/MachineSize/size_logs.log 2>/home/NorthStar/Dhruva/server-specific-scripts/crons/MachineSize/size_error.log

Comments are closed.