how do I set a system wide enviroment variable? (not just bash)
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
how do I set a system wide enviroment variable? (not just bash)
Hello,
I was wondering if it is possible to set a system wide environment variable that can be used at system initialization, not just when I envoke bash. I added export SRV=192.168.0.101 in my /etc/profile but it is only useful after I log in. I would like to use this variable in a couple of my initialization scripts and in things like my /etc/fstab. I'm pretty new to linux so I don't know all of the neat little tricks yet. Everything I found online was just referring to shell environment variables. Is there a way to set a variable that is global to the linux system? Thanks to anyone taking the time to read this and respond!
I added export TEST=192.168.0.101 to my rc.local but it still did not create a global variable. I belive it only created the variable for /bin/sh since it was still not acsessable from bash or from my /etc/fstab and /etc/exports. I even tried adding it to the beginning of my /etc/rc.M since the rc.local gets called at the end of this file. Still didn't work. Is there a way to make a variable that is not shell specific? thanks for the advice!
Have you looked at the .profile files? There is usually one for bash, and one for the system. The system one is supposed to get executed before your local shell .profile does. (in some cases bash is not your default shell).
Here is a question for you. Are you trying to pick up TEST in a system environment variable, or are you trying to go to a TEST url. If the latter, why not create an entry in the /etc/host file with TEST and your ip address
Yes, thank you for the advice. I wanted to create a symbolic to all of the computers on my network so I don't have to type out their IP all of the time. I will give the /etc/host file a try and see if it works in the various configuration files I use.
Just out of curiosity though, is it possible to set a non shell specific system variable? If all commands get procesed through a shell, what comes before the shell, and how can I play with it? The reason I switched to linux was to be able to ask questions just like this one. =)
I see. environment variables that I set in bash shell get caried over to any other shell that I run from bash. Why is it then, that a variable is undefined in bash when I set it before envoking any shell., (IE defined in rc.local) when you 'export SOMEVAR=bananas, where is it exporting to? (a tmp file?, system memory?) Apparently any shell can access exported variables post log in, why is it different pre login? Again, this thread has turned in to more of a curiosity thing then an actual problem. As it is my nature to always have a million questions, I won't get offended if you don't have the time to keep responding to this tread. But if you have any of the answers, or any other information on how variables work that I might find interesting, please share. Or if you prefer, point me in the right direction to get the answers myself, as you did in your last post. I would prefer the later method myself.
The variable $test is visible in the new subshell.
> csh
> echo $test
atest
The variable $test is visible in the new c shell. It inherits it from the environment. The "export" command moves a variable from local storage to the environment.
Quote:
From bashref:
Invoked as an interactive login shell, or with `--login'
........................................................
When Bash is invoked as an interactive login shell, or as a
non-interactive shell with the `--login' option, it first reads and
executes commands from the file `/etc/profile', if that file exists.
After reading that file, it looks for `~/.bash_profile',
`~/.bash_login', and `~/.profile', in that order, and reads and
executes commands from the first one that exists and is readable. The
`--noprofile' option may be used when the shell is started to inhibit
this behavior.
Different shells use different startup scripts. Some others use /etc/profile, but the syntax may not work if you aren't carefull. Csh doesn't use profile, and the syntax for assigning variables is different. However if your system uses bash, and an environment variable is already set, a users new csh will inherit the system environment.
Why is it then, that a variable is undefined in bash when I set it before envoking any shell., (IE defined in rc.local)
rc.local is just a script that is run at boot time. It has no relation to your login shell.
Quote:
when you 'export SOMEVAR=bananas, where is it exporting to? (a tmp file?, system memory?)
Every unix process has an environment, a list of key=value strings. Each process inherits its environment from its parent.
The basic login process goes like so. *getty displays the "login:" prompt. You enter your user name and *getty execs /bin/login, passing the user name. login then prompts you for you password, which it verifies. Assuming the password is correct, login then exec's your login shell with a clean environment. login sets some fundamental variables like HOME, USER, LOGNAME, a default PATH etc.
There are various shell configuration files, system wide files in /etc and user files in ~. The filenames obviously differ from shell to shell. E.g., bash reads /etc/profile and ~/.profile for a login shell, then ~/.bashrc (a non-login shell just reads ~/.bashrc). These files then setup the rest of your environment.
So the process sequence is init->getty->login->shell. rc.local is run before any gettys are started, so there is no direct relationship between rc.local and a user shell. Thus, nothing you do in rc.local has any bearing on your shell environment.
Thank you for everyone's input. I think I'm starting to get a much better understanding for how the linux environment works. I hope to some day really understand the concepts behind linux, not just how to use it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.