Bash

How can I set an environment variable for everyone under my Linux system

25 September 2026 · 5 min read

How can I set an environment variable for everyone under my Linux system

Navigating the intricacies of a Linux system often involves customizing its behavior to suit specific needs, whether for development, system administration, or running applications. One fundamental aspect of this customization is managing environment variables. While setting variables for a single user is straightforward, the challenge often arises when you need to configure an environment variable for everyone under your Linux system. This guide delves into the various methods for achieving system-wide environment variable persistence, ensuring that all users, including those logging in via SSH or running scripts, inherit the necessary configurations. Understanding these approaches is crucial for maintaining a consistent and functional multi-user Linux environment.

Understanding Linux Environment Variables

Environment variables are dynamic named values that can affect the way running processes behave in a computer. They are essentially key-value pairs stored within the shell environment, providing crucial information to programs and scripts. For instance, the PATH variable tells the shell where to look for executable programs, while HOME points to a user’s home directory. These variables allow applications to function correctly without hardcoding paths or configurations, promoting flexibility and portability within the operating system.

In Linux, environment variables can be either user-specific or system-wide. User-specific variables are typically defined in files like ~/.bashrc or ~/.profile, affecting only the user who owns those files. In contrast, system-wide variables are accessible by all users and processes on the system, making them ideal for configurations that universally apply, such as setting up a default Java Development Kit (JDK) path or a shared library directory. Achieving global environment variables requires careful placement in system configuration files.

The distinction between these two types is vital when considering how to set an environment variable for everyone under your Linux system. Incorrectly placing a system-wide variable in a user-specific file will lead to inconsistencies and potential application failures for other users. Properly managing these variables is a cornerstone of effective Linux administration, impacting everything from security to software deployment.

Methods for Setting System-Wide Environment Variables

When it comes to setting global environment variables in Linux, several key files and directories are typically used, each with its own scope and execution timing. Choosing the right method depends on the specific variable’s purpose and how universally it needs to be applied. The most common locations include /etc/environment, /etc/profile, and the /etc/profile.d/ directory. Understanding their differences is key to effective environment configuration.

The /etc/environment file is perhaps the simplest and most robust way to set system-wide variables. It’s read by the pam_env module at login, and it’s designed specifically for defining environment variables, one per line, using a simple KEY="value" format. It does not execute scripts or commands, making it ideal for static variables like PATH or JAVA_HOME. Variables set here are available to all processes, including those started by non-login shells or cron jobs, as long as they inherit the environment from a login shell or a process that sourced /etc/environment.

Another powerful option is /etc/profile. This script is executed by login shells for all users. Unlike /etc/environment, /etc/profile can contain shell commands and logic, making it suitable for more dynamic variable assignments or for running initialization scripts. However, it’s not sourced by non-login interactive shells (like opening a new terminal window after logging in), which often rely on user-specific .bashrc files. For modularity and easier management, the /etc/profile.d/ directory is highly recommended; any .sh scripts placed within this directory are automatically sourced by /etc/profile, allowing administrators to add custom configurations without directly editing /etc/profile.

For interactive non-login shells, such as when you open a new terminal tab, /etc/bash.bashrc (or a similar file depending on your shell) might be relevant. This file is sourced for all interactive Bash shells, whether login or non-login. While it can be used to set environment variables, it’s generally preferred to use /etc/environment or /etc/profile.d/ for true system-wide variables that apply to both login and non-login shells, and even graphical environments.

Step-by-Step Guide: Setting Variables with /etc/profile.d/

For most scenarios, creating a custom script within the /etc/profile.d/ directory is the recommended approach for setting an environment variable for everyone under your Linux system. This method offers excellent modularity, making it easy to add, remove, or modify variables without touching core system files. It ensures that variables are set for all login shells, which then propagate to other processes.

To set a new system-wide environment variable using this method, follow these steps:

  1. Create a new script file: Use a text editor like nano or vim to create a new file in the /etc/profile.d/ directory. The filename should end with .sh. For example, to set a variable for a custom application, you might name it my_app_env.sh. ```bash sudo nano /etc/profile.d/my_app_env.sh

  2. Add your variable definitions: Inside the newly created file, define your environment variables. Use the export command to make the variable available to child processes. For example, to set a CUSTOM_PATH and MY_APP_HOME variable: ```bash export CUSTOM_PATH="/opt/my_app/bin:$CUSTOM_PATH" export MY_APP_HOME="/opt/my_app"

    
     Remember that variables set here will be evaluated by the shell, so you can use existing variables (like `$PATH`) in your definitions. This is a common practice for extending the system's [executable search path](https://courthousezoological.com/n7sqp6kh?key=e6dd02bc5dbf461b97a9da08df84d31c).
    
  3. Set appropriate permissions: Ensure the script has executable permissions for the owner and read permissions for others. This is crucial for the script to be sourced Question & Answer :
    Can I have certain settings that are universal for all my users?

    As well as /etc/profile which others have mentioned, some Linux systems now use a directory /etc/profile.d/; any .sh files in there will be sourced by /etc/profile. It’s slightly neater to keep your custom environment stuff in these files than to just edit /etc/profile.