Discussion:
howto set global environment variable (e.g. PATH, JAVA_HOME)
pixotec
2007-08-08 13:09:16 UTC
Permalink
I want to set the environment variables

PATH=$PATH:/usr/local/jdk-1.5.0/bin
JAVA_HOME=/usr/local/jdk-1.5.0

globally.

for one user I can change therefor .profile like this:
PATH=/usr/local/jdk-1.5.0/bin:/bin:...
...
export PATH HOME TERM

but I want it for all users:
1. could change all .profile-files of all users: no thanx ;-( (and change
/etc/skel/.profile for future new users)
2. change /etc/login.conf ???
3. create /etc/profile, change all existing .profile of users (to source
/etc/profile) and change /etc/skel/.profile
4. change /etc/ksh.kshrc and create .kshrc sourcing /etc/ksh.kshrc for all
users (and in /etc/skel...)

IS THERE A EASY WAY (change only on central file for all users) TO SET THEM?
--
View this message in context: http://www.nabble.com/howto-set-global-environment-variable-%28e.g.-PATH%2C-JAVA_HOME%29-tf4236252.html#a12052849
Sent from the openbsd user - misc mailing list archive at Nabble.com.
Will Maier
2007-08-08 13:26:07 UTC
Permalink
Post by pixotec
1. could change all .profile-files of all users: no thanx ;-( (and
change /etc/skel/.profile for future new users)
This would work.
Post by pixotec
2. change /etc/login.conf ???
This would also work (see login.conf(5)).
Post by pixotec
3. create /etc/profile, change all existing .profile of users (to
source /etc/profile) and change /etc/skel/.profile
As would this.
Post by pixotec
4. change /etc/ksh.kshrc and create .kshrc sourcing /etc/ksh.kshrc
for all users (and in /etc/skel...)
And this.
Post by pixotec
IS THERE A EASY WAY (change only on central file for all users) TO SET THEM?
Well, choose whichever of the above is easiest for you. Based on
your criteria, it seems that modifying login.conf would require the
fewest keystrokes. I prefer to provide a global/site profile or
shell init script and allow users to source it if they'd like, but
that fits my site's policies well; depending on what you do and
where you work, loginf.conf(5) may be more appropriate.
--
o--------------------------{ Will Maier }--------------------------o
| web:.......http://www.lfod.us/ | ***@ml1.net |
*------------------[ BSD Unix: Live Free or Die ]------------------*
J.C. Roberts
2007-08-09 09:14:58 UTC
Permalink
Post by Will Maier
Post by pixotec
4. change /etc/ksh.kshrc and create .kshrc sourcing /etc/ksh.kshrc
for all users (and in /etc/skel...)
And this.
ummm. I don't think so.

The .profile is read only *once* on initial login. Everything that is
spawned from your initial login will inherit the given environment.

In contrast, your shell rc files (.kshrc, .chsrc, etc) will be read on
each new instance of the shell (which you spawn from your original
login).

So, if in .kshrc you do something like this:

PATH=$PATH:/usr/local/jdk-1.5.0/bin
export PATH

your path will grow on each new instance of the shell

$ echo $PATH
/home/jcr/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:/usr/local/bin:/usr/local/sbin:/usr/games:.:/usr/local/java/bin:/home/jcr/ida/
$ echo 'PATH=$PATH:/usr/local/jdk-1.5.0/bin' >> .kshrc
$ echo 'export PATH' >> .kshrc
$ ksh
$ ksh
$ ksh
$ echo $PATH
/home/jcr/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:/usr/local/bin:/usr/local/sbin:/usr/games:.:/usr/local/java/bin:/home/jcr/ida/:/usr/local/jdk-1.5.0/bin:/usr/local/jdk-1.5.0/bin:/usr/local/jdk-1.5.0/bin
$

Is this a really problem? -probably not but then again, it is not what
one would expect and violates the element of least surprise.

kind regards,
jcr
Markus Lude
2007-08-08 15:41:42 UTC
Permalink
Post by pixotec
I want to set the environment variables
PATH=$PATH:/usr/local/jdk-1.5.0/bin
JAVA_HOME=/usr/local/jdk-1.5.0
globally.
PATH=/usr/local/jdk-1.5.0/bin:/bin:...
...
export PATH HOME TERM
1. could change all .profile-files of all users: no thanx ;-( (and change
/etc/skel/.profile for future new users)
2. change /etc/login.conf ???
3. create /etc/profile, change all existing .profile of users (to source
/etc/profile) and change /etc/skel/.profile
Do you really need to source /etc/profile for login shell or is it
automatically sourced? I didn't try it, but sh(1) says:

-l Login shell. If the basename the shell is called with (i.e.
argv[0]) starts with `-' or if this option is used, the shell is
assumed to be a login shell and the shell reads and executes the
contents of /etc/profile and $HOME/.profile if they exist and are
readable.
Post by pixotec
4. change /etc/ksh.kshrc and create .kshrc sourcing /etc/ksh.kshrc for all
users (and in /etc/skel...)
IS THERE A EASY WAY (change only on central file for all users) TO SET THEM?
Regards,
Markus
Clint Pachl
2007-08-08 20:24:21 UTC
Permalink
Post by pixotec
I want to set the environment variables
PATH=$PATH:/usr/local/jdk-1.5.0/bin
JAVA_HOME=/usr/local/jdk-1.5.0
globally.
PATH=/usr/local/jdk-1.5.0/bin:/bin:...
...
export PATH HOME TERM
1. could change all .profile-files of all users: no thanx ;-( (and change
/etc/skel/.profile for future new users)
2. change /etc/login.conf ???
3. create /etc/profile, change all existing .profile of users (to source
/etc/profile) and change /etc/skel/.profile
4. change /etc/ksh.kshrc and create .kshrc sourcing /etc/ksh.kshrc for all
users (and in /etc/skel...)
IS THERE A EASY WAY (change only on central file for all users) TO SET THEM?
As a centralized solution, you can use setenv in login.conf.

Or you could programatically change each user's .profile.

# for i in /home/*/.profile
Post by pixotec
do [[ -f $i ]] || continue
cat << "EOF" >> $i
export PATH=$PATH:/usr/local/jdk-1.5.0/bin
export JAVA_HOME=/usr/local/jdk-1.5.0
EOF
done
If you use the script snippet, DON'T forget the quotes around the first
EOF otherwise $PATH will be interpreted in the current shell, which
would be root's $PATH.

-pachl
Lars Hansson
2007-08-09 04:38:42 UTC
Permalink
Post by Clint Pachl
Or you could programatically change each user's .profile.
Uhm, why? Markus is correct that both /etc/profile and $HOME/.profile
are sourced when you log in so to set up global variables you set them
in /etc/profile.
If you're using xdm things are different though. The Xsession script
does not source any global files so you'll have to modify it to source
/etc/profile.

---
Lars Hansson
Darren Spruell
2007-08-09 05:03:53 UTC
Permalink
Post by Lars Hansson
Post by Clint Pachl
Or you could programatically change each user's .profile.
Uhm, why? Markus is correct that both /etc/profile and $HOME/.profile
are sourced when you log in so to set up global variables you set them
in /etc/profile.
~/.profile overrides /etc/profile.

$ echo 'var1=a' >> /etc/profile
$ echo 'var1=b' >> ~/.profile
$ /bin/ksh -l
$ echo $var1
b

In this case, users' dot files will have be updated.

DS
Lars Hansson
2007-08-09 05:42:56 UTC
Permalink
Post by Darren Spruell
~/.profile overrides /etc/profile.
Yes and both are processed.
Post by Darren Spruell
$ echo 'var1=a' >> /etc/profile
$ echo 'var1=b' >> ~/.profile
$ /bin/ksh -l
$ echo $var1
b
Of course, because .profile is processed after /etc/profile. Variables
set in /etc/profile can be overridden by the user in .profile so
setting the global defaults in /etc/profile works fine.

---
Lars Hansson
Clint Pachl
2007-08-09 09:33:57 UTC
Permalink
Post by Lars Hansson
Post by Clint Pachl
Or you could programatically change each user's .profile.
Uhm, why? Markus is correct that both /etc/profile and $HOME/.profile
are sourced when you log in so to set up global variables you set them
in /etc/profile.
Exactly, however, /etc/profile is only sourced during shell logins,
hence my script snippet.
Post by Lars Hansson
If you're using xdm things are different though. The Xsession script
does not source any global files so you'll have to modify it to source
/etc/profile.
Uhm, why? This is exactly why I gave the advice I did. Just do what I
said and you won't have to worry about whether your using a login shell
or a non-login shell from an xterm, etc.

Even if you source /etc/profile in Xsession, any new shell you invoke in
an xterm, mrxvt, rxvt, etc. will not source /etc/profile, unless it is
invoked as a login shell.

-pachl
Edd Barrett
2007-08-10 08:11:55 UTC
Permalink
Post by Lars Hansson
If you're using xdm things are different though. The Xsession script
does not source any global files so you'll have to modify it to source
/etc/profile.
Is there a global Xdefaults file which can be made to source every
users .profile and /etc/profile for xdm logins?
--
Best Regards

Edd

---------------------------------------------------
http://students.dec.bournemouth.ac.uk/ebarrett
Pierre-Yves Ritschard
2007-08-10 08:22:27 UTC
Permalink
On Fri, 10 Aug 2007 09:11:55 +0100
Post by Edd Barrett
Post by Lars Hansson
If you're using xdm things are different though. The Xsession script
does not source any global files so you'll have to modify it to
source /etc/profile.
Is there a global Xdefaults file which can be made to source every
users .profile and /etc/profile for xdm logins?
Yes: /etc/X11/app-defaults/XTerm, you can use LoginShell: true
there.
Lars Hansson
2007-08-10 09:38:52 UTC
Permalink
Post by Edd Barrett
Is there a global Xdefaults file which can be made to source every
users .profile and /etc/profile for xdm logins?
Yes and no. There's a global defaults for X but they deal with X
resources, not enviroment variables. You can set xterm to always use a
login shell, for example, but that does not affect your DE/WM, only
xterm.
It's not hard to create, say, /etc/xprofile and just source that from
Xsession though.

---
Lars Hansson
pixotec
2007-08-11 21:26:09 UTC
Permalink
thank you all for your tips.

I had success in two ways.

first way:
changed path and added setenv in default-class in /etc/login.
did not work first , had to change .profile of my user, because setting of
PATH in .profile eliminated setting in login.conf. also /etc/skel/.profile
had to be changed for future users.

second (and finally preferred) way:
- created /etc/profile:
# sh/ksh initialization

export JAVA_HOME=/usr/local/jdk-1.5.0

PATH=/usr/local/jdk-1.5.0/bin:$HOME/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6
/bin:/usr/local/bin:/usr/local/sbin:/usr/games:.
export PATH HOME TERM

- changed .profile of my user to not override /etc/profile-PATH:
# $OpenBSD: dot.profile,v 1.4 2005/02/16 06:56:57 matthieu Exp $
#
# sh/ksh initialization

PATH=$PATH
export PATH HOME TERM

- changed /etc/skel/.profile for further users
# $OpenBSD: dot.profile,v 1.4 2005/02/16 06:56:57 matthieu Exp $
#
# sh/ksh initialization

PATH=$PATH
export PATH HOME TERM

thank you all again!

Test:
$ cd /usr/local/jdk-1.5.0/demo/jfc/Stylepad/
$ java -jar Stylepad.jar

Welcome in Alice's Wonderland!
--
View this message in context: http://www.nabble.com/howto-set-global-environment-variable-%28e.g.-PATH%2C-JAVA_HOME%29-tf4236252.html#a12109189
Sent from the openbsd user - misc mailing list archive at Nabble.com.
Loading...