[Shell]Bash变量:环境变量的配置文件和登录信息

-----------------------------------------------------------------------------------------

只有把环境变量放入配置文件中,才能每次开机自动生效。source命令:让配置文件直接生效,而不用注销或重新登录。

  source 配置文件  或者  . 配置文件(等同于 source 配置文件)

【系统中五类环境变量配置文件】

  /etc/profile

  /etc/profile.d/*.sh

  ~/.bash_profile

  ~/.bashrc

  /etc/bashrc

( etc目录内环境变量配置文件对所有用户有效,~开头的只对当前用户有效 )

【配置文件的执行流程】

  /etc/profile ------> ~/.bash_profile ------> ~/.bash_rc ------> /etc/bashrc ------- 命令提示符

    |                                   |

    |--------------> /etc/profile.d/*.sh(加载这里面的所有脚本) <--------|

              |

              |

           /etc/profile.d/lang.sh -----> /etc/locale.conf( LANG="en_US.UTF-8" )

  第一条路: 

    /etc/profile -> /etc/profile.d/*.sh ->

  第二条路:

    vi ~/.bash_profile

# .bash_profile

# Get the aliases and functions ( 如果家目录下有.bashrc, 则执行 )
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/.local/bin:$HOME/bin ( 环境变量追加家目录下的bin目录,我这里的$HOME是 /home/weichen )

export PATH ( 设为环境变量 )

    如果想把某个目录作为系统默认搜索命令的路径,并永久生效,就可以写在这个配置文件中。

    vi ~/.bashrc

# .bashrc

# Source global definitions  ( 如果有/etc/bashrc 文件,则执行 )
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  ( 如果需要定义系统命令别名,可以写在这个配置文件 )

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

    vi /etc/bashrc

定义PS1变量
umask
PATH变量
调用/etc/profile.d/*.sh文件

(此文件仅针对 no login shell)

【总结】

  想让配置永久生效,可以写在上面的任意一个配置文件中,但要注意:是对所有用户生效还是当前用户;后面的文件配置内容将覆盖前面的,除非使用变量叠加。

【其它配置文件和登录信息】

  ~/.bash_logout

  默认是空的,如果想让系统注销时执行一些命令,可以写在这个配置文件中。

  比如:使退出终端时清除历史命令记录,加入 history -c 

  ~/.bash_history

  历史命令的保存文件,只有 history -w 或者 注销登录 才会写到文件中,是系统排错时的重要依据,但是在设置密码后要清空历史命令。

  Shell登录信息:

  1. 本地终端登录信息:/etc/issue

    d        显示当前系统日期

    s        显示操作系统名称

    l         显示登录的终端号,这个比较常用

    m       显示硬件体系结构,如i386、i686等

             显示主机名

    o        显示域名

                显示内核版本

              显示当前系统时间

    u         显示当前登录用户的序列号

  2. 远程终端登录信息:/etc/issue.net

      上面的转义符在/etc/issue.net文件中不能使用

      是否显示此登录信息,由ssh的配置文件/etc/ssh/sshd_config决定,加入“ Banner /etc/issue.net ” 行才能显示(记得重启SSH服务) 

编辑:
vi /etc/ssh/sshd_config

加入:
#Banner none
Banner /etc/issue.net

重启:
service sshd restart

  3. 登录后欢迎信息:/etc/motd

    不管是本地登录还是远程登录,都可以显示此提示信息。

This is Alibaba Cloud Elastic Compute Service !

Warning: If you are not administrater, please logout!
    Otherwise you will take legal responsibility!

Link:http://www.cnblogs.com/farwish/p/4772115.html

原文地址:https://www.cnblogs.com/farwish/p/4772115.html