Setting path environment variable in Linux system (three methods)


1. In Windows system, many software installation needs to configure environment variables, such as JDK. If you do not configure environment variables and run javac command in the directory of non software installation, you will report that the file cannot be found, similar error.

2. So what are environment variables? In short, it is to specify a directory. When running the software, the related programs will look for related files according to the directory. The most practical function of setting variables for ordinary people is not to copy some DLL files into the system directory, and the system variable path is a series of paths for the system to search DLL files

In Linux system, if you download and install an application, it is very likely that the prompt “command not found” will appear when you type its name. If you go to the installation directory folder every time, it will be too cumbersome to find the executable file to operate. This involves the setting of the environment variable path, which is also a part of customizing environment variables under Linux

There are three ways to set environment variables in Linux

If you want to add a path to $path, you can do as follows:

Environment variables that work only on the current shell

1. This method is not approved because it only works on the current shell, and a different shell setting is invalid

Enter in the direct console: $path = $path “: / new_ Path (closing shell path will restore the original path)

Environment variables that work for all users

2. Modify the / etc / profile file. If your computer only uses this method for development, because all users’ shells have the right to use this environment variable, it may bring security problems to the system. This is for all users, all shells

vi /etc/profile

At the bottom of / etc / profile, add: export path = $path / new_ PATH”

Environment variables that work for the current specific user

3. This method is more secure to modify the bashrc file. It can control the permission to use these environment variables to the user level. This is for a specific user. If you need to give a user permission to use these environment variables, you only need to modify the. Bashrc file in the personal user’s home directory.

vi ~/.bashrc

Add below:


Ubuntu Linux system environment variable configuration file:

/Etc / Profile: the first file used by the operating system to customize the user environment when logging in. This file sets the environment information for each user of the system. When the user logs in for the first time, this file is executed.

/Etc / environment: the second file used by the operating system when logging in. The system sets the environment variables of the environment file before reading your own profile.

~/. profile: the third file used in login is. Profile file. Each user can use this file to input shell information for his own use. When the user logs in, this file is only executed once! By default, he sets some environment variables to execute the user’s. Bashrc file.

/Etc / bashrc: execute this file for every user running bash shell. When the bash shell is opened, the file is read

~/. bashrc: this file contains bash information specific to your bash shell, which is read when you log in and every time you open a new shell.

How to set the pass environment variable:

Method 1. profile or. Bashrc file in the user’s home directory (recommended)

Log in to your user (non root) and enter:

$ sudo gedit ~/.profile(or .bashrc) 

The path setting can be added at the end of this file as follows:

export PATH=”$PATH:your path1:your path2 ...” 

Save the file, log off and log on again, the variable takes effect.

The variables added in this way are only valid for the current user.

Method 2Profile file in system directory (caution)

In the etc directory of the system, there is a profile file

$ sudo gedit /etc/profile 

At the end, add the path setting as follows:

export PATH=”$PATH:your path1:your path2 ...” 

After the file is edited and saved, restart the system and the variable takes effect.

The variables added in this way are valid for all users.

Method 3Environment file in system directory (caution)

There is an environment file in the etc directory of the system

$ sudo gedit /etc/environment 

Find the following path variable:


Modify the path variable and add your own path. For example:

PATH="<......>:your path1:your path2 …" 

The paths are separated by colons. The file is also restart effective, affecting all users.

Note that export path =… Is not added here.

Method 4Input directly under the terminal

$ sudo export PATH="$PATH:your path1:your path2 …" 

In this way, the variable takes effect immediately, but the setting becomes invalid after the user logs off or the system restarts, which is suitable for setting temporary variables.

Note: the modification of methods 2 and 3 should be cautious, especially through the root user. If the modification is wrong, it may lead to some serious system errors. Therefore, the author recommends the first method. In addition, it’s better not to develop embedded Linux under root (unless you are already familiar with Linux) To avoid serious system errors caused by improper operation.

The following is an example of the problem caused by the incorrect modification of the environment file and the solution:

Problem: unable to log in due to setting environment variable in etc / environment

Tip: do not set the export path in etc / environment. This will result in the system not logging in after restart


In the login interface Alt + Ctrl + F1, enter the command mode. If it is not the root user, you need to type it (the root user is not allowed to be so wordy, and the GEDIT editor will not be displayed)
/usr/bin/sudo /usr/bin/vi /etc/environment

Move the cursor to the line export path * *, and press D twice to delete the line;

Enter: WQ to save and exit;

Then type / SBIN / reboot to restart the system (you may be prompted to need to boot, and power off directly)

By Joan

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.