To set global environment variables that are available to all users on a Linux system, you can follow these steps:
-
Edit the
/etc/environmentfile:- Open the file with a text editor (you may need
sudoprivileges):sudo nano /etc/environment - Add your environment variable in the format
VARIABLE_NAME=value. For example:PROJECT_DIR="/home/labex/projects" - Save and exit the editor.
- Open the file with a text editor (you may need
-
Using
/etc/profile.d/:- Create a new script file in the
/etc/profile.d/directory:sudo nano /etc/profile.d/myenvvars.sh - Add your environment variables in the same format:
export PROJECT_DIR="/home/labex/projects" - Save and exit the editor. Make sure the script is executable:
sudo chmod +x /etc/profile.d/myenvvars.sh
- Create a new script file in the
-
Reboot or Re-login:
- For the changes to take effect, you can either reboot the system or log out and log back in.
-
Verify the Variable:
- After logging back in, you can verify that the global environment variable is set by using:
echo $PROJECT_DIR
- After logging back in, you can verify that the global environment variable is set by using:
This will ensure that the environment variable is available to all users and persists across sessions.
