1

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.

1 Answers1

1

If sudo is installed, probably not in your path.

See if sudo is your path by running which sudo or echo $PATH. If sudo is not in your path, your path variable might be broken. You can try testing this by executing a common location for sudo /usr/bin/sudo or running locate sudo | grep bin to attempt to find its location.

  • 1
  • 1
    ...Or, since sudo works when he logs in to the server, as you said, just use the output of which sudo as the full path, without guessing and without locate. – aviro Nov 07 '22 at 08:00
  • On the remote server, the sudo program is located in /usr/local/bin which is indeed present in the PATH 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 update PATH when sudo is present in PATH when I login to ther server. – Shubham Johri Nov 07 '22 at 20:03
  • I guess I should set PATH=/usr/local/bin:\$PATH otherwise the command substitutes the client's $PATH value. – Shubham Johri Nov 07 '22 at 20:30
  • Has you setted the PATH on the server in ~/.ssh/environment (needs to be enabled by PermitUserEnvironment yes in sshd_config)? Maybe, enabling PermitUserEnvironment yes is enough. – Fco Javier Balón Nov 08 '22 at 08:02