Changing the behavior of ssh
When you run ssh without a command and there is a local pseudo-terminal, the tool allocates a pseudo-terminal on the remote side automatically. Usually you access an interactive remote shell this way, so allocating a terminal is the right thing to do.
When you provide a remote command to ssh, it assumes the command is not interactive. It doesn't provide a pseudo-terminal to the command. This happens in your case, sudo finds no terminal.
You can explicitly tell local ssh to allocate a pseudo-terminal on the remote side:
-t
Force pseudo-terminal allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.
(source)
ssh -t user@hostIPAddress "cd /opt/somewhere && sh -v -v ./install.sh"
Note when you do this, you can no longer tell the stdout and stderr of ./install.sh apart locally. Read the "broader picture" part of this another answer of mine.
Changing the behavior of sudo
sudo suggests alternative solutions that depend solely on sudo itself:
sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
These are:
- read from standard input:
sudo -S (see this answer);
- configure an askpass helper:
sudo -A (see the second part of this answer).
Both require an argument to sudo, so you would need to change the script. It's easy to lessen security by using any of these options. Strongly prefer ssh -t. Note in general sudo may be configured not to work without a terminal anyway.
ssh user@hostIPAddress 'sudo whoami'? Is this the same user and machine you meant when you said, "I can run sudo commands without needing to input a password"? – bitinerant Dec 30 '20 at 18:52#!/bin/bash¶echo I am $(sudo whoami)and ranssh myserver "cd /tmp && sh -v -v ./install.sh"but it worked fine. – bitinerant Dec 30 '20 at 19:09ssh user@hostIPAddress 'sudo whoami'worked without requiring a password, then we have a proof point that you can run (at least some) sudo commands through ssh successfully, right? That would indicate to me that the problem is likely somewhere in thesh -v -v install.sh. As bitinerant said, give his simple-test install.sh a try. If it works, then we need to figure out what in the otherinstall.shis causing password request. Try thatsudo pm2 stop someprocessin the test file and see if you can isolate the problem to that. – NotTheDr01ds Dec 30 '20 at 20:28-tto the ssh command and it will prompt you for the password. – bitinerant Dec 30 '20 at 22:25-tis sosudocan prompt you for a password. – bitinerant Dec 30 '20 at 22:41sudoers, but with care. See How to run an application using sudo without a password?. Note "You can replace the path to an executable with "ALL" if you choose, giving you complete passwordless sudo." – bitinerant Dec 30 '20 at 22:55a terminal is required] error?" and the accepted answer has nothing to do with it. Please [edit] and make the question coherent with what you accepted. I will revoke my vote and delete my answer then. – Kamil Maciorowski Dec 31 '20 at 00:02