Linux环境变量到底配置到那里?

首先,说明用户配置Linux的环境变量的两个位置:

1、系统位置,/etc目录下的bashrc、profile、environment三个文件

2、用户目录(root或普通用户)下的.bashrc、.bash_profile两个文件,注意这两个文件前面都有点,是隐藏文件。

先说这3个系统文件,列出文件内容(参考CentOS7)。 /**/为添加注释

# /etc/bashrc

# System wide functions and aliases      /* 系统广泛功能(函数)和别名 */
# Environment stuff goes in /etc/profile /* 环境东西(环境变量)在/etc/profile文件中 */

/* 修改这个文件不是一个好主意,除非你知道自己在干什么。*/
/* 在/etc/profile.d/目录下创建一个自定义脚本会是修改环境变量的更好方法 */
/* 但是会阻止在将来更新时合并的需要 */
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging(合并) in future updates.
……………………………………………………………………
……………………………………………………………………
……………………………………………………………………

# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
……………………………………………………………………
……………………………………………………………………
……………………………………………………………………

第三个文件environment文件打开是一个空文件,不表。
再说两个用户文件,贴上文件内容(参考CentOS7)/**/为添加注释

普通用户:

# .bash_profile

# Get the aliases and functions                  /* 获取别名和函数(功能)*/
if [ -f ~/.bashrc ]; then
	. ~/.bashrc
fi

# User specific environment and startup programs /* 用户指定环境变量和启动程序(linux开机启动程序放在这里?还没有试过) */

PATH=$PATH:$HOME/.local/bin:$HOME/bin

export PATH


# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi
/* 取消下面行的注释如果你不喜欢系统的自动分页特性 */
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions       /* 用户指定的别名和函数(功能)*/

root用户

# .bashrc

# User specific aliases and functions      /* 用户指定的别名和函数(功能)*/


alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi

# .bash_profile

# Get the aliases and functions            /* 获取别名和函数(功能)*/

if [ -f ~/.bashrc ]; then
	. ~/.bashrc
fi

# User specific environment and startup programs    /* 用户指定环境变量和启动程序(linux开机启动程序放在这里?还没有试过) */


PATH=$PATH:$HOME/bin

export PATH

这几个文件的注释已经说得很清楚了,bashrc文件是 用户指定别名和函数(功能), bash_profile文件是用来获取bashrc文件中的别名和函数(功能),用户还可以在此文件中指定环境变量和开机启动程序。在系统的bashrc、和bash_profile文件明确说明 /etc/profile.d/目录下创建一个自定义脚本会是修改环境变量的更好方法。如图:



到这里文章就结束了,但是在实际配置环境变量中发现,如果变量仅配置在系统配置文件中,以普通用户登录系统终端中是有自己添加的环境变量,一旦su到root,终端中就没有了之前配环境变量。解决方法就是在每个用户的配置文件中都配置一次,这样太麻烦。在/etc/profile.d/目录下创建一个自定义脚本的方法无论你在一个终端中如何切换用户,环境变量依然存在。

/etc/profile.d/目录下创建一个自定义脚本应该是配置环境变量最好的方法,至少在CentOS7中是这样。






  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拜乔布斯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值