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