/etc/environment is officially the correct place for system wide environment variables. But how can I reload the assignments in this file without rebooting or relogging?

Interestingly enough google does not help me here, aside from the dozens of blog posts suggesting to use

source /etc/environment

which obviously will never work because /etc/environment is a list of assigments (one per line) and not an executable script (hence the missing export commands in /etc/environment ...).

  • 13
    Systemwide, I have no idea. For the current shell session, you can use for line in $( cat /etc/environment ) ; do export $line ; done, if the file format is key=value. – Daniel Beck Sep 25 '11 at 11:38
  • 3
    Works for me without an export... – Izkata Mar 30 '14 at 14:44
  • 3
    @lzkata: If a variable is already exported, it should not be necessary to export it again... If new variable that have not already been exported are added, the export would be necessary... (without it it is just a normal non-environment variable in the current shell, not available in child processes) – Gert van den Berg Sep 1 '14 at 10:16
  • @DanielBeck thank you..plz add you comment as an answer! – Gadelkareem Dec 3 '15 at 19:17
  • does it works also on debian and centos ? – Massimo Jul 10 '17 at 23:28

One thing you are mistaken about is that /etc/environment requires a reboot to reload. This is incorrect. The only time the file is read is on login, when the PAM stack is activated – specifically pam_env.so, which reads the file.

Logging out and back in would apply the changes – and in fact you must do this if you want all your processes to receive the new environment. All other "solutions"2 will only apply the environment to the single shell process, but not to anything you launch through the GUI including new terminal windows.1

If you're fine with that, though – the lack of export commands can be compensated for with set -a and set +a. However, it still remains a poor way, as the file doesn't use quoting either. But this should work fine:

while read -r env; do export "$env"; done

1 GNOME Session Manager provides a way to change its own environment, but only during the Initialization phase:

$ gdbus call -e -d org.gnome.SessionManager \
                -o /org/gnome/SessionManager \
                -m org.gnome.SessionManager.Setenv \
                "FOO" "bar"
Error: GDBus.Error:org.gnome.SessionManager.NotInInitialization: Setenv
    interface is only available during the Initialization phase

2 gdb is not a solution, but can be used sometimes. You have to attach it to the running processes of your session manager (e.g. gnome-session), your window manager (e.g. gnome-shell or openbox), your taskbar/panel if any (e.g. xfce4-panel), and generally anything else that possibly would run stuff. For each of those processes, you need to attach gdb to it by PID, invoke the putenv() function using p, then detach using q:

$ sudo gdb -p $(pidof gnome-session)
GNU gdb (GDB) 7.7.1
Attaching to process 718
0x00007fc2cefed81d in poll () from /usr/lib/libc.so.6

(gdb) p putenv("FOO=bar")
$1 = 0

(gdb) p putenv("BAZ=qux")
$2 = 0

(gdb) q
A debugging session is active.
Quit anyway? (y or n) y
Detaching from program: /usr/bin/gnome-session, process 718

Note that the debugger pauses the process, therefore you must attach to compositing window managers only from another tty (virtual console) or over SSH, otherwise the screen would freeze.

In addition to that, you should also update the environment used by dbus-daemon:

$ dbus-update-activation-environment --systemd FOO=bar BAZ=qux

For older systems:

$ gdbus call -e -d org.freedesktop.DBus \
                -o /org/freedesktop/DBus \
                -m org.freedesktop.DBus.UpdateActivationEnvironment \
                "{'FOO': 'bar', 'BAZ': 'qux'}"
  • 1
    I just tested while read -r env; do export "$env"; done on Ubuntu 16.04 and it errors. Would you like more details? – Frank Jul 30 '16 at 7:41
  • The while read method reads from stdin, so you probably want to pipe something to it (e.g. cat /etc/environment | while ...). But this won't work with quote formatted environment files (key="value") – villasv Aug 14 '17 at 20:06
  • Thank you! I've been looking for (and trying to come up with) an elegant solution like your read -r for a while, to parse generic "env files" like those used with Docker etc. Here is a more complete example, which skips blank lines and lines starting with # (as is common in env files nowadays) and exits with an error if a line is malformed: while read -r pair; do if [[ $pair == ?* && $pair != \#* ]]; then export "$pair" || exit 2; fi; done < SOME_ENV_FILE – Tobia Mar 18 '20 at 11:12

This is not as comprehensive as the accepted answer, but if you have quoted variables in your /etc/environment files both previous methods (which use export $line somehow) will go wrong and you end up with quoted values.

  • Option 1: with a temporary source-able file

sed 's/^/export /' /etc/environment > /tmp/env.sh && source /tmp/env.sh

  • Option 2: with inline for (thanks @tim-jones):

for env in $( cat /etc/environment ); do export $(echo $env | sed -e 's/"//g'); done

  • 4
    Shorter: eval sed 's/^/export /' /etc/environment – Aaron McMillin Aug 23 '17 at 17:59
  • 2
    Non-temp-file version of above: for env in $( cat /etc/environment ); do export $(echo $env | sed -e 's/"//g'); done – Tim Jones Mar 16 '18 at 22:27
  • 8
    Even shorter: set -a; source /etc/environment; set +a;. – ulidtko Apr 20 '18 at 9:03

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.