~/.bashrc、 ~/.bash_profile、~/.profile、 /etc/profile几个配置文件的区别

在 Linux 系统中,.bashrc, .bash_profile, .profile/etc/profile 这几个文件都有各自的用途,它们在不同的场景下被加载和执行。下面是对这几个文件的区别和作用的详细介绍:

1. ~/.bashrc

  • 描述:这个文件是针对每个用户的 bash shell 的个人配置文件。

  • 加载时机 :每当用户打开一个新的交互式终端窗口(或标签页)时,~/.bashrc 就会被读取并执行一次。

  • 用途:通常用于设置个人的环境变量、别名定义、shell 函数以及其他个性化配置。

  • 示例内容

    sh 复制代码
    # Source global definitions
    if [ -f /etc/bashrc ]; then
      . /etc/bashrc
    fi
    
    # User specific aliases and functions
    alias ll='ls -l'

2. ~/.bash_profile

  • 描述 :也是针对每个用户的 bash shell 的个人配置文件,但是它的加载优先级高于 ~/.bashrc

  • 加载时机:在用户登录系统时被读取和执行一次。

  • 用途 :通常用于设置全局环境变量,例如 JAVA_HOMEPATH 等。

  • 示例内容

    sh 复制代码
    # Get the aliases and functions
    if [ -f ~/.bashrc ]; then
      . ~/.bashrc
    fi
    
    # User specific environment and startup programs
    export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
    export PATH=$JAVA_HOME/bin:$PATH

3. ~/.profile

  • 描述:针对每个用户的个人配置文件,适用于所有 shell(不仅仅是 bash)。

  • 加载时机:在用户登录系统时被读取和执行一次。

  • 用途:通常用于设置适用于所有 shell 的环境变量。

  • 示例内容

    sh 复制代码
    # The default umask is set in /etc/profile; for setting the umask
    # for ssh logins, install and configure the libpam-umask package.
    #umask 022
    
    # if running bash
    if [ -n "$BASH_VERSION" ]; then
      # include .bashrc if it exists
      if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
      fi
    fi
    
    # set PATH so it includes user's private bin if it exists
    if [ -d "$HOME/bin" ] ; then
      PATH="$HOME/bin:$PATH"
    fi

4. /etc/profile

  • 描述:系统范围内的配置文件,适用于所有用户和所有 shell(不仅仅是 bash)。

  • 加载时机:在用户登录系统时被读取和执行一次。

  • 用途:通常用于设置系统范围内的环境变量和其他全局配置。

  • 示例内容

    sh 复制代码
    # System-wide .profile file for the Bourne shell (sh(1)) and
    # Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
    
    # If not running interactively, don't do anything
    case $- in
      *i*) ;;
        *) return;;
    esac
    
    # Set PATH for Debian/GNU systems.
    # if "$PATH" is not set up correctly, this can cause problems!
    if [ -z "$PATH" ] || [ "$PATH" = ":/" ]; then
        PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
    fi
    
    # Add an additional PATH entry, if it exists
    if [ -d /usr/games/bonus ]; then
      PATH="$PATH:/usr/games/bonus"
    fi
    
    # set PATH so it includes /usr/local/bin and user's private bin if it exists
    if [ -d "$HOME/bin" ] ; then
      PATH="$HOME/bin:$PATH"
    fi

总结

  • ~/.bashrc:适用于每个用户的 bash shell,每次打开新的终端窗口时加载。
  • ~/.bash_profile :适用于每个用户的 bash shell,登录时加载,可以包含 ~/.bashrc
  • ~/.profile:适用于每个用户的所有 shell,登录时加载。
  • /etc/profile:适用于所有用户的系统范围配置,登录时加载。

根据需求,我们可以选择合适的地方来配置环境变量或其他个性化设置。通常,如果只需要配置 bash shell 的个性化设置,可以使用 ~/.bashrc;如果需要设置全局环境变量,可以使用 ~/.bash_profile~/.profile。系统级别的配置则放在 /etc/profile 中。

相关推荐
派阿喵搞电子15 分钟前
Ubuntu下有关UDP网络通信的指令
linux·服务器·网络
Evan_ZGYF丶23 分钟前
【PCIe总线】 -- PCI、PCIe相关实现
linux·嵌入式·pcie·pci
舰长11531 分钟前
Ubuntu挂载本地镜像源(像CentOS 一样挂载本地镜像源)
linux·ubuntu·centos
程序员JerrySUN31 分钟前
全面理解 Linux 内核性能问题:分类、实战与调优策略
java·linux·运维·服务器·单片机
huangyuchi.1 小时前
【Linux】LInux下第一个程序:进度条
linux·运维·服务器·笔记·进度条·c/c++
帽儿山的枪手2 小时前
程序员必掌握的iptables五表五链
linux·网络协议
西阳未落2 小时前
Linux(14)——库的制作与原理
linux
444A4E2 小时前
深入Linux进程优先级:Nice值与O(1)调度器原理
linux·操作系统
Jooolin2 小时前
【编程史】Git是如何诞生的?这可并非计划之中...
linux·git·ai编程
云边有个稻草人2 小时前
【Linux系统】第八节—进程概念(上)—冯诺依曼体系结构+操作系统+进程及进程状态+僵尸进程—详解!
linux·进程·冯诺依曼体系结构·pcb·僵尸进程·进程的状态·task_ struct