15

How can I set env variables so that KDE recognizes them?

How do you set the path in a non-shell environment (KDE)?

Unlike the above two questions, I want to know how to do this for GNOME. Or better yet, is there a method that is independent of the window manager. For the shell/terminal, I usually edit ~/.bashrc. Where can I place export statements so that environment variables are available for all X applications, no matter the window manager used?

12

The pam_env PAM module let's you set them either in /etc/environment or in ~/.pam_environment, depending on whether you want it for all users (system wide), or just your user (session-wide).

System-wide environment variables

Environment variable settings that affect the system as a whole (rather then just a particular user) should not be placed in any of the many system-level scripts that get executed when the system or the desktop session are loaded, but into

/etc/environment - This file is specifically meant for system-wide environment variable settings. It is not a script file, but rather consists of assignment expressions, one per line. Specifically, this file stores the system-wide locale and path settings.

Session-wide environment variables

Environment variable settings that should affect just a particular user (rather then the system as a whole) should be set into:

~/.pam_environment - This file is specifically meant for setting a user's environment. It is not a script file, but rather consists of assignment expressions, one per line.

Note: Using .pam_environment requires a re-login in order to initialize the variables. Restarting just the terminal is not sufficient to be able to use the variables.

See more at Ubuntu's wiki on Environment Variables.

  • 2
    The problem with the ~/.pam_environment solution is that by default the pam_env module doesn't read it, unless the distribution configures it otherwise. On Fedora 20 it's not read for example. – Cristian Ciupitu May 13 '14 at 9:36
  • @CristianCiupitu is right. ~/.xsession will load environment variables for X applications. See unix.stackexchange.com/questions/47359/what-is-xsession-for – Tek Feb 10 '15 at 1:00
  • @CristianCiupitu: Thanks a lot for your comment which lead me to pam_env. I updated my Fedora 24 pam files to enable the user environment. Now my Wayland sessions all have a custom PATH again. – Zan Lynx Aug 17 '16 at 20:06
  • @CristianCiupitu is that true? The documentation linux-pam.org/Linux-PAM-html/sag-pam_env.html implies that ~/.pam_environment is the default file to be read unless otherwise specified. See the line on user_envfile=filename. My problem with pam_env is that the HOME variable may not be available depending on the PAM application. Which limits its use to just absolute variables. – CMCDragonkai Sep 20 '16 at 18:45
  • 2
    @CMCDragonkai, the pam_env(8) man page from Fedora used to say: "By default this option is off as user supplied environment variables in the PAM environment could affect behavior of subsequent modules in the stack without the consent of the system administrator". So while the upstream PAM module has some default settings, some distributions overwrite them. – Cristian Ciupitu Sep 21 '16 at 12:49
1

/etc/X11/Xsession.d/ ... or along the lines of that directory, depending on your Linux(?). I wrote a little on it here.

The concept is that after login, a display manager starts a "Xsession" and sources (!) everything in that directory in alphabetical order. You can simply add a script which modifies PATH. The Xsession will untimately be your DE and usually all processes are children of that Xsession, therefore inheriting the PATH environment.

  • Is there an equivalent that I can set in my $HOME directory? Like .Xsession? – Code-Guru Jul 3 '13 at 20:22
  • According to Xession(5): /etc/X11/Xsession.d/40x11-common_xsessionrc Source global environment variables. This script will source anything in $HOME/.xsessionrc if the file is present. This allows the user to set global environment variables for their X session, such as locale information. – Bananguin Jul 4 '13 at 8:50
1

A process inherits an environment from the parent process that starts it. To change the PATH or other environment values in a child, we can set a variable to a value in the parent, and export the variable if we are in a shell, and then start the child process. The child may also read one or more initialization files to change its own environment as it starts.

So, there are two more questions to get to an answer:

What is the parent/child inheritance tree that leads to the process that you want to be affected by the PATH or environment change?

What initialization files are used/read/sourced by the relevant processes in that tree?

Here is part of the output of ps(1) to show what I mean by an inheritance tree:

# /bin/ps -o 'uid:5,pid:5,ppid:5,user:4,args' axf 

UID   PID  PPID USER COMMAND
  0     1     0 root /usr/lib/systemd/systemd
  0  1481     1 root /usr/sbin/gdm-binary -nodaemon
  0  1497  1481 root  \_ /usr/libexec/gdm-simple-slave ...
  0  1504  1497 root      \_ /usr/bin/Xorg :0 ...
  0  1855  1497 root      \_ gdm-session-worker ...

Note the \_ graphic sequences and the PIP/PPID numbers (Process ID and Parent PID). PID 1855 was started by (some child of?) 1497, which was started by 1481, which was started by the PID 1, which was started by the ancestor process 0.

Do a similar inheritance trace for the process you want to affect, figure out which initialization files are relevant, and make the change to PATH somewhere in that tree, probably with something similar to:

PATH=${PATH}:/usr/local/bin
  • Can I use ps to determine the parent process of a GUI login? – Code-Guru Jul 3 '13 at 20:20

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.