Setting Environament variables in Debian and ubuntu

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

EconifyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
EconifyAuthor Commented:
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
ravenplCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

XoFCommented:
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
ravenplCommented:
> (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
XoFCommented:
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
ahoffmannCommented:
/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
XoFCommented:
> 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
ahoffmannCommented:
> 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
XoFCommented:
> hmm, sounds like you have not seen those proprietary rc-scripts some distributions deliver
I came, saw and forgot...;)
0
EconifyAuthor Commented:
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
ahoffmannCommented:
> echo $PATH$ shows only
you mean:
  echo $PATH

did you source /etc/profile (or login again) after changing?
0
EconifyAuthor Commented:
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
fargoCommented:
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
EconifyAuthor Commented:
hey fargo

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

export JAVA_HOME CATALINA_HOME ANT_HOME PATH CLASSPATH

0
fargoCommented:
sorry, my eyes overlooked that.
0
ahoffmannCommented:
> 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
EconifyAuthor Commented:
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
ahoffmannCommented:
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
EconifyAuthor Commented:
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
EconifyAuthor Commented:
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
EconifyAuthor Commented:
there is no etc/environment

so not able to try this option too
hung
0
ahoffmannCommented:
please check your /etc/profile if it calls/sources other scripts
Which distribution are you using?
0
EconifyAuthor Commented:
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
ahoffmannCommented:
> 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
EconifyAuthor Commented:
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
ahoffmannCommented:
> 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux Distributions

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.