Why Bash unable to find command even if $PATH is specified properly?

SIG*_*EGV 9 linux shell bash ssh path

I am specifying path to my command in the file /etc/profile:

export PATH=$PATH:/usr/app/cpn/bin
Run Code Online (Sandbox Code Playgroud)

My command is located in:

$ which ydisplay 
/usr/app/cpn/bin/ydisplay
Run Code Online (Sandbox Code Playgroud)

So, when I performing "echo $PATH" output is looks like:

$ echo $PATH
...:/usr/app/cpn/bin
Run Code Online (Sandbox Code Playgroud)

And everything is OK, but when I am trying to launch my command via SSH I am getting error:

$ ssh 127.0.0.1 ydisplay
$ bash: ydisplay: command not found
Run Code Online (Sandbox Code Playgroud)

But the my path is still present:

$ ssh 127.0.0.1 echo $PATH
...:/usr/app/cpn/bin
Run Code Online (Sandbox Code Playgroud)

Please explain me why Bash unable to find ydisplay during SSH session and how to properly configurate SSH to avoid this issue.

More over, if I specifying $PATH in local file .bashrc in the current user all works correctly. But I want to modify only one file instead specifying a lot of files for each user. This is why I am asking.

Mik*_*kel 5

tl;博士

运行ssh 127.0.0.1 ydisplay~/.bashrc而不是/etc/profile. ~/.bashrc改为更改您的路径。

细节

唯一/etc/profile读取的时间是您的 shell 是“登录 shell”。

来自Bash 参考手册

当 bash 作为登录 shell 被调用时,...它首先从文件 /etc/profile 读取并执行命令

但是当你运行ssh 127.0.0.1 ydisplay,bash不是作为登录 shell 启动的。但它确实读取了不同的启动文件。该猛砸参考手册说:

当 ... 由 ... sshd 执行时。...它读取并执行来自~/.bashrc

所以你应该把你的PATH设置放在~/.bashrc.

在大多数系统上,~/.bash_profilesource ~/.bashrc,因此您可以仅将设置放入~/.bashrc而不是将它们放入两个文件中。

有更改设置为所有用户没有标准的方法,但大多数系统都有一个/etc/bashrc/etc/bash.bashrc或类似的。

如果失败,请设置pam_env并将PATH设置放入/etc/environment.

也可以看看: