大网数据广告活动
服务器运维问答

Environment variables are unavailable when running scripts over ssh

· 52CE 我爱测

Suppose I have two hosts: A and B with users a and b. B is Windows machine with Cygwin installed, if this matters. B is configured to be accessed from A through SSH without password (it contains A's public RCA key), so I can run …

问题原文

原帖链接 · Silver · CC BY-SA 3.0

Suppose I have two hosts: A and B with users a and b. B is Windows machine with Cygwin installed, if this matters.

B is configured to be accessed from A through SSH without password (it contains A's public RCA key), so I can run commands on it from A in the following way:

ssh b@B [command]

But when I try to run a command, that requires to access an environment variable, the command fails:

[15:54:08] - a@A - ~
$ ssh b@B 'echo $ANT_HOME'

returns empty string.

The same command, if connected directly (not through SSH) is executed correctly:

b@B ~
$ echo $ANT_HOME
/cygdrive/c/PROGRA~1/apache-ant-1.9.0

The latter is possible because the relevant variables were exported in "bash_profile":

export JAVA_HOME="/cygdrive/c/PROGRA~1/Java/jdk1.7.0_45"
export ANT_HOME="/cygdrive/c/PROGRA~1/apache-ant-1.9.0"

but for some reason this is not applied to the SSH case.


Some additional info that may be useful.

Host B has Windows XP + Cygwin. CYGWIN sshd is run under SYSTEM account. JAVA_HOME and ANT_HOME are defined in "system" environment variables part in Windows.

When I run "$ ssh b@B env" command, it does not print ANT_HOME or JAVA_HOME, but if I include them in sshd_config file, it does. But even then simple command ssh b@B 'echo $ANT_HOME' does not work as I would expect.

Please give me a hint what needs to be done to run the command through SSH.

已采纳解答

原帖链接 · periket2000 · CC BY-SA 4.0

From the man pages of my server:

  1. Reads the file ~/.ssh/environment, if it exists, and users are allowed to change their environment. See the PermitUserEnvironment option in sshd_config(5).

You need to declare the environment variables and allow them in the remote server location /etc/ssh/sshd_config, by enabling the property PermitUserEnvironment as PermitUserEnvironment yes and set your environment variables in ~/.ssh/environment.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2014-04-03)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。