一、環境變量相關的配置文件
1)/etc/profile
全局(公有)配置,不管是哪個用戶,登錄時都會讀取該文件。
2)/ect/bashrc
Ubuntu沒有此文件,與之對應的是/ect/bash.bashrc
它也是全局(公有)的
bash執行時,不管是何種方式,都會讀取此文件。
3)~/.profile
若bash是以login方式執行時,讀取~/.bash_profile,若它不存在,則讀取~/.bash_login,若前兩者不存在,讀取~/.profile。
另外,圖形模式登錄時,此文件將被讀取,即使存在~/.bash_profile和~/.bash_login。
4)~/.bash_login
若bash是以login方式執行時,讀取~/.bash_profile,若它不存在,則讀取~/.bash_login,若前兩者不存在,讀取~/.profile。
5)~/.bash_profile
Unbutu默認沒有此文件,可新建。
只有bash是以login形式執行時,才會讀取此文件。通常該配置文件還會配置成去讀取~/.bashrc。
6)~/.bashrc
當bash是以non-login形式執行時,讀取此文件。若是以login形式執行,則不會讀取此文件。
7)~/.bash_logout
注銷時,且是longin形式,此文件才會讀取。也就是說,在文本模式注銷時,此文件會被讀取,圖形模式注銷時,此文件不會被讀取。
二、文件執行順序說明
下面是在本機的幾個例子:
1)圖形模式登錄時,順序讀取:/etc/profile和~/.profile
2)圖形模式登錄后,打開終端時,順序讀取:/etc/bash.bashrc和~/.bashrc
3)文本模式登錄時,順序讀取:/etc/bash.bashrc,/etc/profile和~/.bash_profile
4)從其它用戶su到該用戶,則分兩種情況:
- 如果帶-l參數(或-參數,--login參數),如:su -l username,則bash是lonin的,它將順序讀取以下配置文件:/etc/bash.bashrc,/etc/profile和~/.bash_profile。
- 如果沒有帶-l參數,則bash是non-login的,它將順序讀取:/etc/bash.bashrc和~/.bashrc
5)注銷時,或退出su登錄的用戶,如果是longin方式,那么bash會讀取:~/.bash_logout
6)執行自定義的shell文件時,若使用“bash -l a.sh”的方式,則bash會讀取行:/etc/profile和~/.bash_profile,若使用其它方式,如:bash a.sh, ./a.sh,sh a.sh(這個不屬於bash shell),則不會讀取上面的任何文件。
7)上面的例子凡是讀取到~/.bash_profile的,若該文件不存在,則讀取~/.bash_login,若前兩者不存在,讀取~/.profile。
三、sudo執行已有命令提示command not found
有些命令在定義了PATH的情況下,普通用戶可以調用而加上sudo卻調用不了,報command not found的錯誤。
原因是系統在編譯sudo的時候加入了–with-secure-path,這個選項。
–with-secure-path[=PATH]
Path used for every command run from sudo(8). If you don’t trust the people running sudo to have a sane PATH environment variable you may want to use this. Another use is if you want to have the “root path” be separate from the “user path.” You will need to customize the path for your site. NOTE: this is not applied to users in the group specified by –with-exemptgroup. If you do not specify a path, “/bin:/usr/ucb:/usr/bin:/usr/sbin:/sbin:/usr/etc:/etc” is used.
要解決這個問題有幾種方法:
1)重新編譯sudo,不加–with-secure-path選項
2)執行命令時使用絕對路徑
如:sudo /home/test/code/go/bin/dlv
3)在環境配置文件里加一個alias【推薦】
例如在/etc/bash.bashrc里添加:alias sudo='sudo env PATH=$PATH:/usr/local/sbin:/sbin'
我們可以在root權限下先查看一下PATH和當前用戶PATH的區別,然后再追加路徑。
重新登錄后生效,這樣我們在執行sudo dlv命令時就等於sudo env PATH=$PATH dlv