bash配置文件在使用SSH运行远程脚本时未加载。

huangapple go评论56阅读模式
英文:

bash profile is not loaded while running a remote script with ssh

问题

情况:

HOST-A上有一个名为hello.sh的bash脚本。此脚本使用了在HOST-A上可用的一些环境变量。我从HOST-B上执行此脚本。

当我在HOST-A上本地执行hello.sh时,它能够访问环境变量。但是如果我使用ssh从HOST-B上运行脚本,脚本就无法访问这些环境变量。

环境:

  • HOST-AHOST-B 是基于 alpine:3.17.3 的Docker容器。
  • 端口 13012HOST-A 的Docker端口 (13012:22)。
  • 端口 13022HOST-B 的Docker端口 (13022:22)。

测试,完全正常工作:

  • 当我使用docker的 exec -it HOST-A /bin/bash 登录到 HOST-A 时,脚本能够访问环境变量。
  • 当我使用 ssh -p 13012 root@localhost 登录到 HOST-A 时,脚本也能访问环境变量。端口 13012 是Docker映射到主机机器的 HOST-A 的SSH端口。
  • 当我登录到 HOST-B,然后 ssh HOST-A,然后运行脚本时,脚本也能访问环境变量。在这里,我也测试了Docker中主机之间的ssh连接。

问题:

当我从HOST-B远程运行脚本时,使用以下命令,bash配置文件没有设置:

[root@HOST-B]# sshpass -p "password" \
                  ssh -oStrictHostKeyChecking=no root@HOST-A 'bash -s < /hello.sh param'

当我尝试在Docker主机上使用以下命令运行脚本时,也不起作用,bash配置文件没有设置:

sshpass -p "password" ssh -p 13012 -oStrictHostKeyChecking=no "root@localhost" 'bash -s < /hello.sh param'

警告:
param 必须是一个变量:$param

我认为问题在于使用ssh连接时没有设置bash配置文件。

我尝试过但没有起作用的方法:

我创建了 .bash_profile.bashrc.profile。它们的内容完全相同:

# pwd
/root

# ls -all
-rw-r--r--    1 root     root            86 May 14 23:52 .bash_profile
-rw-r--r--    1 root     root            86 May 14 22:38 .bashrc
-rw-r--r--    1 root     root            86 May 14 22:52 .profile

如何在alpine Linux中正确设置ssh远程执行的bash配置文件?

☆☆☆ 更新 ☆☆☆

如果我在hello.sh 中使用 source ~/.bashrc,那么一切都能正常工作。但这看起来不太好。

英文:

Situation:

There is a bash script called hello.sh on HOST-A. This script uses some environment variables that are available on HOST-A. I am executing this script from HOST-B.

When I execute the hello.sh locally from HOST-A it sees the environment variables.
But if I run the script from HOST-B using ssh, the script does not see the environment variables.

Environment:

  • HOST-A and HOST-B are docker containers based on alpine:3.17.3.
  • Port 13012 is the docker port of HOST-A (13012:22)
  • Port 13022 is the docker port of HOST-B (13022:22)

Test, that works like a charm:

  • When I log in to HOST-A using docker exec -it HOST-A /bin/bash then the script sees the environment variables.
  • When I log in to HOST-A using ssh -p 13012 root@localhost then the script sees the environment variables. Port 13012 is the SSH port of the HOST-A that Docker maps to the host machine.
  • When I log in to HOST-B, then ssh HOST-A, then run the script, the script sees the environment variables. Here I test the ssh connection between the hosts in Docker too.

Issue:

The bash profile is not set when I run the script remotely from HOST-B using the

[root@HOST-B]# sshpass -p &quot;password&quot; \
                  ssh -oStrictHostKeyChecking=no root@HOST-A &#39;bash -s &lt; /hello.sh param&#39;

When I try to run the script from the docker host machine using sshpass -p &quot;password&quot; ssh -p 13012 -oStrictHostKeyChecking=no &quot;root@localhost&quot; &#39;bash -s &lt; /hello.sh param&#39; then it also does not work, the bash profile is not set.

Warning:
The param must be a variable: $param

I think that the issue is that the bash profile is not set when I connect with ssh.

What I have tried but did not work:

I created .bash_profile, .bashrc, and .profile. They have exactly the same content:

# pwd
/root

# ls -all
-rw-r--r--    1 root     root            86 May 14 23:52 .bash_profile
-rw-r--r--    1 root     root            86 May 14 22:38 .bashrc
-rw-r--r--    1 root     root            86 May 14 22:52 .profile

How to set bash profile properly for ssh remote execution in alpine Linux?

☆☆☆ UPDATE ☆☆☆

If I source the .bashrc in the hello.sh then everything works fine. But this is a hack, looks really bad.

source ~/.bashrc

答案1

得分: 1

要让 .bash_profile 生效,需要在调用 bash 时指定 -l 选项。这将使 bash 成为登录 shell。在这种情况下,其他登录启动文件也会被处理(如在 bash 手册的 INVOCATION 部分中指定的那样),例如 /etc/profile

英文:

To have .bash_profile being processed, you need to specify the -l option when invoking bash. This makes bash a login shell. In this case, the other login-startup-files are processed too (as specified in the section INVOCATION of the bash man-page), for instance /etc/profile.

huangapple
  • 本文由 发表于 2023年5月15日 08:14:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定