美文网首页
Linux 重启不自动加载 bash.profile 文件.md

Linux 重启不自动加载 bash.profile 文件.md

作者: Noah牛YY | 来源:发表于2018-07-13 07:55 被阅读111次

    linux 开机不自动加载 ~/bash_profile, 导致设置的 alias 无效

    原因

    终端启动分为 loginnon-login 两种方式, non-login 方式启动是不加载 ~/.bash_profile 文件的。在登录情况下,
    一般 shell 会先读取 ~/.profile,再读取 ~/.bashrc。如果是在非登录情况下,shell只会去读取 ~/.bashrc

    1. Debian 默认的 shell 是 Bash

    1.1 命令行 和 ssh 登录

    首先读入 /etc/profile,这是对所有用户都有效的配置;然后依次寻找下面三个文件,这是针对当前用户的配置。

    • ~/.bash_profile
    • ~/.bash_login
    • ~/.profile

    需要注意的是,这三个文件只要有一个存在,就不再读入后面的文件了。比如,要是 ~/.bash_profile 存在,就不会再读入后面两个文件了。

    1.2 图形界面登录

    只加载 /etc/prfile~/.profile。也就是说,~/.bash_profile 不管有没有,都不会运行。

    2.用户进入操作系统图形界面

    常常会再手动开启一个 shell。这个 shell 就叫做 non-login shell,意思是它不同于登录时出现的那个 shell,不读取 /etc/profile
    .profile等配置文件。

    3.终端模拟器

    通常会有选项来指定是开 login shell 还是 non-login shell,比如 xfce4-terminal 的。

    解决方式

    1. 把环境变量写入到 ~/.bashrc 中,或者 ~/.profile

    2. ~/.profile 里添加下面几行代码,即加载 .profile 文件同时调用 ~/.bash_profile 文件。

      if [ -n "$BASH_VERSION" ]; then
          if [ -f "$HOME/.bash_profile" ]; then
                "$HOME/.bash_profile"
          fi
      fi
      
    3. 改变登录方式,注销系统并重新登录

      # 编辑文件
      vi ~/.config/deepin/deepin-terminal/config.conf
      # 找到第56行,讲 false 修改为 true
      run_as_login_shell=true
      # :wq 保存退出
      :wq
      

    总结:

    1. 图形界面登录是 non-login 不运行 .bash_profile
    2. shell 和 ssh 登录打开 login shell ,会运行 .bash_profile
    3. 图形界面登录,可以指定 终端模拟器 为 non-login 还是 login ,但这只是指定 终端模拟器 ,经实测,和写到 .bashrc 效果一样,每次打开终端都会执行一次。
    4. 要只在图形界面登录时执行一次,应该写入 .profile,而不是 .bash_profile,或者在 .profile 增加一条 调用 .bash_profile

    转载自: Linux开机不能自动加载 ~/.bash_profile 文件的解决方法

    相关文章

      网友评论

          本文标题:Linux 重启不自动加载 bash.profile 文件.md

          本文链接:https://www.haomeiwen.com/subject/vrgdpftx.html