linux环境变量怎么用

  • linux下主要有四个配置文件:
配置文件 生效范围 功能
/etc/profile 对所有用户生效 设置的是系统全局环境和登录系统的一些配置
/etc/bashrc 对所有用户生效 shell全局自定义配置文件,主要用于自定义shell
/root/.bashrc 只对单个用户生效 只用于root用户的 bash,若要对elk用户生效则需要配置 /home/elk/.bashrc文件
/root/.bash_profile 只对单个用户生效 只用于root用户的系统环境,若要对elk用户生效则需要配置 /home/elk/.bash_profile

注:由于 ubuntu 重启之后 /etc/bashrc 不会生效(CentOS不会这样),所以建议统一使用 /etc/profile

  • 验证四个配置文件的加载顺序
[root@localhost ~]# echo "echo "this is /etc/profile"" >> /etc/profile
[root@localhost ~]# echo "echo "this is /etc/bashrc"" >> /etc/bashrc
[root@localhost ~]# echo "echo "this is /root/.bashrc"" >> /root/.bashrc
[root@localhost ~]# echo "echo "this is /root/.bash_profile"" >> /root/.bash_profile
[root@localhost ~]# exit
logout

Last login: Sun Dec 29 04:51:02 2024 from 192.168.31.164
this is /etc/profile
this is /etc/bashrc
this is /root/.bashrc
this is /root/.bash_profile
  • 结论:当登录系统或者重新ssh连接,这四个配置文件的加载顺序为:/etc/profile > /etc/bashrc > /root/.bashrc > /root/.bash_profile
Copyright © www.sqlfans.cn 2024 All Right Reserved更新时间: 2025-01-10 10:36:34

results matching ""

    No results matching ""