The use of /etc/profile.d
is confusing to me.
I can put a file in there for example with contents
export MYVAR=MYVALUE
and when I logon the the machine using SSH MYVAR
has a value.
This machine is a Ubuntu desktop, when I logon using ordinary login and start a terminal MYVAR
has no value.
I don’t understand why this should be the case. Linux documentation I think states that these files are used when I shell is started.
When I start /bin/bash
in the terminal window MYVAR
is also empty.
This does not make sense. Why is this directory only used on SSH login?
Advertisement
Answer
To understand what’s going on here, you need to understand a little background information about how shells (bash in this case) are run.
Traditionally, the place to define per-user environment variables on unix systems is ~/.profile. This file is read by the login shell (i.e. the program that is started when you log in, and that you can type commands into), provided that the login shell is a Bourne-compatible shell.
Bash is a Bourne-compatible shell. When it is invoked as a login shell reads ~/.bash_profile if this file exists, and ~/.profile if ~/.bash_profile doesn’t exist.
As a rule of thumb, if you type your password in text mode (e.g., on a text console, or remotely with ssh), then the shell you get is a login shell.
However, if you type your password in a graphical program and get logged into a graphical environment, this bypasses the normal login shell. Whether .profile is read in this case depends on how the graphical session is set up; for instance it varies between Linux distributions, between display managers and between desktop environments. Sometimes one of the programs in the chain explicitly invokes a login shell; sometimes one of the proPleagrams explicitly reads ~/.profile; and sometimes none of this happens and ~/.profile doesn’t get read.
You can try to set environment variables in ~/.bashrc or /etc/bashrc