购买
下载掌阅APP,畅读海量书库
立即打开
畅读海量书库
扫码下载掌阅APP

3.3.2 用户环境初始化文件

对于用户的工作环境,在系统启动后就进行相关的初始化工作,因此用户通过安全认证并登录系统后就可以进行工作。

系统对用户工作环境的初始化是通过读取多个相关文件中的配置参数来进行的,这些文件主要包括/etc/profile、~/.bash_profile、~/.bashrc和/etc/bashrc等。本小节将对这些文件的基本作用进行简单介绍。

1.配置文件/etc/profile

/etc/profile的参数用于配置系统环境变量,这些变量对系统有效,可以说该文件是配置系统环境变量不可缺少的。

系统为用户初始化工作环境时,当系统重启及用户首次登录时,系统都会读取该配置文件的参数,并从/etc/profile.d/目录的配置文件中搜集shell的相关设置信息,使得用户登录后就可以正常对系统的相关资源进行操作。

以下是/etc/profile文件的配置内容(省略部分内容):


……
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
    umask 002
else
    umask 022
fi
 
for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then 
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done
 
unset i
unset -f pathmunge
 
if [ -n "${BASH_VERSION-}" ] ; then
        if [ -f /etc/bashrc ] ; then
                # Bash login shells run only /etc/profile
                # Bash non-login shells run only /etc/bashrc
                # Check for double sourcing is done in /etc/bashrc.
                . /etc/bashrc
       fi
fi

在该文件中新增变量参数时,建议把新参数添加到文件的末尾处,并在添加后使用source命令重新加载该文件,这样新增的变量就可以立刻使用。

2.配置文件~/.bash_profile

~/.bash_profile用于记录用户自定义shell,在该文件中定义的相关参数仅对当前的用户有效,也就是说系统给每个用户都创建这个文件,并支持用户使用该文件设置自己专有的变量参数,这些配置参数在系统每次重启或启动时就执行该文件,并从中获取相关的参数来初始化工作环境。

以下是该文件的配置信息:


# .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

3.配置文件~/.bashrc

~/.bashrc中包含专用于用户的命令别名设置和定义初始化环境的必要配置文件,该文件中定义的相关信息仅对当前用户有效,该文件是在用户登录或打开新的shell时被执行的。以下是该文件的配置信息:


# .bashrc
 
# User specific aliases and functions
 
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
 
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

4.配置文件/etc/bashrc

用户登录系统时使用的shell默认是bash,在用户的bash被打开时,系统就执行该文件来初始化相关的参数。以下是该文件的配置信息(部分配置信息): X50TleJ7yRxOa5lCQGWPYI3qhxPK865xQsK3ENOgDFP2ijSsKVIgMuSSqSND6eDG


……
    if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
       umask 002
    else
       umask 022
    fi
 
    SHELL=/bin/bash
    # Only display echos from profile.d scripts if we are no login shell
    # and interactive - otherwise just process them to set envvars
    for i in /etc/profile.d/*.sh; do
        if [ -r "$i" ]; then
            if [ "$PS1" ]; then
                . "$i"
            else
                . "$i" >/dev/null
            fi
        fi
    done
 
    unset i
    unset -f pathmunge
  fi
 
fi
# vim:ts=4:sw=4

点击中间区域
呼出菜单
上一章
目录
下一章
×