-----------------------------------------------------------------------------------------
只有把環境變量放入配置文件中,才能每次開機自動生效。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等
\n 顯示主機名
\o 顯示域名
\r 顯示內核版本
\t 顯示當前系統時間
\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