How to set global environment variables?

0156

To set global environment variables that are available to all users on a Linux system, you can follow these steps:

  1. Edit the /etc/environment file:

    • Open the file with a text editor (you may need sudo privileges):
      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.
  2. 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
  3. Reboot or Re-login:

    • For the changes to take effect, you can either reboot the system or log out and log back in.
  4. Verify the Variable:

    • After logging back in, you can verify that the global environment variable is set by using:
      echo $PROJECT_DIR

This will ensure that the environment variable is available to all users and persists across sessions.

0 Comments

no data
Be the first to share your comment!