歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux基礎知識 >> linux環境變量配置文件

linux環境變量配置文件

日期:2017/3/2 17:15:29   编辑:Linux基礎知識

環境變量配置文件中主要是定義對系統操作環境生效的系統默認環境變量,如PATH等。當你登陸Linux系統啟動一個bash shell時,默認情況下bash會幾個文件中查找命令,bash檢查的啟動文件取決於bash shell的方式。啟動bash shell的方式有三種:

(1).登陸時當作默認登陸的shell

(2).作為非登陸的交互式shell

(3).作為運行腳本的非交互shell

環境變量配置文件:

/etc/profile
/etc/profile.d/*.sh
~/.bash_profile  #每個用戶下面都有
~/.bashrc      #每個用戶下面都有
/etc/bashrc

1.默認登陸shell。

當登陸linux系統時,bash shell會作為默認登陸shell啟動,shell會一次讀取以下文件:

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

/etc/profile的作用:

USER變量 LOGNAME變量 MAIL變量 PATH變量 HOSTNAME變量 HISTSIZE變量 umask變量 調用/etc/profile.d/*.sh文件

/etc/profile文件是系統上默認的bash shell主啟動文件。系統上每個用戶登錄時都會執行這個啟動文件。bash z中有段重要的話:

# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

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

/etc/profile文件有個for語句,逐一訪問/etc/profile.d目錄下的每個文件./etc/profile.d目錄為系統提供了一個存放用戶登錄時要執行的應用專屬的啟動文件的地方。該目錄下的文件基本都跟系統上的特定應用有關。大部分應用會創建兩個自啟文件:bash shell的.sh文件和c shell的.csh文件。lang.sh和lang.csh會判定系統上的默認語言字符集,然後正確的設置LANG變量

~/.bash_profile的作用

1.調用~/.bashrc文件。2.在PATH變量後邊加入了“:$HOME/bin”

[root@future ~]# cat .bash_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

~/.bashrc文件作用:

1.查看/etc目錄下公用的bashrc文件。2.為用戶提供了定制自己的別名和私有腳本函數功能

[root@future ~]# cat .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

/etc/bashrc:

設置了一些環境變量,但並沒有執行export命令讓它們成為全局變量。/etc/bashrc中也有一段重要的話。/etc/bashrc也會去執行/etc/profile.d目錄下的文件。

[root@future ~]# 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.

交互式shell

如果你的shell不是登陸系統時啟動的(比如在命令行提示符下輸入bash),則這種shell叫做交互式shell。交互式shell不能像登陸shell一樣運行,但也提供了命令行提示符來輸入命令。

如果bash是作為交互式shell啟動的,則不會去訪問/etc/profile,而是去用戶的HOME目錄檢查.bashrc是否存在.而上文提過,.bashrc文件有兩個作用。因此通用的/etc/bashrc啟動文件會被系統上每個交互式shell執行。

非交互式shell:

系統執行shell腳本時用的就是這種shell.當每次運行腳本時,仍然要運行特定的啟動文件。因此,bash shell提供了BASH_ENV的環境變量。當shell啟動一個非交互式進程時,它會檢查這個環境變量來查看要執行的啟動文件,如果有指定

則會去執行文件裡的內容。在Linux的發行版中,默認這環境變量並未設置。

其他環境變量文件:

~/.bash_logout:退出時執行,可在文件中加入退出時所要執行的命令

/etc/issue 本地終端歡迎信息

/etc/issue.net 遠程終端歡迎信息

1.轉義符在/etc/issue.net文件中不能使用

2.是否顯示此歡迎信息,由ssh的配置文件/etc/ssh/sshd_config決定,加入Banner /etc/issue.net行才能顯示,需重啟SSH服務。

/etc/motd

不管是本地登錄,還是遠程登陸,都可以顯示此信息

Copyright © Linux教程網 All Rights Reserved