1

I have an Alpine docker container and depending on how I connect using ssh the path is different. If I connect using a PTY shell:

ssh root@localhost sh -lc env | grep PATH

this prints:

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

However if don't use this shell:

ssh root@localhost sh -c env | grep PATH

this prints:

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

Why is this happening? What do I need to do so that the second command produces the same output as the first command?

Carlos Granados
  • 11,273
  • 1
  • 38
  • 44
  • If you are really ssh'ing to a container, then the value of `$PATH` is not your biggest problem, really. https://jpetazzo.github.io/2014/06/23/docker-ssh-considered-evil/ – β.εηοιτ.βε Mar 29 '21 at 21:45
  • 1
    @CarlosGranados what alpine image exactly are you using? I think SSH is not part of a default alpine image. Actually I don't understand why people always want to SSH into containers. Why not use `docker exec`? – dpr Mar 30 '21 at 07:32
  • @dpr and the other person with an impossible name: I am not ssh'ing by choice, it is a tool that is doing it and I am trying to fix that tool. – Carlos Granados Mar 31 '21 at 06:03

2 Answers2

1

With sh -l you start a login 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.

From https://linux.die.net/man/1/sh

That is you can probably edit the profile files to make the login shell behave similar to noprofile but it might become difficult the other way around.

dpr
  • 10,591
  • 3
  • 41
  • 71
1

I'll answer my own question. This stack overflow post has the main info needed: Where to set system default environment variables in Alpine linux?

Given that, there are two alternatives:

  • Declare PATH using the ENV option of the Dockerfile

  • Or add PermitUserEnvironment yes to sshd_config file and define PATH in ~/.ssh/environment

Carlos Granados
  • 11,273
  • 1
  • 38
  • 44
  • How does this explain the difference between the different `sh` options? – dpr Mar 31 '21 at 08:50
  • If you pass the -l (or --login) option it will read the /etc/profile file. If you don't, it won't read it and you need to use one of the other alternatives I posted – Carlos Granados Apr 01 '21 at 09:03
  • Ok, I thought you mentioned that no `/etc/profile` was present in the image... – dpr Apr 01 '21 at 12:51