Carlos Granados
Carlos Granados

Reputation: 11351

Path is different depending on how you connect to container

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?

Upvotes: 1

Views: 622

Answers (2)

Carlos Granados
Carlos Granados

Reputation: 11351

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

Upvotes: 1

dpr
dpr

Reputation: 10972

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.

Upvotes: 1

Related Questions