Shell – “Command not found” when using ssh and non absolute commands

shellsshsshdsynology

I want to use a command over ssh:

ssh myuser@myhost mycommand

but doing so I always get:

sh: mycommand: command not found

using following obviously works:

ssh myuser@myhost /usr/local/bin/mycommand

and i understand why: it's because the command is somehow executed over a non-login shell.

Using the full command or any other parameters in my ssh command is not an option in my scenario. My command is executed by a script I cannot touch and worked on every host yet except this one.

The host that's giving me the problem is a Synology NAS and the /etc/passwd setting for that myuser is:

myuser:x:1048:100::/var/services/homes/myuser:/bin/sh

Again:

I can:

  • ssh as myuser into myhost
  • execute as myuser using the absolute path provided by which
    mycommand
  • execute mycommand (non absolute) when already on myhost (via ssh)

I can't but want:

  • execute: ssh myuser@myhost mycommand (non absolute, no additional
    parameters)

Best Answer

Probably, your $PATH doesn't include /usr/local/bin. Since this is ssh, there are three approaches that come to mind:

  1. If PermitUserEnvironment is enabled in the sshd config, you ought to be able to set PATH in ~/.ssh/environment (that's a file in your home directory on the server — the NAS).

  2. If you can edit the sshd config, then you should be able to use SetEnv PATH=/bin:/usr/bin:/usr/local/bin (etc.) to set a path. At least if it's using OpenSSH.

  3. It's possible you could use the ssh client's SetEnv option to send the server a PATH, depending on the server config. You could set this in your ~/.ssh/config file, on your client machine.

Note that both OpenSSH server and client config files can have options limited to particular clients/servers. For example, in the client config, you could do something like this:

Host myhost
    SetEnv PATH=/bin:/usr/bin:/usr/local/bin

to only do that for one server. Note that block continues until the next block starts (e.g, another Host … block) — the indenting is just for visual clarity.

OpenSSH config files are documented in the ssh_config and sshd_config manual pages.

Related Question