在Debian环境下,环境变量的优先级如下:
内核参数:内核参数是系统启动时由内核设置的,具有最高的优先级。可以通过sysctl
命令查看和修改内核参数。
/etc/environment:这是一个全局配置文件,用于设置系统范围内的环境变量。它在系统启动时被读取,优先级次于内核参数。
/etc/profile 和 /etc/profile.d/ 目录下的脚本:这些脚本在用户登录时执行,用于设置用户级别的环境变量。它们的优先级低于/etc/environment。
~/.bashrc 和 ~/.bash_profile(或 ~/.profile):这些文件是针对单个用户的配置文件,它们在用户登录时执行。~/.bashrc 主要用于交互式shell,而 ~/.bash_profile(或 ~/.profile)主要用于登录shell。它们的优先级低于/etc/profile 和 /etc/profile.d/ 目录下的脚本。
~/.pam_environment:这是一个用户级别的环境变量配置文件,它允许PAM(Pluggable Authentication Modules)模块设置环境变量。它的优先级低于~/.bashrc 和 ~/.bash_profile(或 ~/.profile)。
命令行参数:在启动程序时,可以通过命令行参数传递环境变量。这种方式的环境变量优先级最低。
总结一下,环境变量的优先级顺序为:内核参数 > /etc/environment > /etc/profile 和 /etc/profile.d/ 目录下的脚本 > ~/.bashrc 和 ~/.bash_profile(或 ~/.profile)> ~/.pam_environment > 命令行参数。