29

I know that system wide environment variables can be set by adding entries into

/etc/environment

or

/etc/profile

But that requires system restart or X restart.

Is it possible to set an environment variable in Ubuntu / Linux so that immediately available system wide without restarting OS or logging out the user?

4
  • X restart is enough, why would you want to do a system restart? Commented Dec 30, 2011 at 10:42
  • Well, I don't want to do a system restart. Thats the whole point. Commented Dec 31, 2011 at 12:43
  • 1
    This means you cannot do what you ask, then: you will at least have to restart X. You cannot change the environment of an already running process. Commented Dec 31, 2011 at 12:45
  • 1
    @fge actually, you can but it requires you to attach to the process with gdb. It works but it's very hackish Commented Aug 11, 2012 at 3:39

3 Answers 3

19

The simple answer is: you cannot do this in general.

Why can there be no general solution?

The "why?" needs a more detailed explanation. In Linux, the environment is process-specific. Each process environment is stored in a special memory area allocated exclusively for this process.

As an aside: To quickly inspect the environment of a process, have a look at /proc/<pid>/env (or try /proc/self/env for the environment of the currently running process, such as your shell).

When a ("parent") process starts another ("child") process (via fork(2)), the environment the environment of the parent is copied to produce the environment of the child. There is no inheritance-style association between those two environments thereafter, they are completely separate. So there is no "global" or "master" environment we could change, to achieve what you want.

Why not simply change the per-process environment of all running processes? The memory area for the environment is in a well-defined location (basically right before the memory allocated for the stack), so you can't easily extend it, without corrupting other critical memory areas of the process.

Possible half-solutions for special cases

That said, one can imagine several special cases where you could indeed achieve what you want.

  • Most obviously, if you do "size-neutral" changes, you could conceivable patch up all environments of all processes. For example, replace every USER=foo environment variable (if present), with USER=bar. A rather special case, I fear.

  • If you don't really need to change the environments of all processes, but only of a class of well-known ones, more creative approaches might be possible. Vorsprung's answer is an impressive demonstration of doing exactly this with only Bash processes.

There are probably many other special cases, where there is a possible solution. But as explained above: no solution for the general case.

Sign up to request clarification or add additional context in comments.

Comments

14
+50

This perl program uses gdb to change the USER variable in all currently running bash shells to whatever is given as the program arg. To set a new variable the internal bash call "set_if_not" could be used

#!/usr/bin/perl

use strict;
use warnings;

my @pids = qx(ps -C bash -o pid=);
my $foo  = $ARGV[0];
print "changing user to $foo";
print @pids;

open( my $gdb, "|gdb" ) || die "$! gdb";
select($gdb);
$|++;
for my $pid ( @pids ) {
    print "attach $pid\n";
    sleep 1;
    print 'call bind_variable("USER","' . $foo . '",0)' . "\n";
    sleep 1;
    print "detach\n";
}

This only works with bash ( I only tested it with version 4.1 on Ubuntu 10.04 LTS) and does not alter the environment for arbitrary already running programs. Obviously it must be run as root.

12 Comments

Why root is needed for this?
@osgx the implication is that the variable must be changed in all shells running on the system. To alter processes for all users, the obvious thing to do is to be root
Definitely +1! It could be simplified a little bit. ps -C bash -o pid= could be used to eliminate the PID header row. Then @pid could be used instead of @pids[ 1 .. $#pids ].
@TrueY or I could shift(@pids) tmtowtdi :)
Sure! But not to write the header file is more effective. And it takes only one extra =.
|
2

I fear the solution here is a frustrating one: Don't use environment variables. Instead use a file.

So, instead of setting up /etc/environment with:

SPECIAL_VAR='some/path/I/want/later/'

And calling it with:

$SPECIAL_VAR

Instead, create a file at ~/.yourvars with the content:

SPECIAL_VAR='some/path/I/want/later/'

And source the thing every time you need the variable:

cd `source ~/.yourvars; echo $SPECIAL_VAR`

A hack? Perhaps. But it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.