Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Setting Environament variables in Debian and ubuntu

Posted on 2006-04-21
27
Medium Priority
?
10,265 Views
Last Modified: 2013-11-15
hi experts

i want to set some system environment variables and paths
to the Debian and ubuntu machines like

JAVA_HOME=usr/local/jdk
...............
...............
etc

and also paths to the PATH variable like

PATH=$PATH:/usr/local/tomcat/bin

these variables should be set permanently and shud be available to all the terminals
opened by default

pls give the script procedure and how to set it to load permanently on startup

thanks

0
Comment
Question by:Econify
  • 10
  • 8
  • 5
  • +2
27 Comments
 
LVL 7

Accepted Solution

by:
XoF earned 400 total points
ID: 16505372
add the variable definitions to /etc/profile:

echo "JAVA_HOME=usr/local/jdk" >> /etc/profile
echo "PATH=$PATH:/usr/local/tomcat/bin" >> /etc/profile

HTH,
-XoF-
0
 

Author Comment

by:Econify
ID: 16505406
hi xof

can u pls tel me what this echo command does
i used echo like this

echo $JAVA_HOME "Java is set"

>> /etc/profile ?
what does this do

thanks
0
 
LVL 43

Assisted Solution

by:ravenpl
ravenpl earned 800 total points
ID: 16505462
I guess /etc/environment is better sollution than /etc/profile ...

>> /etc/profile ?
> what does this do
this script is called upon user login, bot only on login(login shell)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:XoF
ID: 16505530
echo "JAVA_HOME=usr/local/jdk"
will print "JAVA_HOME=usr/local/jdk" to stdout.

>> /etc/profile

will redirect stdout to /etc/profile and will append to the end of the file.

So
echo "JAVA_HOME=usr/local/jdk" >> /etc/profile
will append the line "JAVA_HOME=usr/local/jdk" to the end of /etc/profile.

> I guess /etc/environment is better sollution than /etc/profile ...

For debian-based systems (as stated in the question title), you're most likely correct.
(does /etc/environment exist on other distributions, too? Guess not...)

In any case, just one thing we missed:
Exporting the Variables might be a good idea. So the following is to do:

cat << EOF >> /etc/environment
JAVA_HOME=usr/local/jdk
PATH=$PATH:/usr/local/tomcat/bin
export JAVA_HOME PATH
EOF

regards,
-XoF-

0
 
LVL 43

Expert Comment

by:ravenpl
ID: 16505572
> (does /etc/environment exist on other distributions, too? Guess not...)
it surely does/works for RH/FC & Suse. Surely not for gentoo. Don't know about others.
0
 
LVL 7

Expert Comment

by:XoF
ID: 16505676
well, my linux experience began with kernel 1.3 and I haven't ever used /etc/environment. Seems as if one never stops learning...;)
Thx,
-XoF-
0
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 800 total points
ID: 16505714
/etc/profile is the traditional place for most systems (I'd prefer that for compatibility;-)
/etc/environment is proprietary to some distributions, if you're adicted to them, use it and keep this dragon in mind
/etc/profile.env is the gentoo way

According the variables mentioned in the question, I guess that this is for some users, not root or any other system users, henc I highly recommend that you do not add it to the files in /etc (except there is one for some users only).
You better add that to the corresponding startup file in each user's home directory, like ~/.profile
0
 
LVL 7

Expert Comment

by:XoF
ID: 16505751
> According the variables mentioned in the question, I guess that this is for some users, not root or any other system users

Funny to see, how different the understandings can be...:)
I assumed the opposite, hence didn't mention ~/.profile
The variables seem to be necessary for a servlet engine, which I consider as a system service....
Anyway, /etc/profile won't have any effect in startup scripts...


-XoF-
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16505992
> Anyway, /etc/profile won't have any effect in startup scripts...
hmm, sounds like you have not seen those proprietary rc-scripts some distributions deliver where they use something like
  . /etc/profile
