How to Effectively Use the ‘/etc/environment’ File in Linux
Linux, the powerful open-source operating system, offers flexibility and customization opportunities that are often unattainable with other platforms. One area where this flexibility becomes particularly apparent is in managing environment variables. The `/etc/environment` file in Linux plays a pivotal role in this aspect. But what exactly is it, and how can it be used effectively? Let’s dive in.
What is the /etc/environment file?
Before we delve into the how, it’s crucial to understand what the `/etc/environment` file is. It is a system-wide configuration file in Linux that is read upon system boot. This file is utilized to set up environment variables — key-value pairs that can be used by processes in the operating system.
These environment variables might include paths to executable files, application settings, or configuration details for software. In Linux, environment variables are typically stored in multiple locations, but the `/etc/environment` file is the most universal and straightforward method to set these variables system-wide.
Structure of the /etc/environment file
The `/etc/environment` file is pretty simple in its structure. It does not support shell scripting, unlike other files used for setting environment variables, such as `~/.bashrc` or `~/.profile`. It only contains raw environment variable assignments in the form:
How to Set an Environment Variable in Linux
Zaira Hira
In programming, you use variables to store information like strings and numbers temporarily.
Variables can be used repeatedly throughout the code or by your operating system to provide values. You can edit them, overwrite them, and delete them.
In this tutorial, I’ll teach you what environment variables are and how to set them in Linux.
What Are Environment Variables?
Environment variables are the variables specific to a certain environment. For example, each user in an operating system has its own environment. An admin user has a different environment than other users do, for example.
You might declare an environment variable only required by your user (for example a secret token) that doesn’t need to be exposed to other users.
Here are some examples of environment variables in Linux:
- USER – This points to the currently logged-in user.
- HOME – This shows the home directory of the current user.
- SHELL – This stores the path of the current user’s shell, such as bash or zsh.
- LANG – This variable points to the current language/locales settings.
- MAIL – This shows the location of where the current user’s mail is stored.
These environment variables vary based on the current user session.
How to List Environment Variables in Linux
The command used to display all the environment variables defined for a current session is env .
Here is the output for my session:
For the changes to take effect, update the .bashrc file using the source command:
Let’s verify by opening a new session.
For the changes to take effect, use the command source /etc/environment .
- /etc/profile – Variables set in this file are read whenever a bash shell is logged in. Edit this file and use the export command:
Now, I’ll switch the user to the root user and verify if I can access the variable GLOBAL_VARIABLE .
root@Zaira:~# echo $GLOBAL_VARIABLE This is a global variable
It worked! I have been able to access the global variable defined by the user Zaira through the root user as well. The same would apply to other users too. So now you also know how to define global environment variables.
Conclusion
In this tutorial, you learned how to create and define environment variables in Linux. You also learned how to make them persistent so that you can use them across multiple sessions.
What’s your favorite thing you learned here? Let me know on Twitter!
You can read my other posts here.