No TTY present when running commands over SSH in here-document

Solution 1:

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.

Solution 2:

Why not simply store the here document in a variable that will be given ssh -t as a command argument.

On a more general note, use ssh -T or specify the remote shell as a command argument if the stdin of the remote host is being redirected from a heredoc (to prevent ssh from trying to allocate a pty).

# store heredoc in a variable
heredoc="$(cat <<EOF
who
sudo ls -ld /
logname
EOF
)"

ssh -t localhost "$heredoc"


# avoid: Pseudo-terminal will not be allocated because stdin is not a terminal.
- ssh example.com <<END
+ ssh -T example.com <<END
+ ssh example.com /bin/sh <<END