How to make environment variables permanent in Linux?

Making Environment Variables Permanent in Linux

In Linux, environment variables are used to store various settings and configurations that can be accessed by the operating system and applications. These variables are typically set during the login process or in the shell configuration files, but they are only valid for the current session. If you want to make environment variables persistent across sessions, you need to make them permanent.

There are a few different ways to make environment variables permanent in Linux:

1. Editing the System-Wide Environment File

The most common way to make environment variables permanent is to edit the system-wide environment file, which is typically located at /etc/environment. This file is read by the system during the boot process, and any variables defined in it will be available to all users and applications.

To edit the /etc/environment file, you can use a text editor like nano or vi:

sudo nano /etc/environment

Then, add your environment variable to the file, using the following format:

VARIABLE_NAME=value

For example, to set the JAVA_HOME environment variable, you would add the following line to the file:

JAVA_HOME=/opt/jdk/jdk-11.0.12

After making the changes, save the file and exit the text editor. The changes will take effect the next time you log in or start a new shell session.

2. Editing User-Specific Environment Files

If you only want to make an environment variable permanent for a specific user, you can edit the user-specific environment files. These files are typically located in the user's home directory and are read when the user logs in.

The most common user-specific environment files are:

  • .bashrc: This file is read by the Bash shell when it starts up. You can add environment variables to this file.
  • .bash_profile: This file is read by the Bash shell when a login shell starts up. You can add environment variables to this file.
  • .profile: This file is read by the system when a user logs in. You can add environment variables to this file.

To edit one of these files, you can use a text editor like nano or vi. For example, to edit the .bashrc file:

nano ~/.bashrc

Then, add your environment variable to the file, using the same format as in the system-wide environment file:

export VARIABLE_NAME=value

After making the changes, save the file and exit the text editor. The changes will take effect the next time you log in or start a new shell session.

3. Using the export Command

You can also make environment variables permanent by using the export command in your shell configuration files. This method is similar to the previous one, but it allows you to set the variable for the current shell session as well as make it persistent.

To make an environment variable permanent using the export command, add the following line to your shell configuration file (e.g., .bashrc, .bash_profile, or .profile):

export VARIABLE_NAME=value

This will set the environment variable for the current shell session and make it persistent across sessions.

Mermaid Diagram: Environment Variable Persistence in Linux

graph TD A[System-Wide Environment File] --> B[User-Specific Environment Files] B --> C[export Command] A[System-Wide Environment File] --> D[Boot Process] B[User-Specific Environment Files] --> E[Login Process] C[export Command] --> F[Current Shell Session] D[Boot Process] --> G[All Users and Applications] E[Login Process] --> H[Specific User] F[Current Shell Session] --> I[Current User]

In summary, there are three main ways to make environment variables permanent in Linux: editing the system-wide environment file, editing user-specific environment files, and using the export command in shell configuration files. The choice of method depends on whether you want the environment variable to be available to all users or just a specific user, and whether you want it to be available for the current shell session or across sessions.

0 Comments

no data
Be the first to share your comment!