Linux远程调用命令失败,ssh执行远程脚本遇到的坑

最新在搭建新的产品环境就遇到了一个坑,使用js 的node-ssh module执行远程机器的sh脚本的时候发现,无法获取全局的环境变量。我们知道linux机器的全局环境变量的设置是在/etc/profile这个文件中的。我在这个文件中添加了JAVA_HOME的环境变量,并将java的bin目录添加到了$PATH中,但是通过ssh执行远程脚本的时候拿到的JAVA_HOME的值是空的,但是如果通过xshell工具ssh到远程主机就可以拿到环境变量,当时不理解是为什么?后台通过查了一些资料才了解大致原因。

原来通过ssh执行远程的命令或者脚本和通过ssh登录到远程主机后在执行脚本这两种方式的bash模式不同。bash工作模式主要分为:login + interactive,login + non-interactive ,non-login + interactive ,non-login + non-interactive 。login 是指需要输入用户名和密码登陆的 shell; interactive 顾名思义为交互式,命令的标准输入与输出为该 bash 绑定到的终端。

* login + interactive

* 登陆 Linux 获取的第一个 shell

* 通过 ssh user_name @ romote_ip 登陆获取到的 shell

* 运行命令 bash -l 进入的 shell

* 首先读取 /etc/profile 文件

* 再从下面三个文件读取到第一个存在的文件

~/.bash_profile、~/.bash_login、~/.profile

etc/profile 会依次读取 /etc/profile.d 下所有文件

* login + non-interactive

* 运行命令 bash -l script.sh

*配置文件读取同上 该模式比较少用

* non-login + interactive

* 运行命令bash

* 读取 /etc/bash.bashrc

* 读取 ~/.bashrc文件

* non-login + non-interactive

* 运行命令 bash script.sh

* 读取环境变量 $BASH_ENV 的值,导入该值的配置文件

通过SSH登录后在执行脚本

这种方式使用的是Bash的interactive + login shell模式。

这种模式下回读取优先读取/etc/profile的配置,所谓我设置的环境变量是能够读取到的。

通过ssh远程执行脚本命令

这个方式使用的是bash的non-login + non-interactive模式。是不会读取/etc/profile中的配置,所以我设置的环境变量是获取不到的,于是我就去之前已经搭建好的机器上查看了$BASH_ENV这个变量的值,发现是空的,这就很奇怪了,为什么这个值是空的ssh远程执行脚本的时候也能成功的拿到JAVA_HOME这个环境变量呢? 后来发现是在~/.bashrc中文件中设置的,但是按照上面的工作模式应该是不会读取这个配置的,于是就又去查了一下资料,从帮助文档中找到了原因:

Bash attempts to determine when it is being run with its standard input connected to a network connection,

as when executed by the remote shell daemon,

usually rshd, or the secure shell daemon sshd.

If bash determines it is being run in this fashion,

it reads and executes commands from ~/.bashrc,

if that file exists and is read‐ able.

大致的意思就是 bash 会判断标准输入是否关联到 sshd 这样的网络链接上,如果是,会读取 ~/.bashrc 配置文件。

所以要解决SSH远程执行命令时找不到自定义环境变量的问题,那么可以在登录用户的HOME目录的.bashrc中添加需要的环境变量。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值