This is quite complex.
Pam also can add environment variables, as well as other things (login
program, getty
, whatever...).
What I used to do is to add lines of the style (see this)
export CALLED_IN_ETC_PROFILE=$(date date +%s.%N)
in all suspicious files, and then look at the result for example using https://askubuntu.com/a/356973/16395.
To find which program/file could be responsible of the setting of a variable, you can try to look at the process tree of a shell into a terminal emulator:
[romano:/etc/X11/Xsession.d] % pstree -a -s -l -p -u $$
systemd,1 splash
└─gdm3,1926
└─gdm-session-wor,3752
└─gdm-x-session,3807,romano --run-script gnome-session --session=gnome
└─upstart,3824 --user
└─gnome-terminal-,5027
└─zsh,11224
└─pstree,11831 -a -s -l -p -u 11224
...I think that to start the graphic session, gdm-x-session
is started from a user shell (note the UID change in the chain) in non-interactive mode, so that .profile
is read (but not .bashrc
, which is read by the shell in terminal emulator).
The tree is somewhat different if called from the gnome-shell
:
bash-4.3$ pstree -a -s -l -p -u $$
systemd,1 splash
└─gdm3,1926
└─gdm-session-wor,3752
└─gdm-x-session,3807,romano --run-script gnome-session --session=gnome
└─upstart,3824 --user
└─gnome-session-b,4162 --session=gnome
└─gnome-shell,4278
└─xterm,11882 -e bash --noprofile --norc
└─bash,11884 --noprofile --norc
└─pstree,11885 -a -s -l -p -u 11884
but this is expected.
BTW, a lot of environment variables could also be set before the UID change to the user; there are a lot of scripts in /etc/X11/Xsession.d
and around that the X
server, gdm
(or whichever display manager you use), that can set environment variables.
/usr/sbin/lightdm-session
file is part of the answer. There you can see that for instance~/.profile
is sourced when LightDM starts a user session.