tee << '+++' | ssh user@remoteserver MYVAR1="$MYVAR1" MYVAR2="$MYVAR2" bash
set -x
echo "hey, your variable is $MYVAR1"
echo "and your other variable is $MYVAR2"
+++
environment="NAME=value"
Specifies that the string is to be added to the environment when logging in using this key. Environment variables set this way override other
default environment values. Multiple options of this type are permitted. Environment processing is disabled by default and is controlled via
the PermitUserEnvironment option. This option is automatically disabled if UseLogin is enabled.
检查您的 /etc/ssh/ssh_config (在客户端)并查看 SendEnv 选项。就我而言,我有
SendEnv LANG LC_*
.man ssh_config中有一些有趣的信息
如果您可以控制 ssh 命令调用本身,则可以尝试以下操作:
我还使用“tee”(为了视觉清晰)和 heredoc 来发送远程 bash 脚本:
服务器上该用户的外壳是什么?如果它是 bash,那么包含所有变量(或类似“./etc/environment”的东西)的 ~/.bash_profile 或 ~/.profile 应该可以工作。如果不是 bash,请考虑更改:)
我的猜测是你的环境变量在错误的文件中。有两个不同的文件可用于登录/交互式 shell。
看看这篇文章:
'Login' 和 'Interactive' bash shell 有什么区别
编辑:好的,现在对于非交互式登录:
您是否使用密钥登录?如果是,您可以将其添加到您的
authorized_keys
文件中:(来自人 sshd)
您是否在
~/.bashrc
服务器上定义了环境变量?有时会读一些东西~/.profile
。我做的一个技巧是符号链接~/.profile
到~/.bashrc
,例如:
@Dom(我不能发表评论):对我来说它正在工作
/etc/ssh/sshd_config