Centos下面有多個文件可以用於加載環境變量,包括:
全局的配置文件: /etc/profile /etc/profile.d/*.sh
用於用戶初始化的配置文件:/etc/skel/.*
用戶home目錄下的配置文件:.bashrc、.bash_profile
用戶登錄時環境變量的加載順序:
1. 先讀取/etc/profile, 然後根據這個文件讀取其他include的配置文件,如/etc/profile.d/*.sh
2. 根據不同的登錄用戶讀取.bash_profile
3. 根據不同的登錄用戶讀取.bashrc
也就是說/etc/profile中的配置會被.bash_profile覆蓋,.bash_profile會被.bashrc覆蓋。
我比較習慣與在.bashrc中設置自己的環境變量,原因在於這個文件中的配置不會被其他配置文件中的不同配置覆蓋掉,另外一個原因在於每次打開一個shell的時候.bashrc都會重新加載,而.bash_profile只會在登錄的時候加載一次。這就導致了在用su命令切換用戶以後.bash_profile裏面的配置不會生效,當然也可用用su – 來切換用戶,加-可以強制加載.bash_profle文件。
清理用戶帳號並初始化環境變量:
有時候需要批量的清理用戶帳號,並重新初始化,這個時候可以把用戶家目錄下所有文件刪除,然後從/etc/skel目錄下拷貝初始化的配置文件。
shell> rm –frv ~/{*,.??*} //刪除home目錄下所有文件
shell>cp –frv /etc/skel/.??* ~ //初始化環境變量
其中:
* 代表0或多個字符(或數字)
? 代表“一定有”1個字母,這裏用了2個(?),表示點號後面至少有2個字符,可以排除(../),?可以用戶匹配(.)號,如果只有1個?的話:
[tianjing@tianjing-ops Tue Nov 15 07:25 PM ~/test1]$cp -r /etc/skel/.?* .
cp: cannot access `/etc/skel/../cron.d’: Permission denied
cp: cannot open `/etc/skel/../sudoers’ for reading: Permission denied
cp: cannot open `/etc/skel/../security/opasswd’ for reading: Permission denied
cp: cannot open `/etc/skel/../at.deny’ for reading: Permission denied
cp: cannot open `/etc/skel/../default/useradd’ for reading: Permission denied
cp: cannot open `/etc/skel/../iscsi/iscsid.conf’ for reading: Permission denied
cp: cannot open `/etc/skel/../cups/printers.conf’ for reading: Permission denied
cp: cannot access `/etc/skel/../cups/ssl’: Permission denied
[tianjing@tianjing-ops Tue Nov 15 07:40 PM ~/test1]$cp -rfv /etc/skel/.??* .
`/etc/skel/.bash_logout’ -> `./.bash_logout’
`/etc/skel/.bash_profile’ -> `./.bash_profile’
`/etc/skel/.bashrc’ -> `./.bashrc’
`/etc/skel/.emacs’ -> `./.emacs’
`/etc/skel/.mozilla’ -> `./.mozilla’
`/etc/skel/.mozilla/extensions’ -> `./.mozilla/extensions’
`/etc/skel/.mozilla/plugins’ -> `./.mozilla/plugins’
|