英文:
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-A
和HOST-B
是基于alpine:3.17.3
的Docker容器。- 端口
13012
是HOST-A
的Docker端口 (13012:22
)。 - 端口
13022
是HOST-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
andHOST-B
are docker containers based onalpine:3.17.3
.
- Port
13012
is the docker port ofHOST-A
(13012:22
) - Port
13022
is the docker port ofHOST-B
(13022:22
)
Test, that works like a charm:
- When I log in to
HOST-A
using dockerexec -it HOST-A /bin/bash
then the script sees the environment variables. - When I log in to
HOST-A
usingssh -p 13012 root@localhost
then the script sees the environment variables. Port13012
is the SSH port of theHOST-A
that Docker maps to the host machine. - When I log in to
HOST-B
, thenssh 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 "password" \
ssh -oStrictHostKeyChecking=no root@HOST-A 'bash -s < /hello.sh param'
When I try to run the script from the docker host machine using sshpass -p "password" ssh -p 13012 -oStrictHostKeyChecking=no "root@localhost" 'bash -s < /hello.sh param'
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论