I am trying to run a command on a remote server as the superuser through ssh
. For that, I pass the command with ssh -t
from macOS as follows:
bash-5.2$ ssh -t user@server.com "sudo echo Hi"
I get the password prompt for logging in to the server, but then instead of the password prompt for sudo I get the following error (no matter which command I use with sudo):
bash: line 1: sudo: command not found
Connection to server.com closed.
I don't understand why this happens. When I login to the server and run the command on it separately, it works fine. So this works:
bash-5.2$ ssh user@server.com
... Password:
Last login: ...
bash-5.1$ sudo echo hi
Password:
Last login: ...
hi
I am quite new to this so I may be overlooking something obvious.
which
? What to use then? – Kamil Maciorowski Nov 07 '22 at 07:59sudo
works when he logs in to the server, as you said, just use the output ofwhich sudo
as the full path, without guessing and withoutlocate
. – aviro Nov 07 '22 at 08:00/usr/local/bin
which is indeed present in thePATH
environment variable. I have verified that this works correctly:ssh -t user@server.com "PATH=/usr/local/bin:$PATH && sudo echo Hi"
but I don't understand why I need to updatePATH
when sudo is present inPATH
when I login to ther server. – Shubham Johri Nov 07 '22 at 20:03PATH=/usr/local/bin:\$PATH
otherwise the command substitutes the client's $PATH value. – Shubham Johri Nov 07 '22 at 20:30PATH
on the server in~/.ssh/environment
(needs to be enabled byPermitUserEnvironment yes
insshd_config
)? Maybe, enablingPermitUserEnvironment yes
is enough. – Fco Javier Balón Nov 08 '22 at 08:02