如何修改LINUX用户环境变量
如何修改LINUX用户环境变量详细介绍
在 Linux 系统里,你能够通过修改特定的配置文件来更改用户环境变量。下面为你详细介绍不同的修改方式:
临时修改
在当前的终端会话里,可借助export命令对环境变量进行临时修改。一旦关闭终端,所做的修改就会失效。
bash
:/new/directory/path
永久修改
若要永久修改用户环境变量,需要编辑用户主目录下的配置文件。以下是几种常见的配置文件及其适用场景:
1.
~/.bashrc(适用于交互式非登录 shell)
如果你使用的是 Bash shell,那么可以编辑~/.bashrc文件。当你打开一个新的终端会话(非登录会话)时,这个文件会被自动加载。
bash
~/.bashrc
:/new/directory/path
~/.bashrc
2.
~/.bash_profile(适用于交互式登录 shell)
对于交互式登录 shell,可编辑~/.bash_profile文件。当你通过登录系统或者使用su -命令切换用户时,这个文件会被加载。
bash
~/.bash_profile
:/new/directory/path
~/.bash_profile
3.
~/.profile(适用于所有用户的通用配置)
~/.profile文件适用于所有用户的通用配置,无论使用何种 shell。当你登录系统时,这个文件会被加载。
bash
~/.profile
:/new/directory/path
~/.profile
系统级别的环境变量修改
若要对所有用户的环境变量进行修改,可编辑/etc/environment或者/etc/profile文件。不过,这种修改会影响到系统中的所有用户,所以操作时要格外谨慎。
1.
/etc/environment
bash
/etc/environment
:/new/directory/path"
2.
/etc/profile
bash
/etc/profile
:/new/directory/path
991234567891011 nano /etc/profile
= =:/new/directory/path
修改环境变量之后,你可以通过echo $变量名来验证修改是否生效。