SSH – Fix No TTY Present When Running Commands Over SSH in Here-Document

bashsshsudo

I'm trying to run a command over SSH like this in a here-document:

ssh example.com <<END
sudo /etc/init.d/apache2 reload
END

Which normally works except when I'm trying to run something that needs input, such as the password for sudo. In that case, I'm getting the following message:

sudo: no tty present and no askpass program specified

I know I can use the -t flag on SSH to allocate a pseudo-tty such as this:

ssh -t example.com "sudo /etc/init.d/apache2 reload"

And it'll work, but when I try the same thing with here-document, it doesn't work and I'll get the same error about no tty being present:

ssh -t example.com <<END
sudo /etc/init.d/apache2 reload
END

Any idea how I can make this work?

Also, in case you are wondering why I want it to work with the here-document instead of just passing it on the same line, it is because the input commands (there can be a few) come from a configuration file read by a script and I've heard it avoids the trouble of escaping the commands for the quotes, double quotes, etc.

Best Answer

Use visudo to edit the sudoers file and insert a below line:

Defaults:<user>    !requiretty

It doesn't answer why using the ssh -t "something" versus ssh -t <<STOP something STOP doesn't work. Let's say I'm not using sudo but I'm using passwd for my own user directly, I still won't get the TTY using the heredoc.

Try ssh -t -t to force pseudo-tty allocation even if stdin is not a terminal.