linux:环境变量配置文件

所有用户下都生效

/etc/profile

/etc/profile.d/*.sh  *.sh表示所有的.sh文件都支持

/etc/bashrc


用户目录下的环境变量文件

~/.bash_profile 

~/.bashrc


环境变量调用顺序:

1.当用户登录shell的时候顺序,也就是需要输入账号和密码的时候,在ssh远程登录:

/etc/profile  ->/etc/profile.d/*.sh -> /etc/profile.d/lang.sh -> /etc/sysc.config/i18n

流程分析:

命令:vim /etc/profile,查看这个文件内容如下:

  1 # /etc/profile 文件名称
  2 
  3 # System wide environment and startup programs, for login setup
  4 # Functions and aliases go in /etc/bashrc
  5 
  6 # It's NOT a good idea to change this file unless you know what you
  7 # are doing. It's much better to create a custom.sh shell script in
  8 # /etc/profile.d/ to make custom changes to your environment, as this
  9 # will prevent the need for merging in future updates.
 10 
 11 pathmunge () {
 12     case ":${PATH}:" in
 13         *:"$1":*)
 14             ;;
 15         *)
 16             if [ "$2" = "after" ] ; then
 17                 PATH=$PATH:$1
 18             else
 19                 PATH=$1:$PATH
 20             fi
 21     esac
 22 }
 23 
 24 #判断 /usr/bin/id 是否执行文件,是的话进行执行
 25 if [ -x /usr/bin/id ]; then
 26     if [ -z "$EUID" ]; then
 27         # ksh workaround
 28         EUID=`id -u`
 29         UID=`id -ru`
 30     fi
 31     USER="`id -un`"
 32     LOGNAME=$USER
 33     MAIL="/var/spool/mail/$USER"
 34 fi
 35 
 36 # Path manipulation 环境变量操作
 37 if [ "$EUID" = "0" ]; then 
#root用户
 38     pathmunge /sbin
 39     pathmunge /usr/sbin
 40     pathmunge /usr/local/sbin
 41 else
#普通用户的
 42     pathmunge /usr/local/sbin after
 43     pathmunge /usr/sbin after
 44     pathmunge /sbin after
 45 fi
 46 
 47 HOSTNAME=`/bin/hostname 2>/dev/null`
 48 HISTSIZE=1000
 49 if [ "$HISTCONTROL" = "ignorespace" ] ; then
 50     export HISTCONTROL=ignoreboth
 51 else
 52     export HISTCONTROL=ignoredups
 53 fi
 54 
 55 export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
 56 
 57 # By default, we want umask to get set. This sets it for login shell
 58 # Current threshold for system reserved uid/gids is 200
 59 # You could check uidgid reservation validity in
 60 # /usr/share/doc/setup-*/uidgid file
 61 if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
 62     umask 002
 63 else
 64     umask 022
 65 fi
 66 #可以看到这里开始执行/etc/profile.d/*.sh  所有的文件
 67 for i in /etc/profile.d/*.sh ; do
 68     if [ -r "$i" ]; then
 69         if [ "${-#*i}" != "$-" ]; then
 70             . "$i"
 71         else
 72             . "$i" >/dev/null 2>&1
 73         fi
 74     fi
 75 done
 76 
 77 unset i
 78 unset -f pathmunge
 79 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin

 
 

vim /etc/profile.d/lang.sh

内容如下:

# /etc/profile.d/lang.sh - set i18n stuff


sourced=0


if [ -n "
$LANG" ]; then
    saved_lang="$LANG"
    [ -f "$HOME/.i18n" ] && . "$HOME/.i18n" && sourced=1
    LANG="$saved_lang"
    unset saved_lang
else

#因为$LANG环境变量没有设置,我们可以看到接下来执行这里
    for langfile in /etc/sysconfig/i18n "$HOME/.i18n" ; do
        [ -f $langfile ] && . $langfile && sourced=1
    done
fi


if [ "$sourced" = 1 ]; then
    [ -n "$LANG" ] && export LANG || unset LANG
    [ -n "$LC_ADDRESS" ] && export LC_ADDRESS || unset LC_ADDRESS
    [ -n "$LC_CTYPE" ] && export LC_CTYPE || unset LC_CTYPE
    [ -n "$LC_COLLATE" ] && export LC_COLLATE || unset LC_COLLATE
    [ -n "$LC_IDENTIFICATION" ] && export LC_IDENTIFICATION || unset LC_IDENTIFICATION
    [ -n "$LC_MEASUREMENT" ] && export LC_MEASUREMENT || unset LC_MEASUREMENT
    [ -n "$LC_MESSAGES" ] && export LC_MESSAGES || unset LC_MESSAGES
    [ -n "$LC_MONETARY" ] && export LC_MONETARY || unset LC_MONETARY
    [ -n "$LC_NAME" ] && export LC_NAME || unset LC_NAME
    [ -n "$LC_NUMERIC" ] && export LC_NUMERIC || unset LC_NUMERIC
    [ -n "$LC_PAPER" ] && export LC_PAPER || unset LC_PAPER
    [ -n "$LC_TELEPHONE" ] && export LC_TELEPHONE || unset LC_TELEPHONE
    [ -n "$LC_TIME" ] && export LC_TIME || unset LC_TIME
    if [ -n "$LC_ALL" ]; then
       if [ "$LC_ALL" != "$LANG" ]; then
         export LC_ALL
       else
         unset LC_ALL
       fi
    else
       unset LC_ALL
    fi
    [ -n "$LANGUAGE" ] && export LANGUAGE || unset LANGUAGE
    [ -n "$LINGUAS" ] && export LINGUAS || unset LINGUAS
    [ -n "$_XKB_CHARSET" ] && export _XKB_CHARSET || unset _XKB_CHARSET


    consoletype=$CONSOLETYPE
    if [ -z "$consoletype" ]; then
      consoletype=$(/sbin/consoletype stdout)
    fi
    if [ -n "$LANG" ]; then
      case $LANG in
        *.utf8*|*.UTF-8*)
        if [ "$TERM" = "linux" ]; then
            if [ "$consoletype" = "vt" ]; then
                case $LANG in
                        ja*) LANG=en_US.UTF-8 ;;
                        ko*) LANG=en_US.UTF-8 ;;
                        si*) LANG=en_US.UTF-8 ;;
                        zh*) LANG=en_US.UTF-8 ;;
                        ar*) LANG=en_US.UTF-8 ;;
                        fa*) LANG=en_US.UTF-8 ;;
                        he*) LANG=en_US.UTF-8 ;;
                        en_IN*) ;;
                        *_IN*) LANG=en_US.UTF-8 ;;
                esac
            fi
        fi
        ;;
        *)
        if [ "$TERM" = "linux" ]; then
            if [ "$consoletype" = "vt" ]; then
                case $LANG in
                        ja*) LANG=en_US ;;
                        ko*) LANG=en_US ;;
                        si*) LANG=en_US ;;
                        zh*) LANG=en_US ;;
                        ar*) LANG=en_US ;;
                        fa*) LANG=en_US ;;
                        he*) LANG=en_US ;;
                        en_IN*) ;;
                        *_IN*) LANG=en_US ;;
                esac
            fi
        fi
        ;;
      esac
    fi


    unset SYSFONTACM SYSFONT consoletype
fi
unset sourced
unset langfile


vim /etc/syscconfig/i18n         内容如下:

LANG="en_US.UTF-8" 设置了环境变量
SYSFONT="latarcyrheb-sun16"                  


2.当切换用户(如 su命令) 或者更换shell类型,则流程如下:

 /etc/profile  ->~/.bash_profile -> ~/.bashrc -> /etc/bashrc  -> /etc/profile.d/*sh

vim ~/.bash_profile 内容如下:

# .bash_profile


# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi


# User specific environment and startup programs


PATH=$PATH:$HOME/bin


export PATH
从这里我们可以看到最后环境变量PATH从这里导出,所以如果我们要更改PATH环境变量可以在这个文件修改,注意这只针对当前用户


vim ~/.bashrc 内容如下:

# .bashrc
# User specific aliases and functions


alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

从上面我们可以看到这里是别名的导出,所以我们如果要自定义自己的别名可以从这个文件增加

注意也是当前登录用户生效
# Source global definitions

最后这里调用了/etc/bashrc
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
~                


vim /etc/bashrc 内容如下;

35   shopt -s checkwinsize
 36   [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
这里定义了我们的shell 提示
 37   # You might want to have e.g. tty in prompt (e.g. more virtual machines)
 38   # and console windows
 39   # If you want to do so, just add e.g.
 40   # if [ "$PS1" ]; then
 41   #   PS1="[\u@\h:\l \W]\\$ "
 42   # fi
 43   # to your custom modification shell script in /etc/profile.d/ directory
 44 fi
 45 
 46 if ! shopt -q login_shell ; then # We're not a login shell 如果没有登录shell的情况下执行
 47     # Need to redefine pathmunge, it get's undefined at the end of /etc/profile
 48     pathmunge () {
 49         case ":${PATH}:" in
 50             *:"$1":*)
 51                 ;;
 52             *)
 53                 if [ "$2" = "after" ] ; then
 54                     PATH=$PATH:$1
 55                 else
 56                     PATH=$1:$PATH
 57                 fi
 58         esac
 59     }


流程可以按照分析1的流程进行分析                            1,25          Top

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值