Env vars in /etc/environment not globally visible?

/etc/environment is not a global configuration file.

It applies only on Linux; only to PAM sessions, as employed by login and so forth; and only in the case where a particular PAM plug-in is installed and enabled on the system, and where that PAM plug-in has not been configured to use some other file (because /etc/environment is merely its default if it is not told otherwise). It is that PAM plug-in that reads it.

Otherwise, it is just a meaningless file in /etc that nothing uses.

Whilst you could set environment variables globally in systemd-system.conf, this applies to all services, not just to the ones that you are wanting to change.

Similarly, whilst an EnvironmentFile in the appropriate service unit(s) is a way to have systemd read a common file with a list of environment variable definitions, the systemd people discourage its use. They regard EnvironmentFile as a bad idea, and its use as a mistake.

The proper way to do this per the systemd people is to set each variable in the service unit with an Environment setting, which contains the actual environment variable name and value to be set, not a filename. This can either be directly in the relevant service unit, if as here one is writing one's own service unit to be placed in /etc; or be in a .conf override file, if one is adjusting a pre-packaged service unit in /lib or /usr/lib.

Further reading

  • https://unix.stackexchange.com/a/419061/5132
  • Lennart Poettering et al. (2016). systemd-system.conf. systemd manual pages. Freedesktop.org.
  • Lennart Poettering et al. (2016). systemd.exec. systemd manual pages. Freedesktop.org.
  • Andrew G. Morgan and Thorsten Kukuk (August 2010). "pam_env — set/unset environment variables". The Linux-PAM System Administrators' Guide. linux-pam.org.

The following offered to provide clarity on what /etc/environment is/does, when its' consulted and what package provides it:

whereis /etc/environment
environment: /usr/lib/environment.d /etc/environment

Feeding the results of whereis above, we confirm /etc/environment is a SystemD file:

sudo dpkg -S /usr/lib/environment.d
systemd, snapd: /usr/lib/environment.d

Consulting man environment.d we are told the purpose of /etc/environment:

"The environment.d directories contain a list of "global" environment variable assignments for the user environment. systemd-environment-d-generator(8) parses them and updates the environment exported by the systemd user instance to the services it starts.... For backwards compatibility, a symlink to /etc/environment is installed, so this file is also parsed."

Hope this cleared up any confusion. I know I was scratching my head a bit initially before I dug a bit deeper... ;-)