zoukankan      html  css  js  c++  java
  • Linux进阶之环境变量文件/etc/profile、/etc/bashrc、/etc/environment

    一、Centos 环境变量/etc/profile/etc/profile.d

    1、两个文件都是设置环境变量的文件。

    2/etc/profile 是永久性的环境变量,是全局变量,/etc/profile.d/ 设置对所有用户生效

    3/etc/profile.d/ /etc/profile 好维护。不想要什么变量直接删除/etc/profile.d/下对应的.sh shell脚本即可,不用像/etc/profile需要改动此文件。

    区别:

    /etc/profile 是文件

    1、只有Login shell 启动时才会运行 /etc/profile 这个脚本,而Non-login shell 不会调用这个脚本。

    说明:当一个用户登录Linux系统或使用su -命令切换到另一个用户时,也就是Login shell 启动时,首先要确保执行的启动脚本就是 /etc/profile

    2、在/etc/profile 文件中设置的变量是全局变量。

    /etc/profile.d 文件夹,下面包含很多.sh 脚本

    1)在/etc/profile.d 目录中存放的是一些应用程序所需的启动脚本,而这些脚本文件是用来设置一些变量和运行一些初始化过程的。其中包括了颜色、语言、lessvimwhich等命令的一些附加设置。

    2/etc/profile.d 下的脚本之所以能自动执行,是因为在/etc/profile 中有一个for循环语句来调用这些脚本。

    环境变量配置

    1/etc/profile配置

    #################################

    #在文件最后添加

    export PATH=/usr/local/tomcat/bin/:$PATH

    ##################################

    2/etc/profile.d配置

    生成一个子配置文件tomcat.sh

    ###############################

    export PATH=/usr/local/tomcat/bin/:$PATH

    ################################

    二、/etc/profile/etc/bashrc的区别

    /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行,并从/etc/profile.d目录的配置文件中搜集shell的设置。

    /etc/bashrc:为每一个运行bash shell的用户执行此文件,当bash shell被打开时,该文件被读取。

    ~/.bash_profile:每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件。是交互式login 方式进入 bash 运行的。

    ~/.bashrc:该文件包含专用于你的bash shellbash信息,当登录时以及每次打开新的shell时,该文件被读取。交互式 non-login 方式进入 bash 运行的。

    ~/.bash_logout:当每次退出系统(退出bash shell)时,执行该文件。

    另外,/etc/profile中设定的变量(全局)的可以作用于任何用户,而~/.bashrc等中设定的变量(局部)只能继承/etc/profile中的变量,他们是"父子"关系。通常二者设置大致相同,通常前者会调用后者

    三、在登录Linux时要执行文件的过程如下:

    在刚登录Linux时,首先启动 /etc/profile 文件,然后再启动用户目录下的 ~/.bash_profile ~/.bash_login ~/.profile文件中的其中一个(根据不同的linux操作系统的不同,命名不一样)

    执行的顺序为:~/.bash_profile ~/.bash_login ~/.profile

    如果 ~/.bash_profile文件存在的话,一般还会执行 ~/.bashrc文件因为在 ~/.bash_profile文件中一般会有下面的代码: if [ -f ~/.bashrc ] ; then . ./bashrc fi ~/.bashrc中,一般还会有以下代码: if [ -f /etc/bashrc ] ; then . /bashrc fi 所以,~/.bashrc会调用 /etc/bashrc文件。最后,在退出shell时,还会执行 ~/.bash_logout文件。

    执行顺序为:/etc/profile -> (~/.bash_profile | ~/.bash_login | ~/.profile) -> ~/.bashrc -> /etc/bashrc -> ~/.bash_logout

    四、/etc/profile/etc/environment等各种环境变量设置文件的用处

    先将export加入/etc/profile,退出系统重新登录,登录提示显示英文。

    /etc/profile 中的export删除,将LNAG=zh_CN加入/etc/environment,退出系统重新登录,登录提示显示中文。

    用户环境建立的过程中总是先执行/etc/profile然后在读取/etc/environment为什么会有如上所叙的不同呢?

    应该是先执行/etc/environment,后执行/etc/profile

    /etc/environment是设置整个系统的环境,而/etc/profile是设置所有用户的环境,前者与登录用户无关,后者与登录用户有关。

    系统应用程序的执行与用户环境可无关但与系统环境是相关的,对于用户的SHELL初始化而言是先执行/etc/profile,再读取文件/etc/environment。对整个系统而言是先执行/etc/environment

    /etc/enviroment --> /etc/profile --> $HOME/.profile -->$HOME/.env (如果存在)

    /etc/profile 是所有用户的环境变量

    /etc/enviroment是系统的环境变量

    登陆系统时shell读取的顺序应该是/etc/profile ->/etc/enviroment -->$HOME/.profile -->$HOME/.env

    如果同一个变量在用户环境(/etc/profile)和系统环境(/etc/environment)有不同的值那应该是以用户环境为准了。

  • 相关阅读:
    WebService之Axis2 (3):使用services.xml文件发布WebService
    WebService之Axis2(2):复合类型数据的传递
    WebService之Axis2(1):用POJO实现0配置的WebService
    com.mchange.v2.c3p0.impl.NewPooledConnection@be1839d closed by a client的正确解答
    拿到新服务器时应做的标准测试
    rabbitmq heartbeat missing with heartbeat = N seconds原因总结
    从技术专家到管理者的思路转变(V1)
    netty集成ssl完整参考指南(含完整源码)
    技术晋升的评定与博弈(转)
    Eureka-zookeeper的服务发现替代方案
  • 原文地址:https://www.cnblogs.com/renyz/p/11351934.html
Copyright © 2011-2022 走看看