ssh 远程执行命令的问题

ssh node6 env | grep -i path
总是得到PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
而正常环境变量是

root@node7:~#  env | grep -i path
LIBRARY_PATH=/usr/local/cuda/lib64/stubs
LD_LIBRARY_PATH=/usr/local/nvidia/lib:/usr/local/nvidia/lib64:/usr/local/openmpi/lib/
PATH=/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/openmpi/bin

ssh远程执行命令属于非交互非登录类型。
参考:https://blog.csdn.net/whitehack/article/details/51705889
本来这种情况下ssh应该是读取~/.bashrc文件的。但是把需要的环境变量放到~/.bashrc中没有得到正确的环境变量。
最后看到:https://stackoverflow.com/questions/940533/how-do-i-set-path-such-that-ssh-userhost-command-works

Bash in non-interactive mode sometimes reads the file ~/.bashrc (which is also often source'd from the interactive scripts.) By "sometimes" I mean that it is distribution-dependent: quite oddly, there is a compile-time option for enabling this. Debian enables the ~/.bashrc reading, while e.g. Arch does not.

As grawity said, ~/.bashrc is what you want, since it is sourced by non-interactive non-login shells.
I expect the problem you're having has to do with the default Ubuntu ~/.bashrc file. It usually starts with something like this:

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

You want to put anything for non-interactive shells before this line.

ssh 远程执行命令的问题_第1张图片

所以最后把变量放到[ -z "$PS1" ] && return前面即可。
如下图所示,可以正确获取所需环境变量


如下图,SP1不一样
image.png

你可能感兴趣的:(ssh 远程执行命令的问题)