howto set global environment variable (e.g. PATH, JAVA_HOME)

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

howto set global environment variable (e.g. PATH, JAVA_HOME)

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.

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 AN EASY WAY (change only one central file for all users) TO SET THEM?
If not, how must /etc/login.conf changed for PATH and JAVA_HOME?

Reply | Threaded
Open this post in threaded view
|

Re: howto set global environment variable (e.g. PATH, JAVA_HOME)

Will Maier
On Wed, Aug 08, 2007 at 06:09:16AM -0700, pixotec wrote:
> 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)

This would work.

> 2. change /etc/login.conf ???

This would also work (see login.conf(5)).

> 3. create /etc/profile, change all existing .profile of users (to
> source /etc/profile) and change /etc/skel/.profile

As would this.

> 4. change /etc/ksh.kshrc and create .kshrc sourcing /etc/ksh.kshrc
> for all users (and in /etc/skel...)

And this.

> 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/ | [hidden email] |
*------------------[ BSD Unix: Live Free or Die ]------------------*

Reply | Threaded
Open this post in threaded view
|

Re: howto set global environment variable (e.g. PATH, JAVA_HOME)

Markus Lude-3
In reply to this post by pixotec
On Wed, Aug 08, 2007 at 06:09:16AM -0700, pixotec wrote:

> 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

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.

> 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

Reply | Threaded
Open this post in threaded view
|

Re: howto set global environment variable (e.g. PATH, JAVA_HOME)

Clint Pachl
In reply to this post by pixotec
pixotec wrote:

> 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?
>  

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
 > 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

Reply | Threaded
Open this post in threaded view
|

Re: howto set global environment variable (e.g. PATH, JAVA_HOME)

Lars Hansson-5
On 8/9/07, Clint Pachl <[hidden email]> wrote:
> 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

Reply | Threaded
Open this post in threaded view
|

Re: howto set global environment variable (e.g. PATH, JAVA_HOME)

Darren S.
On 8/8/07, Lars Hansson <[hidden email]> wrote:
> On 8/9/07, Clint Pachl <[hidden email]> wrote:
> > 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

Reply | Threaded
Open this post in threaded view
|

Re: howto set global environment variable (e.g. PATH, JAVA_HOME)

Lars Hansson-5
On 8/9/07, Darren Spruell <[hidden email]> wrote:
> ~/.profile overrides /etc/profile.

Yes and both are processed.

> $ 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

Reply | Threaded
Open this post in threaded view
|

Re: howto set global environment variable (e.g. PATH, JAVA_HOME)

J.C. Roberts
In reply to this post by Will Maier
On Wednesday 08 August 2007, Will Maier wrote:
> > 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

Reply | Threaded
Open this post in threaded view
|

Re: howto set global environment variable (e.g. PATH, JAVA_HOME)

Clint Pachl
In reply to this post by Lars Hansson-5
Lars Hansson wrote:
> On 8/9/07, Clint Pachl <[hidden email]> wrote:
>  
>> 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.
> 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

Reply | Threaded
Open this post in threaded view
|

Re: howto set global environment variable (e.g. PATH, JAVA_HOME)

Edd Barrett
In reply to this post by Lars Hansson-5
On 09/08/07, Lars Hansson <[hidden email]> wrote:
> 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

Reply | Threaded
Open this post in threaded view
|

Re: howto set global environment variable (e.g. PATH, JAVA_HOME)

Pierre-Yves Ritschard-4
On Fri, 10 Aug 2007 09:11:55 +0100
"Edd Barrett" <[hidden email]> wrote:

> On 09/08/07, Lars Hansson <[hidden email]> wrote:
> > 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.

Reply | Threaded
Open this post in threaded view
|

Re: howto set global environment variable (e.g. PATH, JAVA_HOME)

Lars Hansson-5
In reply to this post by Edd Barrett
On 8/10/07, Edd Barrett <[hidden email]> wrote:
> 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

Reply | Threaded
Open this post in threaded view
|

Re: howto set global environment variable (e.g. PATH, JAVA_HOME)

pixotec
In reply to this post by pixotec
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.conf.
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!