177

Can I have certain settings that are universal for all my users?

4
  • 2
    Is it EXPORT? append that and it is global?
    – TIMEX
    Oct 29, 2009 at 3:38
  • 2
    i think it is only append on current session , when you open new session in terminal it would be disappeared Mar 19, 2019 at 9:24
  • Yes, export is only for current session, but I didn't have good results on adding my variable to /etc/environment file. That's weird the only environment variable I have there is PATH but its content is different from the PATH that I see when I list all the variables via env
    – Maf
    Sep 22, 2021 at 13:10
  • I was missing to run source /etc/environment
    – Maf
    Sep 22, 2021 at 13:15

7 Answers 7

127

As well as /etc/profile which others have mentioned, some Linux systems now use a directory /etc/profile.d/; any .sh files in there will be sourced by /etc/profile. It's slightly neater to keep your custom environment stuff in these files than to just edit /etc/profile.

5
  • 5
    What if some users use another shell, like zsh? Jul 18, 2013 at 7:41
  • 99
    this is not global ... it is restricted to the shell.. too bad it is the most accepted answer
    – mjs
    Apr 21, 2014 at 11:08
  • 1
    zsh will source .sh files in /etc/profile.d/, you can see it from /etc/zshrc @Matthieu Napoli
    – Bily
    Jul 24, 2014 at 8:45
  • 7
    Scripts are not used for ROOT user.
    – Val
    Jul 26, 2014 at 7:36
  • @Bily, this is not correct for all systems. For example on my Ubuntu 21.04 there is no /etc/zshrc and the global config at /etc/zsh/zshrc does not source stuff from /etc/profile.d/
    – trallnag
    Oct 22, 2021 at 9:38
113

If your LinuxOS has this file:

/etc/environment

You can use it to permanently set environmental variables for all users.

Extracted from: http://www.sysadmit.com/2016/04/linux-variables-de-entorno-permanentes.html

1
  • Works for me too on Kubuntu
    – xeruf
    Jun 29, 2018 at 16:34
40

man 8 pam_env

man 5 pam_env.conf

If all login services use PAM, and all login services have session required pam_env.so in their respective /etc/pam.d/* configuration files, then all login sessions will have some environment variables set as specified in pam_env's configuration file.

On most modern Linux distributions, this is all there by default -- just add your desired global environment variables to /etc/security/pam_env.conf.

This works regardless of the user's shell, and works for graphical logins too (if xdm/kdm/gdm/entrance/… is set up like this).

3
  • 1
    +1 you also need to reboot after adding a variable in pam_env.conf cause instantly on the fly $ echo $variablename does not show
    – user285594
    Dec 16, 2013 at 9:42
  • 10
    You don't need to reboot, you need to relog-in. (No rebooting critical servers for me) Aug 1, 2014 at 6:58
  • This doesn't seem to apply to my distro, Mint 17.2. The preinstalled file is entirely commented out, and if I add something like echo foo>/home/me/bar and reboot (and log in again), that file doesn't get created. (There's probably a more elegant way to test this, but I wanted to be sure before commenting here.) Jul 23, 2015 at 1:34
17

Amazingly, Unix and Linux do not actually have a place to set global environment variables. The best you can do is arrange for any specific shell to have a site-specific initialization.

If you put it in /etc/profile, that will take care of things for most posix-compatible shell users. This is probably "good enough" for non-critical purposes.

But anyone with a csh or tcsh shell won't see it, and I don't believe csh has a global initialization file.

2
  • 9
    /etc/environment actually seems to set environment variables at for me, even as the root user. I know that /etc/environment is on amazon linux and ubuntu. I didn't get to check if works for csh, tcsh, or zsh. May 4, 2017 at 12:10
  • it is not specific to the shell you are using, it is truly global
    – xeruf
    Jun 29, 2018 at 16:34
14

Some interesting excerpts from the bash manpage:

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
...
When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of /etc/bash.bashrc and ~/.bashrc.

So have a look at /etc/profile or /etc/bash.bashrc, these files are the right places for global settings. Put something like this in them to set up an environement variable:

export MY_VAR=xxx
3
  • Note that on classic Unix systems, both Bourne shell and Korn shell also read /etc/profile - it is probably the mostly widely used location for system environment setting. Some versions of the C shell look in /etc/csh.cshrc and /etc/csh.login as well as per-user locations; others do not use any system environment setting file. Oct 29, 2009 at 4:55
  • Awesome, just what I was looking for as far as setting the env for non-login shell users.
    – David Mann
    Mar 3, 2014 at 20:50
  • Sorry, I seem to have accidentally downvoted this when for a while the arrows weren't visible... would you mind editing this (typo "environEment" in the second to last line, for example) so I can remove the downvote? Nov 2, 2018 at 14:40
5

Every process running under the Linux kernel receives its own, unique environment that it inherits from its parent. In this case, the parent will be either a shell itself (spawning a sub shell), or the 'login' program (on a typical system).

As each process' environment is protected, there is no way to 'inject' an environmental variable to every running process, so even if you modify the default shell .rc / profile, it won't go into effect until each process exits and reloads its start up settings.

Look in /etc/ to modify the default start up variables for any particular shell. Just realize that users can (and often do) change them in their individual settings.

Unix is designed to obey the user, within limits.

NB: Bash is not the only shell on your system. Pay careful attention to what the /bin/sh symbolic link actually points to. On many systems, this could actually be dash which is (by default, with no special invocation) POSIXLY correct. Therefore, you should take care to modify both defaults, or scripts that start with /bin/sh will not inherit your global defaults. Similarly, take care to avoid syntax that only bash understands when editing both, aka avoiding bashisms.

4

Using PAM is execellent.

# modify the display PAM
$ cat /etc/security/pam_env.conf 
# BEFORE: $ export DISPLAY=:0.0 && python /var/tmp/myproject/click.py &
# AFTER : $ python $abc/click.py &
DISPLAY  DEFAULT=${REMOTEHOST}:0.0 OVERRIDE=${DISPLAY}
abc   DEFAULT=/var/tmp/myproject

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.