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:



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)

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.

  • Thank you for your very detailed answer! Especially 1 and 2 look very promissing. I should have mentioned that I have root rights on host and can edit whatever I want. I guess that for 1 I need to set the enviroment for each user on host. I'll give it a test in a few hours when I'm back in the network.
    – Simon
    Jan 8 '19 at 20:39
  • @Simon #2 is a system-wide one, so if you need it for a bunch of users, that's probably the option you want to pick. At least if you can edit the config (no idea what Synology allows).
    – derobert
    Jan 8 '19 at 20:59
  • thanks again for your answer, I started to investigate the reason why I even have this problem to begin with and I decided to go another, more simplier but maybe also dirtier (?) way. (see my answer)
    – Simon
    Jan 9 '19 at 9:42
  • FYI using ~/.ssh/environment doesn't work for me at all. If I set PATH=$PATH:... and try $ ssh host 'echo $PATH', it prints a literal $PATH (and anything else results in command not found). Explanation here. (Both client and host are Debian FWIW.)
    – Ryan Lue
    Nov 3 '20 at 9:34

Ok so on further investigation I noticed that on all of my other hosts mycommand is indeed in /usr/bin not in /usr/local/bin. The reason for this is, that every package manager has installed it in /usr/bin except for the weird "software store" (or whatever it is called) from synology.

I went the "dirty" way and just created a symlink: /usr/bin/mycommand > /usr/local/bin/mycommand:

ln -s /usr/local/bin/mycommand /usr/bin/mycommand

Now for everyone facing a similar problem: there is a reason why a /usr/local/bin exists and there might be risks with my solution. But it's the easiest and fastest and I already spent way to much time on this problem. In my situation all ssh-keys will be limited anyways to one command only so I don't really care.

Anyways: if you want to read more about the difference between those two locations I recommend this post: https://askubuntu.com/a/308048

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.