Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set PATH environment variable in Linux system

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to set the PATH environment variable in the Linux system? Many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can gain something.

There are three ways to set environment variables under Linux:

To add a path to $PATH, you can do something like this:

Environment variables that work only on the current shell

1. Setting it in the console does not support this method, because it only works on the current shell, and changing the shell setting will have no effect:

Enter: $PATH= "$PATH": / NEW_PATH in the direct console (closing shell Path will restore to the original path)

Environment variables that work for all users

2, modify the / etc/profile file, if your computer is only used as a developer to use this method, because all users' shell has the right to use this environment variable, it may bring security problems to the system. This is for all users, all shell

Vi / etc/profile

Add: export PATH= "$PATH:/NEW_PATH" at the bottom of / etc/profile

Environment variables that work for the current specific user

3, modify the bashrc file, this method is more secure, it can control the use of these environment variables to the user level, here is for a specific user, if you need to give a user permission to use these environment variables, you only need to modify the individual user home directory under the .bashrc file on it.

Vi / .bashrc

Add below:

Export PATH= "$PATH:/NEW_PATH"

Ubuntu Linux system environment variable profile:

/ etc/profile: the first file used by the operating system to customize the user environment when logging in, which sets the environment information for each user of the system, and is executed when the user logs in for the first time.

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

~ / .profile: the third file used when logging in is the .profile file, which each user can use to enter shell information dedicated to his or her own use. When the user logs in, the file is executed only once! By default, he sets some environment variables to execute the user's .bashrc file.

/ etc/bashrc: execute this file for each user running bash shell. When 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 each time you open a new shell.

The setting method of PASH environment variable:

Method 1: .profile or .bashrc file in the user's home directory (recommended)

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

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

The settings for adding PATH at the end of this file are as follows:

Export PATH= "$PATH:your path2:your path3..."

Save the file, log out and log in again, and the variable takes effect.

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

Method 2: profile files in the system directory (be careful)

In the system's etc directory, there is a profile file, edit it:

$sudo gedit / etc/profile

The settings for adding PATH at the end are as follows:

Export PATH= "$PATH:your path2:your path3..."

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

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

Method 3: environment files in the system directory (be careful)

In the system's etc directory, there is an environment file, edit it:

$sudo gedit / etc/environment

Find the following PATH variable:

PATH= ""

Modify the PATH variable to add your own path, for example:

PATH= ": your path2:your path3..."

Each path is separated by a colon. The file is also effective for rebooting, affecting all users.

Note that export PATH= is not being added here. .

Method 4: enter directly under the terminal

$sudo export PATH= "$PATH:your path2:your path3..."

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

Note: methods 2 and 3 need to be modified with caution, especially by root users, which may lead to some serious system errors. Therefore, the author recommends the first method. In addition, the development of embedded Linux should not be carried out under root (unless you are already familiar with Linux! To avoid serious errors in the system due to improper operation

The following is an example of a problem caused by incorrect modification of the environment file and an example of how to solve it:

Problem: unable to log in because it was accidentally set to the environment variable in etc/environment

Tip: do not set export PATH in etc/environment. This will cause you not to log in to the system after reboot.

Solution:

Enter command mode in the login interface alt + ctrl+f1. If it is not necessary for root users to type (root users are not allowed to be so wordy, gedit editors will not be able to display)

/ usr/bin/sudo / usr/bin/vi / etc/environment

Move the cursor to the export PATH** line and press d twice in a row to delete the line

Enter: wq save exit

Then type / sbin/reboot to restart the system (need to boot may be prompted, power off directly at this point)

What is Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report