1

I need to set 6 variables which are used by several services and applications in Linux. I have so far been using the environment variables, but find it very complicating to modify these variables throughout multiple users. At the same time I wish that all users are able to modify these variables that are used globally.

My solution so far has been to use SQLite instead, but seems to be a bit of a mess when I want to set the variables within a service daemon.

So the question is, is it possible or even realistic to use global environment variables that can be altered by any user or should I consider a database solution such as SQLite?

E.g. if i have a variable IP_RECIPIENT, this variable has to be accessed and set by all users in the system. If it is altered, all users should be able to read the changes, in other words it has to be global even when altered and not just local.

Also whats the best-practice for using global environment variables within Arch Linux ARM.

  • 1
    Please edit and explain what these variables do in a bit more detail. From what I can understand, simply defining them in /etc/profile would be enough. They would then be available to all users and they would be free to modify them for their own sessions. – terdon Mar 22 '15 at 17:01
  • @terdon, if they are modified it has to be done globally, which introduces some problems. And the new value has to be maintained and not reset when the system is rebooted. – JavaCake Mar 22 '15 at 17:05
  • Ah. OK, please edit your question and describe the limitations you need to deal with precisely. Perhaps you could set /etc/profile to read the variables from another file and make that file world-writeable. – terdon Mar 22 '15 at 17:09
  • I'll do my best! – JavaCake Mar 22 '15 at 17:12
2

A simple solution might be to have these variables set in a file, making that file world-writeable and sourcing it from /etc/bash.bashrc. That way, the variables would be available to all users, any change in the sourced file would be kept after reboot and would also be available to any new shells started. Note that any open shells will not have their values updated.

  1. Create the file with the variable definitions and make it world readable

    printf "%s=%s\n%s=%s\n" "var1='foo'" "var2='bar'" | sudo tee /etc/variables
    sudo chmod a+w /etc/variables
    

    That will create a file called /etc/variables with the following contents:

    var1='foo'
    var2='bar'
    
  2. Edit /etc/bash.bashrc to make it source that file

    sudo printf "\n## Source global variables\n. /etc/variables\n" | 
        sudo tee -a /etc/bash.bashrc
    

Now, any changes in that file will be reflected in any new interactive non-login shells.


If you need this to be immediately inherited even by open shells, you could use bash's PROMPT_COMMAND variable:

   PROMPT_COMMAND
          If set, the value is executed as a command prior to issuing each
          primary prompt.

Edit /etc/profile and add this line:

PROMPT_COMMAND='. /etc/variables'

Once you have done this, any changes in the /etc/variables file will be immediately available to all shells since the file will be sourced before every prompt is shown. Note that this will only take effect after your users have logged back in since they will need to read /etc/profile again.

  • One last thing. When i use export var1='test' in a arbitrary user, the value is not set. Whys that? But i can read foo and bar – JavaCake Mar 26 '15 at 12:19
  • @javacake that won't work, you can't export variables to another user's session. If you want to add a new. Variable, add it to the /etc/variables file. – terdon Mar 26 '15 at 14:49
  • But can i change the variables from any user or are they static in the variables file? – JavaCake Mar 26 '15 at 16:05
  • @JavaCake if you make the file editable by all users (either with chmod a+w /etc/variables or by creating a new user group for the target users and changing the ownership of the file to that group), then any of your users will be able to edit the file and the changes will be immediately available to any other user. Basically, that file is read before a prompt is shown so it is re-read after every command executed. – terdon Mar 26 '15 at 16:56
  • Im not sure if this will be a good solution after all. I will be changing the variables very often, and as i can understand it will require direct editing of the file. The modification of variables could e.g. be done within a nodejs application interacting via a web-interface. Perhaps the database solution would be better after all compared to file manipulation? – JavaCake Mar 26 '15 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.