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:59sudoworks when he logs in to the server, as you said, just use the output ofwhich sudoas the full path, without guessing and withoutlocate. – aviro Nov 07 '22 at 08:00/usr/local/binwhich is indeed present in thePATHenvironment 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 updatePATHwhen sudo is present inPATHwhen I login to ther server. – Shubham Johri Nov 07 '22 at 20:03PATH=/usr/local/bin:\$PATHotherwise the command substitutes the client's $PATH value. – Shubham Johri Nov 07 '22 at 20:30PATHon the server in~/.ssh/environment(needs to be enabled byPermitUserEnvironment yesinsshd_config)? Maybe, enablingPermitUserEnvironment yesis enough. – Fco Javier Balón Nov 08 '22 at 08:02