Linux—环境变量管理

环境变量文件的分类

系统级环境变量:每一个登录到系统的用户都能够读取到系统级的环境变量。

用户级环境变量:每一个登录到系统的用户只能够读取属于自己的用户级的环境变量。

一、系统级环境变量文件

  • Centos  中:/etc/environment、 /etc/profile、 /etc/profile.d/*.sh、 /etc/bashrc
  • Ubuntu 中:/etc/environment、 /etc/profile、 /etc/profile.d/*.sh、 /etc/bash.bashrc
  • Debian  中:/etc/environment、 /etc/profile、 /etc/profile.d/*.sh、 /etc/bash.bashrc

二、用户级环境变量文件

  • Centos  中:~/.bashrc、 ~/.bash_profile
  • Ubuntu 中:~/.bashrc、 ~/.profile
  • Debian  中:~/.bashrc、 ~/.profile

环境变量文件的加载顺序

一、正常登录系统的时候

正常 就是直接输入username+password ,login 的时候。不正常 就是当我们 在root用户中 使用 su 命令 切换用户 的时候。

  • Centos  中:/etc/environment、 /etc/profile(包含/etc/profile.d/*.sh)、 ~/.bash_profile、 ~/.bashrc、 /etc/bashrc。
  • Ubuntu 中:/etc/environment、 /etc/profile(包含/etc/profile.d/*.sh与/etc/bash.bashrc)、 ~/.profile、 ~/.bashrc。
  • Debian  中:/etc/environment、 /etc/profile(包含/etc/profile.d/*.sh与/etc/bash.bashrc)、 ~/.profile、 ~/.bashrc。

二、非正常登录系统的时候

非正常登录的时候,是指在 root用户 中 使用 su 命令 切换其他用户,因为 系统在我们登录root用户 的时候,已经加载过一遍 环境变量的配置文件了,所以当你 切换用户登录系统的时候就不用重头重新加载环境变量文件了!

  • Centos  中:/etc/bashrc、      /etc/profile.d/*.sh、 /etc/profile.d/lang.sh。
  • Ubuntu 中:/etc/bash.bashrc、 /etc/profile.d/*.sh、 /etc/profile.d/cedilla-portuguese.sh。
  • Debian  中:/etc/bash.bashrc、 /etc/profile.d/*.sh。

环境变量设置文件

1、读取环境变量的方法:

[root@localhost ~]# env             # 显示当前用户定义的所有环境变量。
[root@localhost ~]# export          # 显示当前系统定义的所有环境变量。
[root@localhost ~]# echo $PATH      # 输出当前的PATH环境变量的值。

2、Linux 环境变量设置文件

  • /etc/profile   全局用户,应用于所有的Shell。

3、Linux 环境变量加载顺序

  • /etc/environment

4、/etc/profile>/etc/profile.d/*.sh>/etc/bashrc

Linux 环境变量配置方法

使用man命令:man:command not found

参考:https://www.cnblogs.com/ben-ben/archive/2012/11/13/2767588.html

# 用echo $PATH查看该环境变量。这种问题一般是环境变量PATH不对的问题。
[root@localhost ~]# echo $PATH

# 用whereis命令查看,该指令的位置。
[root@localhost ~]# whereis man

# 直接使用该地址,或者把该路径加入PATH,添加PATH环境变量方法有两种。
# 方法1:PATH 在终端关闭后就会消失。
[root@localhost ~]# export PATH=/usr/local/webserver/mysql/bin:$PATH
# 方法2:通过编辑/etc/profile来改PATH,也可以修改家目录下的.bashrc(即:~/.bashrc)。
[root@localhost ~]# vim /etc/profile
[root@localhost ~]# export PATH="/usr/local/webserver/mysql/bin:$PATH"
[root@localhost ~]# source /etc/profile

https://blog.csdn.net/ljlfather/article/details/105024401

https://www.cnblogs.com/youyoui/p/10680329.html

https://www.cnblogs.com/EasonJim/p/7137849.html

https://blog.csdn.net/pengjunlee/article/details/81585726

https://blog.csdn.net/qq_31877249/article/details/80607186

https://blog.csdn.net/qq_32863631/article/details/76348963

http://blog.chinaunix.net/uid-20738531-id-1881149.html

原文地址:https://www.cnblogs.com/liuhaidon/p/11505348.html