How to reload /etc/environment without rebooting?

118

Solution 1

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'}"
()

Solution 2

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

  • Option 3: with the allexport bash option (thanks @ulidtko):

set -a; source /etc/environment; set +a;

Share:
118

Related videos on Youtube

Imran H
Author by

Imran H

Updated on September 18, 2022

Comments

  • Imran H
    Imran H 8 months

    I have a jasper report which has 12 columns. 10 of these columns were defined with width 160 and other 2 with width 480. While the report could be opened in PDF and excel successfully, it could not export in docx. I decreased the width. Made each field as 100 and bigger 2 as 200. But very shockingly this has resulted in lesser number of rows being resulted. This is completely baffling to me. Any help on this would be appreciated.

    Thanks

    • HikeMike
      HikeMike over 11 years
      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.
    • badboy24
      badboy24 about 9 years
      Works for me without an export...
    • Gert van den Berg
      Gert van den Berg over 8 years
      @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)
    • Petter Friberg
      Petter Friberg over 7 years
      Its probably wrapping the text (hence shorter columns, text goes to new line, record take more space...) being resulted?.. what means? I interpreted as less rows on page... do you mean rows missing??
    • Imran H
      Imran H over 7 years
      Missing rows is what I meant. Sorry for not explaining earlier. Number of rows in excel decreased from 1100+ to somewhere around 900.
    • Petter Friberg
      Petter Friberg over 7 years
      This seems very strange... can you post some relevant jrxml code, surely some other issue than column size.. isPrintRepeatedValues="false"
    • Imran H
      Imran H over 7 years
      I figured out what was wrong. I found out there were lesser number of rows in excel but in PDF I could see blank rows. Culprit was 'Position Type' set to 'Fix Relative to Bottom' and 'Stretch Type' set to 'Relative to Tallest Object'. Changing these two settings fixed the issue. It was missing rows in excel which completely took me by surprise. Thanks for your inputs.
    • Petter Friberg
      Petter Friberg over 7 years
      Great : ) ...consider posting answer (accept it) or delete question... so that this can be closed.
    • Gadelkareem
      Gadelkareem over 7 years
      @DanielBeck thank you..plz add you comment as an answer!
    • Massimo
      Massimo almost 6 years
      does it works also on debian and centos ?
    • Massimo
      Massimo almost 6 years
      does it require user login or its settings are used also by daemons and cron jobs ?
    • Nicholas Saunders
      Nicholas Saunders over 2 years
      I'm just curious why all the blog posts I see also suggest to use source /etc/environment when it doesn't work (for me)
  • Frank
    Frank almost 7 years
    I just tested while read -r env; do export "$env"; done on Ubuntu 16.04 and it errors. Would you like more details?
  • villasv
    villasv almost 6 years
    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")
  • Aaron McMillin
    Aaron McMillin almost 6 years
    Shorter: eval sed 's/^/export /' /etc/environment
  • Tim Jones
    Tim Jones about 5 years
    Non-temp-file version of above: for env in $( cat /etc/environment ); do export $(echo $env | sed -e 's/"//g'); done
  • Massey101
    Massey101 about 5 years
    Even shorter: set -a; source /etc/environment; set +a;.
  • Tobia
    Tobia about 3 years
    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