11

I'd like to set a couple of environment variables (at boot time) on an Ubuntu machine (10.04), but I want to create their value via a script, much like:

export THE_ENV_VAR=$(script_to_execute_and_use_stdout_from)
  • The reason I want to do this is that the commands used to set the environment variables take long time to execute (10s or so), so that's why I only want to do it once after boot. Anyway, currently I solved this by caching the result of the command at boot time, so that subsequent calls will go fast, thus not using env. variables at all.

I've tried setting in /etc/environment, but that only copies rhs verbatim

I've tried executing a script in /etc/init.d/ at startup, but that does not seem to work.

Ideas?

0

4 Answers 4

11

To set system-wide environmental variables that are available on boot you place them in /etc/environment. This file is not a script file, but rather consists of assignment expressions, one per line. Like this:

SYSTEM_FOO=barnone

Note: Variable expansion does not work in /etc/environment.

Original information from help.ubuntu.com

3
  • it won't work if the value of the enviroment key is "#" and there is no way to scape it; I test it on debian May 20, 2020 at 7:16
  • Thank you so much. In my case I just have a normal string. it works Mar 25, 2023 at 10:02
  • FYI this only apply for pam-sessions not e.g. systemd daemons (units/timers), it all depends of one mean by "system-wide"
    – MrCalvin
    Apr 7, 2023 at 6:17
5

Put that in ~/.bash_profile or ~/.bashrc

gedit ~/.bashrc

Use .bash_profile if you need to use those variable with non-interactive shells (scripts) and ~/.bashrc if you are setting your variables in a shell.

If that does not work, we need more details, in particular what variables are you setting and for what purpose ?

1
  • 18
    This is not at boot time...
    – user877329
    Oct 22, 2014 at 9:53
1

While this isn't likely a good practice, you can specify environment variables as kernel parameters. The Linux kernel documentation has some good information around it (important paragraph bolded):

The argument list

The kernel command line is parsed into a list of strings (boot arguments) separated by spaces. Most of the boot arguments have the form:

 name[=value_1][,value_2]...[,value_10]

where 'name' is a unique keyword that is used to identify what part of the kernel the associated values (if any) are to be given to. Note the limit of 10 is real, as the present code handles only 10 comma separated parameters per keyword. (However, you can reuse the same keyword with up to an additional 10 parameters in unusually complicated situations, > assuming the setup function supports it.)

Most of the sorting is coded in the kernel source file init/main.c. First, the kernel checks to see if the argument is any of the special arguments 'root=', 'nfsroot=', 'nfsaddrs=', 'ro', 'rw', 'debug' or 'init'. The meaning of these special arguments is described below.

Then it walks a list of setup functions to see if the specified argument string (such as 'foo') has been associated with a setup function ('foo_setup()') for a particular device or part of the kernel. If you passed the kernel the line foo=3,4,5,6 then the kernel would search the bootsetups array to see if 'foo' was registered. If it was, then it would call the setup function associated with 'foo' (foo_setup()) and hand it the arguments 3, 4, 5, and 6 as given on the kernel command line.

Anything of the form 'foo=bar' that is not accepted as a setup function as described above is then interpreted as an environment variable to be set.
A (useless?) example would be to use 'TERM=vt100' as a boot argument.

Any remaining arguments that were not picked up by the kernel and were not interpreted as environment variables are then passed onto PID 1, which is usually the init(1) program. The most common argument that is passed to the init process is the word 'single' which instructs it to boot the computer in single user mode, and not launch all the usual daemons. Check the manual page for the version of init(1) installed on your system to see what arguments it accepts.

This AskUbuntu answer has some good instructions on how to modify your kernel boot parameters.

-1

Put your commands into /etc/rc.local. Make sure to read the comments at the head.

1
  • Ok, tried that too, but it doesn't seem to work either, when I login after rebooting the variables don't exist :(
    – Robert
    Oct 13, 2011 at 4:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .