Linux-Bash配置

1. bash登录欢迎信息

issue

bash登陆后显示的欢迎信息由/etc/issue,/etc/issue.net(远程登录)设定。
例如,文件内容为:

[root@iZwz9ayvrn26ptadymu9ymZ ~]# cat /etc/issue
\S
Kernel \r on an \m

那么登录时显示的信息为:

CentOS Linux7 (Core)
Kernel 3.10.0-693.5.2.el7.x86_64 on an x86_64

issue文件内可以设置的参数有:

代码 含义
\d 本地端时间的日期
\l 显示第几个终端机接口
\m 显示硬件等级
\n 显示主机网络名称
\O 显示domain name
\r 操作系统的版本(相当于 uname -r)
\t 显示本地端时间的时间
\S 操作系统名称
\v 操作系统的版本

motd

登录后可以显示一些通知信息,在/etc/motd中设。

2.bash中的环境配置文件

2.1 login shell和non-login shell

  • login shell:取得bash需要完整的登录流程。
  • non-login shell:取得bash接口的方法不需要重复登录的操作。

在login shell中,登陆后读取两个配置文件:

  1. /etc/profile:系统全局配置文件
  2. ~/.bash_profile~/.bash_login~/.profile:使用者个人配置文件

2.2 /etc/profile

全局配置文件,设定的变量主要有:

  • PATH:根据UID决定PATH要不要含有sbin的系统指令目录

    if [ "$EUID" = "0" ]; then
        pathmunge /usr/sbin
        pathmunge /usr/local/sbin
    else
        pathmunge /usr/local/sbin after
        pathmunge /usr/sbin after
    fi
    
  • MAIL:依据账号设定使用者的mailbox到/var/spool/mail账号名

  • USER:根据用户的账号设定改变量

  • HOSTNAME:根据主机hostname指令决定该变量内容

    HOSTNAME=`/usr/bin/hostname 2>/dev/null`
    
  • HISTORYSIZE:历史指令记录数量

  • unmask:包括root默认为022而一般用户为002

    if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
        umask 002
    else
        umask 022
    fi
    

此外,改文件还会引入其他的数据。

  • /etc/profile.d/*.sh
    /etc/profile 中以下代码引入
for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

这些文件规范了bash操作接口的颜色,语系,ll,ls,vi,which等命令的别名。如果要在全局设定共享命令,可以再这个文件夹下面建立扩展名为.sh的文件。

  • /etc/locale.conf
    由/etc/profile.d/lang.sh引入。
    对LANG/LC_ALL等变量进行设定,决定了bash使用何种语系。

  • /usr/share/bash-completion/completions/*

  • 由/etc/profile.d/bash_completion.sh引入。

  • [tab]能够进行命令补齐,文件名补齐,指令的选项/参数补齐。就是在这个目录中找到相应的指令来处理的。

2.3 ~/.bash_profile

bash读取/etc/profile并执行之后,会读取用户个人配置文件,按照如下顺序查找,并读取找到的第一个:

  1. ~/.bash_profile
  2. ~/.bash_login
  3. ~/.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变量进行了补充,还读取了bashrc文件

那么,login shell的配置文件读取流程为:


Linux-Bash配置_第1张图片
login shell配置文件读取流程

2.3 ~/.bashrc

用户个人的配置信息存放在~/.bashrc中,其内容一般为:

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

如果有修改,需要主要后重新登录才会生效。
或者用source或者小数点(.)符号重新载入

srouce ~/.bashrc
. ~/.bashrc

可以看到~/.bashrc最后调用了/etc/bashrc文件,这是CentOS特有的,其功能为:

  • 依据不同的UID规范出unmask的值
  • 依据不同的UID规范出提示字符
  • 调用 /etc/profile.d/*.sh的设定

2.4 其他配置文件

  • /etc/man_db.conf
    man命令的man page路径
  • ~/.bash_history
    上次登录的历史指令
  • ~/.bash_logout
    注销bash后,系统执行的任务

3 terminal环境设定

3.1 stty

stty(setting tty)可以查看或者设定terminal的输入按键含义(另起笔记学习)。
查看当前stty参数:

[root@iZwz9ayvrn26ptadymu9ymZ ~]# stty -a
speed 38400 baud; rows 26; columns 69; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = ;
eol2 = ; swtch = ; start = ^Q; stop = ^S; susp = ^Z;
rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl
ixon -ixoff -iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0
bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop
-echoprt echoctl echoke

部分解释为:

  • intr:发送interrupt(中断)信号给正在run的进程
  • quit:发送quit信号给正在run的进程
  • erase:向后删除字符
  • kill:删除目前指令行的文字
  • eof:End of file 结束输入
  • start:程序停止之后,重启其output
  • stop:停止目前屏幕输出
  • susp:发送terminal stop信号给正在run的程序

设定参数的命令为:

stty [指令] [按键]

3.2 set

set可以显示所有的变量,可以设置输出指令输入/输出的环境(另起笔记学习),其格式为

set [-uvCHhmBx]

你可能感兴趣的:(Linux-Bash配置)