路径因连接容器的方式而异

Path is different depending on how you connect to container

我有一个 Alpine docker 容器,根据我使用 ssh 连接的方式不同,路径也不同。如果我使用 PTY shell:

连接

ssh root@localhost sh -lc env | grep PATH

这会打印:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

但是如果不使用这个shell:

ssh root@localhost sh -c env | grep PATH

这会打印:

PATH=/bin:/usr/bin:/sbin:/usr/sbin

为什么会这样?我需要做什么才能使第二个命令产生与第一个命令相同的输出?

使用 sh -l 开始登录 shell:

When invoked as an interactive login shell, or a non-interactive shell with the --login option, it first attempts to read and execute commands from /etc/profile and ~/.profile, in that order. The --noprofile option may be used to inhibit this behavior.
...
A non-interactive shell invoked with the name sh does not attempt to read any other startup files.

来自https://linux.die.net/man/1/sh

也就是说,您可能可以编辑配置文件,使登录 shell 的行为类似于 noprofile,但反过来可能会变得困难。

我会回答我自己的问题。此堆栈溢出 post 具有所需的主要信息:

鉴于此,有两种选择:

  • 使用 Dockerfile

    ENV 选项声明 PATH
  • 或将PermitUserEnvironment yes添加到sshd_config文件并在~/.ssh/environment

    中定义PATH