2

Is there any namespace (process, user, etc) that isolates environment variables?

In case there is, what happens when you unshare? Do they get cloned? What happens if a process changes an existing variable?

EDIT: I am using Ubuntu

4

You don't need any namespace to isolate environment variables. They're already isolated and private to each process. Modifying an environment variable in a child process will NOT propagate to its parent.

Environment variables in Unix are just a bunch of strings stored in the address space of a process. When a process forks, its children will "inherit" a copy of them together with the whole address space, and when a program executes another (and replaces the entire content of the address space), they have to be passed explicitly as the envp argument to the execve(2) system call if they're to be preserved.

If you want to start a process with an empty environment, you can start it as

env - cmd ...

which will just call execve("/path/to/cmd", ["cmd", ..., NULL], [NULL]).

Or to run it with an environment containing just FOO=bar:

env - FOO=bar cmd ...

Both the namespace affecting system calls (clone(2), unshare(2), setns(2)) and command line utilities like unshare(1) do not consider or affect the environment in any special way.


Note: This is how it works in Unix, not some law of nature. On other systems like plan9 (where the concept the namespace originated, but where all namespaces are basically mount namespaces, since everything there is a file/system) the environment is just a filesystem mounted by default on /env and, contrary to Unix, shared by default between the parent and the child.

3
  • Is this the same in Linux? Every time I start a process all that data is copied to the process memory? Thanks! Apr 1 '20 at 7:11
  • I think I got it now, the initial variables, which i believed they were "global" (like in windows) they are actually not, and they are just written in some file. Thank you for your answer! Apr 1 '20 at 7:44
  • 1. For all intents and purposes, Linux is Unix. 2. Yes all that data is copied, see here for some low level details. You may think of the environment strings as of "hidden" command line arguments -- which are automatically added to each command for your convenience.
    – mosvy
    Apr 1 '20 at 14:33
1

Environment variables in bash are only scoped to a particular instance of the shell, so there is no need to define separate namespaces for them.

You can see this clearly if you open two terminal windows at once (in a Desktop Environment). Create a new environment variable in one of them, e.g.:

TEST_VAR=34

It won't appear in the bash session in the other terminal window.

5
  • It is not bash specific. Mar 31 '20 at 18:52
  • @ctrl-alt-delor the OP has applied the tag 'bash'
    – Time4Tea
    Mar 31 '20 at 19:56
  • Yes I noticed. However it is still not specific to bash. Apr 1 '20 at 6:39
  • Thanks Both, this is very useful for me and helps me to understand - When I start a new process, where are all those variables coming from? Apr 1 '20 at 7:12
  • I think I got it now, the initial variables, which i believed they were "global" (like in windows) they are actually not, and they are just written in some file Apr 1 '20 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.