环境变量配置文件
环境变量配置文件
环境变量配置文件:顾名思义就是存放环境变量的文件。
环境变量配置文件中的环境变量,系统任何时候启动都会生效。
source命令
source命令通常用于重新执行刚修改的初始化文件,使之立即生效,而不必注销并重新登录。
语法格式:source [文件]
或 source 和 . 点作用相同
语法格式:. [文件]
常用的环境变量
- HOME 代表用户的家目录。 cd ~ 到自己的家目录, ~ 是一个环境变量
- SHELL 告知,该系统支持那种shell脚本, Linux 默认使用 /bin/bash 的啦!
- HISTSIZE history命令记录多少条命令
- MAIL 使用 mail 命令收信时,系统读取的邮件信箱文件 (mailbox)。
- PATH 运行文件搜寻的路径;目录与目录中间以冒号(:)分隔, 文件搜寻依次按 PATH 变量中配置的目录来查询,所以,目录的顺序也是重要。
- RANDOM『随机随机数』的变量; 大多数的 distributions 都会有随机数生成器,那就是 /dev/random 这个文件。 $RANDOM) 来随机取得随机数值 0~32767 之间的值。
环境变量配置文件
针对所有用户的配置文件:
/etc/profile
/etc/profile.d/*.sh
/etc/bashrc
针对当前登录用户的配置文件:
~/.bash_profile
~/.bashrc
linux中使用[.]点开头命名的文件为隐藏文件。ls -a 或 ll -a 查看
环境变量配置文件作用
配置文件的执行顺序
1. /etc/profile 作用:定义如下环境变量
- USER变量
- LOGNAME变量
- MAIL变量
- PATH变量
- HOSTNAME变量
- HISTSIZE变量
- umask变量
- 调用/etc/profile.d/*.sh文件
pathmunge () {
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
}
# 配置USER LOGNAME MAIL 环境变量
if [ -x /usr/bin/id ]; then
if [ -z "$EUID" ]; then
# ksh workaround
EUID=`id -u`
UID=`id -ru`
fi
USER="`id -un`"
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
fi
# 配置PATH 环境变量 调用 pathmunge $EUID=0 代表root用户
if [ "$EUID" = "0" ]; then
pathmunge /usr/sbin
pathmunge /usr/local/sbin
else
pathmunge /usr/local/sbin after
pathmunge /usr/sbin after
fi
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
export HISTCONTROL=ignoreboth
else
export HISTCONTROL=ignoredups
fi
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
umask 002
else
umask 022
fi
for i in /etc/profile.d/*.sh ; do
if [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then
. "$i"
else
. "$i" >/dev/null
fi
fi
done
unset i
unset -f pathmunge
2. ~/.bash_profile 作用
执行 . ~/.bashrc
修改 PATH
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
3. ~/.bashrc 作用
执行 . /etc/bashrc
User specific aliases and functions: 定义当前用户定义的别名
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
4. /etc/bashrc 作用
[ “$PS1” = “\s-\v\$ " ] && PS1=”[\u@\h \W]\$ " 定义默认登录提示符
# /etc/bashrc
# System wide functions and aliases
# Environment stuff goes in /etc/profile
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
# are we an interactive shell?
if [ "$PS1" ]; then
if [ -z "$PROMPT_COMMAND" ]; then
case $TERM in
xterm*|vte*)
if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
elif [ "${VTE_VERSION:-0}" -ge 3405 ]; then
PROMPT_COMMAND="__vte_prompt_command"
else
PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
fi
;;
screen*)
if [ -e /etc/sysconfig/bash-prompt-screen ]; then
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
else
PROMPT_COMMAND='printf "\033k%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
fi
;;
*)
[ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default
;;
esac
fi
# Turn on parallel history
shopt -s histappend
history -a
# Turn on checkwinsize
shopt -s checkwinsize
[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
# You might want to have e.g. tty in prompt (e.g. more virtual machines)
# and console windows
# If you want to do so, just add e.g.
# if [ "$PS1" ]; then
# PS1="[\u@\h:\l \W]\\$ "
# fi
# to your custom modification shell script in /etc/profile.d/ directory
fi
if ! shopt -q login_shell ; then # We're not a login shell
# Need to redefine pathmunge, it get's undefined at the end of /etc/profile
pathmunge () {
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
}
# By default, we want umask to get set. This sets it for non-login shell.
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`id -gn`" = "`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
# vim:ts=4:sw=4
该配置文件中重新定义了PATH、umask 等的值;
不是重复定义,定义了不使用账号密码登录情况的下PATH、umask 等的值。 We’re not a login shell 文档中已经注释了。
其他配置
注销时生效的环境变量配置文件:~/.bash_logout
历史命令的配置文件:~/.bash_histtory
登录信息
- /etc/issue 本地登陆显示的信息,本地登录前
- /etc/issue.net ==网络登陆(远程登录)==显示的信息,登录后显示,需要由sshd配置
- /etc/motd 通告信息本地登录远程登录都显示,如计划关机时间的警告等,登陆后的提示信息
/etc/issue转义符如下
转义符 | 作用 |
---|---|
\d | 本地端时间的日期 |
\s | 系统的名称 |
\l | 显示登录终端号 ;显示当前tty的名字即第几个tty |
\m | 显示硬体的架构 (i386/i486/i586/i686…) |
\n | 显示主机名 |
\o | 显示 domain name 域名 |
\r | 当前系统内核版本 (相当于 uname -r) |
\t | 显示本地端时间的时间 |
\u | 当前用户序列号 |
\v | 当前系统的版本 |