当环境变量被写入配置文件,重启机器后该环境变量依然生效
1.环境变量的存放位置
以下是系统默认的5类存放环境变量的配置文件
其中存放在 /etc/profile /etc/profile.d/*.sh 和/etc/bashrc文件中的环境变量对所有用户生效
存放在~/.bash_profile和~/.bashrc下的环境变量只对当前用户生效(~表示家目录)
注意:修改完配置文件可以通过"source filename"或". filename"(注意中间有空格)命令来保证配置文件的修改立即生效
2.环境变量配置文件的作用
以下是环境变量的调用流程图(如果前后定义两个相同的环境变量,定义在流程后面的变量优先级高,因为后面的会覆盖前面的;如果是以变量叠加的方式体现则不会覆盖,即依然是前面的优先级高)
2.1 /etc/profile的作用
2.2 ~/.bash_profile的作用
2.3 ~/.bashrc的作用
2.4 /etc/bashrc的作用
先查看该文件内容
[root@wenhaijin ~]# cat /etc/bashrc
# /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*)
if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
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 "\033]0;%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 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
# 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 2>&1
fi
fi
done
unset i
unset pathmunge
fi
# vim:ts=4:sw=4
[root@wenhaijin ~]#
该文件中定义的变量有些是在/etc/profile中已经定义了,但是从注释可以看出这边定义的变量是针对没有输入密码登入的用户生效的
# We're not a login shell
# By default, we want umask to get set. This sets it for non-login shell.
3.其他配置文件和登入信息
3.1 注销时生效的环境变量配置文件~/bash_logout
[root@wenhaijin ~]# cat .bash_logout
# ~/.bash_logout
3.2 历史命令的保存文件~/bash_history
当系统出问题需要排查原因的时候可以查看该文件看下执行了什么操作
3.3 shell登入信息
可以通过/etc/issue来自定义本地终端欢迎信息
3.4远程终端欢迎信息
可以通过/etc/issue.net来定义远程终端欢迎信息
###查看远程配置欢迎信息文件
[root@wenhaijin ~]# vim /etc/issue.net
CentOS release 6.5 (Final)
Kernel \r on an \m
###编辑/etc/ssh/sshd_config文件
[root@wenhaijin ~]# vim /etc/ssh/sshd_config
# no default banner path
#Banner none
###加入远程配置文件欢迎信息然后保存退出
Banner /etc/issue.net
###改完后重启ssh服务
[root@wenhaijin ~]# service sshd restart
Stopping sshd: [ OK ]
Starting sshd: [ OK ]
[root@wenhaijin ~]#
改完后再重新进行远程登入,发现已经看到欢迎信息,但是从结果中发现“\r”和“\m”所代表的信息并没有被正确识别,说明这两个变量都只针对本地欢迎信息生效
CentOS release 6.5 (Final)
Kernel \r on an \m
Last login: Thu Apr 13 21:50:30 2017 from 113.87.161.29
Welcome to aliyun Elastic Compute Service!
[root@wenhaijin ~]#
3.5本地和远程都能被识别的转义符文件信息/etc/motd
不过/etc/issue和/etc/issue.net是在登入前后都能看到的欢迎信息,而/etc/motd是要在登入后才能看到的欢迎信息