and they think/claim that it is a sophisticated script then :-((

sorry for being off-topic
0
 
LVL 7

Expert Comment

by:XoF
ID: 16506022
> hmm, sounds like you have not seen those proprietary rc-scripts some distributions deliver
I came, saw and forgot...;)
0
 

Author Comment

by:Econify
ID: 16506140
hello everybody
thanks for the reponses

i did the mods in etc/profile

everythings is getting set except the PATH variables
echo $PATH$ shows only
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11$

its not appending my locations to it

pls help

my etc file looks like this


# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "`id -u`" -eq 0 ]; then
  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11"
else
  PATH="/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games"
fi

if [ "$PS1" ]; then
  if [ "$BASH" ]; then
    PS1='\u@\h:\w\$ '
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi
JAVA_HOME=/usr/local/jdk
CATALINA_HOME=/usr/local/tomcat
ANT_HOME=/usr/local/ant

PATH=$PATH:/usr/local/jdk/bin:/usr/local/ant/bin:/usr/local/mysql/bin:/usr/local/tomcat/bin

CLASSPATH=$CLASSPATH:

export JAVA_HOME CATALINA_HOME ANT_HOME PATH CLASSPATH

echo $JAVA_HOME '... path of JAVA_HOME='
echo $CATALINA_HOME '... path of CATALINA_HOME'
echo $ANT_HOME '... path of ANT_HOME'

umask 022
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16506438
> echo $PATH$ shows only
you mean:
  echo $PATH

did you source /etc/profile (or login again) after changing?
0
 

Author Comment

by:Econify
ID: 16506465
hi hoffman

yes echo $ PATH or echo $PATH$ shows only this line

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11

it shud have appended my app paths to it as shown in script

pls help

0
 
LVL 11

Expert Comment

by:fargo
ID: 16506516
i think export is missing from your paths

export JAVA_HOME=/usr/local/jdk
export CATALINA_HOME=/usr/local/tomcat
export ANT_HOME=/usr/local/ant
export PATH=$PATH:/usr/local/jdk/bin:/usr/local/ant/bin:/usr/local/mysql/bin:/usr/local/tomcat/bin
export CLASSPATH=$CLASSPATH:



0
 

Author Comment

by:Econify
ID: 16506525
hey fargo

its there in the script
see the second last variable in lien

export JAVA_HOME CATALINA_HOME ANT_HOME PATH CLASSPATH

0
 
LVL 11

Expert Comment

by:fargo
ID: 16506540
sorry, my eyes overlooked that.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16506923
> it shud have appended my app paths to it as shown in script
.. only if you start a new login shell or source /etc/profile before starting the app
0
 

Author Comment

by:Econify
ID: 16507618
hi hoffman

can u please be a little elaborative

.. only if you start a new login shell or source /etc/profile before starting the app

After setting the variables in etc/profile i restarted the  m/c
and started the new terminal
i checked for echo $JAVA_HOME it was properly set to /usr/local/jdk
but still echo $path is not showing  these
usr/local/jdk/bin:/usr/local/ant/bin:/usr/local/mysql/bin:/usr/local/tomcat/bin

as i want to run tomcat or java commands directly from any folder
instead traversing everytime to the bin folder of these applications.

pls help


0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16512508
replace
  PATH=$PATH:/usr/local/jdk/bin:/usr/local/ant/bin:/usr/local/mysql/bin:/usr/local/tomcat/bin
by
  PATH="${PATH}:/usr/local/jdk/bin:/usr/local/ant/bin:/usr/local/mysql/bin:/usr/local/tomcat/bin"

and be sure you test in a new login shell after changing
0
 

Author Comment

by:Econify
ID: 16524048
hi did that still its not working out

i have a strange observation
when i created a script in seperate file called setappsetting.sh
and ran the script by command
. setappsetting

the path are getting set properly and the command
echo $PATH is proprly showing my app paths

here's the script of file

JAVA_HOME=/usr/local/jdk
TOMCAT_HOME=/usr/local/tomcat
CATALINA_HOME=/usr/local/tomcat
BASEDIR=/usr/local/tomcat
MYSQL_HOME=/usr/local/mysql
ANT_HOME=/usr/local/ant

PATH=$PATH:/usr/local/jdk/bin:/usr/local/ant/bin:/usr/local/mysql/bin
CLASSPATH=$CLASSPATH:
export JAVA_HOME TOMCAT_HOME BASEDIR CATALINA_HOME MYSQL_HOME ANT_HOME PATH CLASSPATH
echo $JAVA_HOME '... java home'

pls help
when the same thing works here why is it not working in etc/profile

0
 

Author Comment

by:Econify
ID: 16524155
i guess the path variable is getting reset again to default
during the rest of boot up process
i think we should set it in the last process of the booting up of debian os

no idea how to do it.

anybody there pls help
0
 

Author Comment

by:Econify
ID: 16524340
there is no etc/environment

so not able to try this option too
hung
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16525414
please check your /etc/profile if it calls/sources other scripts
Which distribution are you using?
0
 

Author Comment

by:Econify
ID: 16526167
i am using debian
and i have no idea whether etc/profile  calls the sources
i have already pasted my etc/profile files content above
pls see that

thanks
jags
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16527792
> i have already pasted my etc/profile files content above
oops, you're right
the posted file does not source other files
does your .profile in the home directory set PATH?
0
 

Author Comment

by:Econify
ID: 16531707
hi hofman i think we have found it
here's the content of my /root/.profile file

now pls guide me what shud i do further now
shud i remove the path line from this file

debinac:/home# vi /root/.profile
# ~/.profile: executed by Bourne-compatible login shells.

if [ -f ~/.bashrc ]; then
  . ~/.bashrc
fi

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11
export PATH

mesg n
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16531840
> now pls guide me what shud i do further now
please read my comments posted so far again

You asked me, here in short:
  I guess that thes environment variables are for some special accounts only, where these accounts are used by human users.
  Then I'd add these variables to the ~/.profile , ~/.bashrc , ~/.cshrc (or whatever you need for their login shell) in the corresponding home directory.
  I'd never change /etc/profile (and similar for other shells) for such "application-" and user-specific things.
  I'd *never, never, never* add it to the environment of user root.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You ever wonder how to backup Linux system files just like Windows System Restore?  Well you can use Timeshift in Linux to perform those similar action.  This tutorial will show you how to backup your system files and keep regular intervals. Note…
I have written articles previously comparing SARDU and YUMI.  I also included a couple of lines about Easy2boot (easy2boot.com).  I have now been using, and enjoying easy2boot as my sole multiboot utility for some years and realize that it deserves …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question