强烈建议将环境变量写入 ~/.bashrc
根据其他文章参考资料,环境变量可加入以下四个位置且优先级由高到低:
1、/etc/profile.d/test.sh #/etc/profile.d/ 目录下脚本均会执行
2、/etc/profile
3、/etc/bashrc
4、~/.bashrc #用户级
验证:
#测试环境
[root@test ~]# cat /etc/centos-release
CentOS Linux release 7.5.1804 (Core)
[root@test ~]# echo "echo \"/etc/profile say hi\"" >> /etc/profile
[root@test ~]# echo "echo \"/etc/profile.d say hi\"" >> /etc/profile.d/test.sh
[root@test ~]# echo "echo \"~/.bashrc say hi\"" >> ~/.bashrc
[root@test ~]# echo "echo \"/etc/bashrc say hi\"" >> /etc/bashrc
#登录
Last login: Tue Mar 2 21:32:25 2021 from 10.0.0.101
/etc/profile.d say hi
/etc/profile say hi
/etc/bashrc say hi
~/.bashrc say hi
[root@test ~]#
为什么建议写入~/.bashrc呢?
今天使用ansible-playbook时,启动zookeeper时总是不成功,最后发现是jdk环境变量没有设置上,当时设置的位置是/etc/profile,后来改为~/.bashrc时问题解决。