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"