Linux启动配置文件

Linux启动配置文件

描述

启动文件一般指系统启动或用户登录等所执行配置文件;
如系统配置/etc/profile,用户配置~/.profile

启动文件分类

当登入Linux系统启动一个bash shell时,bash会在启动文件或环境文件中查找命令,
根据启动bash shell方式不同,bash检查的启动文件也不同

  • /etc/profile
    系统默认的bash shell主启动文件,系统每个用户每次登录时都会执行;
    /etc/profile文件中都会迭代执行/etc/profile.d目录下的文件;

  • $HOME/.bash_profle
    H O M E 目 录 的 启 动 文 件 , 提 供 一 个 用 户 专 属 的 启 动 文 件 来 定 义 用 户 所 用 到 的 环 境 变 量 ; ‘ HOME目录的启动文件,提供一个用户专属的启动文件来定义用户所用到的环境变量; ` HOMEHOME/.bash_profle文件会执行$HOME/.bashrc`目录下的文件;

  • /etc/rc.local
    设置整个系统的环境信息,在系统启动时被读取,与登录用户无关;

  • /etc/environment
    设置整个系统的环境信息,在系统启动时被读取,与登录用户无关;

  • /etc/bashrc
    针对所有用户的bash Shell初始化文件,任何用户每次登录或打开新的Shell时执行该文件;

  • $HOME/.bashrc
    该文件设置用户bash shell的bash信息,在用户每次登录时或打开新的Shell时执行该文件;

  • $HOME/.bash_logout
    在用户每次退出登录的时候执行该文件;

shell分类
  • 登录式shell

    直接通过用户名、密码进行登录Linux操作系统的shell;
    登录式shell会执行/etc/profile$HOME/.bash_profle/etc/bashrc$HOME/.bashrc
    示例

    [root@localhost ~]#ssh git@172.16.36.2
    git@172.16.36.2's password: 
    Last login: Fri Jul 23 11:23:21 2021 from 172.16.36.25
    i am /etc/profile @@@@@@@@@@@@@@@@@@@@
    i am /etc/bashrc @@@@@@@@@@@@@@@@@
    i am /home/git/.bashrc %%%%%%%%%%%%%%%%%%%%%%%%
    i am /home/git/.bash_profile %%%%%%%%%%%%%%%%%%%%%%%%
    [git@centos-36_2 ~]$ 
    [git@centos-36_2 ~]$  grep 'i am' /etc/profile
    echo "i am /etc/profile @@@@@@@@@@@@@@@@@@@@"
    [git@centos-36_2 ~]$  grep 'i am' /etc/bashrc
    echo "i am /etc/bashrc @@@@@@@@@@@@@@@@@"
    [git@centos-36_2 ~]$  grep 'i am' /home/git/.bash_profile
    echo "i am /home/git/.bash_profile %%%%%%%%%%%%%%%%%%%%%%%%"
    [git@centos-36_2 ~]$  grep 'i am' /home/git/.bashrc
    echo "i am /home/git/.bashrc %%%%%%%%%%%%%%%%%%%%%%%%"
    [git@centos-36_2 ~]$ 
    
  • 交互式shell

    bash shell不是登录时启动的,如直接在命令行提示下敲入bash启动;
    交互式shell会执行/etc/bashrc$HOME/.bashrc

    ps --forest展示子shell间的嵌套关系;

    示例

    [git@centos-36_2 ~]$ grep 'i am' /etc/bashrc
    echo "i am /etc/bashrc @@@@@@@@@@@@@@@@@"
    [git@centos-36_2 ~]$ grep 'i am' /home/git/.bashrc
    echo "i am /home/git/.bashrc %%%%%%%%%%%%%%%%%%%%%%%%"
    [git@centos-36_2 ~]$ 
    [git@centos-36_2 ~]$ bash
    i am /etc/bashrc @@@@@@@@@@@@@@@@@
    i am /home/git/.bashrc %%%%%%%%%%%%%%%%%%%%%%%%
    [git@centos-36_2 ~]$
    
  • 非交互式shell

    系统执行shell脚本用的shell,无命令提示符;
    bash shell提供BASH_ENV环境变量,启动非交互式shell时,检查这个环境变量来查看要执行的文件;
    一般shell脚本通过启动子shell执行,子shell可继承父shell的变量,所以一般很少设置BASH_ENV;

    示例:

    [gao@centos-36_2 ~]$ cat gaogao.sh 
    #!/bin/bash
    echo "this is a test shell!!!!"
    [gao@centos-36_2 ~]$ cat myenv.sh 
    #!/bin/bash
    echo "this is a bash_env test"
    [gao@centos-36_2 ~]$ ./gaogao.sh
    this is a test shell!!!!
    [gaokangkang@centos-36_2 ~]$ echo $BASH_ENV
    <br />
    [gao@centos-36_2 ~]$ ./gaogao.sh
    this is a test shell!!!!          #未设置BASH_ENV时,仅执行gaogao.sh
    [gao@centos-36_2 ~]$ 
    [gao@centos-36_2 ~]$ BASH_ENV="/home/gao/myenv.sh"
    [gao@centos-36_2 ~]$ export BASH_ENV
    [gao@centos-36_2 ~]$ echo $BASH_ENV
    /home/gao/myenv.sh
    [gao@centos-36_2 ~]$ ./gaogao.sh
    this is a bash_env test          #设置BASH_ENV后,优先执行myenv.sh再执行gaogao.sh
    this is a test shell!!!!
    [gao@centos-36_2 ~]$ 
    
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值