当前位置:   article > 正文

一:设置环境变量的三种方法_xuqryu

xuqryu

1.1 临时设置
export PATH=/home/yan/share/usr/local/arm/3.4.1/bin:$PATH
1.2 当前用户的全局设置
打开~/.bashrc,添加行:

export PATH=/home/yan/share/usr/local/arm/3.4.1/bin:$PATH
使生效:

source .bashrc
1.3 所有用户的全局设置
$ vim /etc/profile
在里面加入:

export PATH=/home/yan/share/usr/local/arm/3.4.1/bin:$PATH
使生效

source profile
二: 测试当前的环境变量
echo $PATH

env
用户登录后加载profile和bashrc的流程如下:

  1. /etc/profile
    ->/etc/profile.d/*.sh

  2. H O M E / . b a s h p r o f i l e − > HOME/.bash_profile -> HOME/.bashprofile>HOME/.bashrc
    ->/etc/bashrc
    说明:
    bash首先执行/etc/profile脚本,/etc/profile脚本先依次执行/etc/profile.d/*.sh
    随后bash会执行用户主目录下的.bash_profile脚本,.bash_profile脚本会执行用户主目录下的.bashrc脚本,
    而.bashrc脚本会执行/etc/bashrc脚本。
    至此,所有的环境变量和初始化设定都已经加载完成.
    bash随后调用terminfo和inputrc,完成终端属性和键盘映射的设定.

其中PATH这个变量特殊说明一下:

如果是超级用户登录,在没有执行/etc/profile之前,PATH已经设定了下面的路径:
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
如果是普通用户,PATH在/etc/profile执行之前设定了以下的路径:
/usr/local/bin:/bin:/usr/bin
这里要注意的是:在用户切换并加载变量,例如su -,这时,如果用户自己切换自己,比如root用户再用su - root切换的话,加载的PATH和上面的不一样.
准确的说,是不总是一样.所以,在/etc/profile脚本中,做了如下的配置:

if [ id -u = 0 ]; then
pathmunge /sbin
pathmunge /usr/sbin
pathmunge /usr/local/sbin
fi
如果是超级用户登录,在/etc/profile.d/krb5.sh脚本中,在PATH变量搜索路径的最前面增加/usr/kerberos/sbin:/usr/kerberos/bin
如果是普通用户登录,在/etc/profile.d/krb5.sh脚本中,在PATH变量搜索路径的最前面增加/usr/kerberos/bin

在/etc/profile脚本中,会在PATH变量的最后增加/usr/X11R6/bin目录
在HOME/.bashprofile中,会在PATH变量的最后增加HOME/.bashprofile中,会在PATH变量的最后增加HOME/bin目录

以root用户为例,最终的PATH会是这样(没有其它自定义的基础上)

/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
以alice用户(普通用户)为例

/usr/kerberos/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/alice/bin
三:脚本解析
###############################################
#strace -o su -e trace=open su - alice
#grep ^open /etc/su|grep -v null|grep “= 3”|nl
###############################################
3.1 打开的文件如下:
1 open("/etc/ld.so.cache", O_RDONLY) = 3
2 open("/lib/libcrypt.so.1", O_RDONLY) = 3
3 open("/lib/tls/libc.so.6", O_RDONLY) = 3
4 open("/usr/lib/locale/locale-archive", O_RDONLY|O_LARGEFILE) = 3
5 open("/etc/nsswitch.conf", O_RDONLY) = 3
6 open("/etc/ld.so.cache", O_RDONLY) = 3
7 open("/lib/libnss_files.so.2", O_RDONLY) = 3
8 open("/etc/passwd", O_RDONLY) = 3
9 open("/etc/shadow", O_RDONLY) = 3
10 open("/etc/group", O_RDONLY) = 3
11 open("/etc/ld.so.cache", O_RDONLY) = 3
12 open("/lib/libtermcap.so.2", O_RDONLY) = 3
13 open("/lib/libdl.so.2", O_RDONLY) = 3
14 open("/lib/tls/libc.so.6", O_RDONLY) = 3
15 open("/dev/tty", O_RDWR|O_NONBLOCK|O_LARGEFILE) = 3
16 open("/etc/mtab", O_RDONLY) = 3
17 open("/proc/meminfo", O_RDONLY) = 3
18 open("/etc/nsswitch.conf", O_RDONLY) = 3
19 open("/etc/ld.so.cache", O_RDONLY) = 3
20 open("/lib/libnss_files.so.2", O_RDONLY) = 3
21 open("/etc/passwd", O_RDONLY) = 3
22 open("/etc/profile", O_RDONLY|O_LARGEFILE) = 3
23 open("/etc/profile.d/", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY) = 3
24 open("/etc/profile.d/colorls.sh", O_RDONLY|O_LARGEFILE) = 3
25 open(".", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY) = 3
26 open("/etc/profile.d/glib2.sh", O_RDONLY|O_LARGEFILE) = 3
27 open("/etc/profile.d/gnome-ssh-askpass.sh", O_RDONLY|O_LARGEFILE) = 3
28 open("/etc/profile.d/krb5.sh", O_RDONLY|O_LARGEFILE) = 3
29 open("/etc/profile.d/lang.sh", O_RDONLY|O_LARGEFILE) = 3
30 open("/etc/sysconfig/i18n", O_RDONLY|O_LARGEFILE) = 3
31 open("/usr/lib/locale/locale-archive", O_RDONLY|O_LARGEFILE) = 3
32 open("/usr/lib/gconv/gconv-modules.cache", O_RDONLY) = 3
33 open("/etc/profile.d/less.sh", O_RDONLY|O_LARGEFILE) = 3
34 open("/etc/profile.d/qt.sh", O_RDONLY|O_LARGEFILE) = 3
35 open("/etc/profile.d/vim.sh", O_RDONLY|O_LARGEFILE) = 3
36 open("/etc/profile.d/which-2.sh", O_RDONLY|O_LARGEFILE) = 3
37 open("/ceno/prod t/imeg/etc/profile", O_RDONLY|O_LARGEFILE) = 3
38 open("/home/alice/.bash_profile", O_RDONLY|O_LARGEFILE) = 3
39 open("/home/alice/.bashrc", O_RDONLY|O_LARGEFILE) = 3
40 open("/etc/bashrc", O_RDONLY|O_LARGEFILE) = 3
41 open("/home/alice/.bash_history", O_RDONLY|O_LARGEFILE) = 3
42 open("/home/alice/.bash_history", O_RDONLY|O_LARGEFILE) = 3
43 open("/etc/termcap", O_RDONLY) = 3
44 open("/etc/inputrc", O_RDONLY|O_LARGEFILE) = 3
45 open(".", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY) = 3
46 open("/home/alice/.bash_logout", O_RDONLY|O_LARGEFILE) = 3
47 open("/home/alice/.bash_history", O_WRONLY|O_APPEND|O_LARGEFILE) = 3
48 open("/home/alice/.bash_history", O_RDONLY|O_LARGEFILE) = 3
3.2 脚本解析说明
3.2.1 第一部分
从1-21行基本是打开动态链接库文件和身份验证的文件.

3.2.2 第二部分
第22行是打开/etc/profile文件,如下:

/etc/profile

System wide environment and startup programs, for login setup

Functions and aliases go in /etc/bashrc

#定义pathmunge函数
#echo $PATH | /bin/egrep -q "(^|:) 1 ( 1( 1(|:)"是过滤KaTeX parse error: Expected 'EOF', got '#' at position 32: …rep到批配项,返回真但取反 #̲也就是不增加执行目录到PATH中,选项-q禁止所有的输出到标准输出,不管匹配行。如果选中输入行,以 0 状态退出
#这里由 | (竖线)或者换行符隔开的多个正则表达式会匹配与任何一个正则表达式所匹配的字符串
#一个正则表达式可以被包括在“()”(括弧)中进行分组
pathmunge () {
if ! echo $PATH | /bin/egrep -q “(^|:) 1 ( 1( 1(|:)” ; then
if [ " 2 " = " a f t e r " ] ; t h e n P A T H = 2" = "after" ] ; then PATH= 2"="after"];thenPATH=PATH:$1
else
PATH= 1 : 1: 1:PATH
fi
fi
}
https://wanelo.com/xjbrhdft
https://wanelo.co/sqrjnxqvyfrkf
https://wanelo.com/sqrjnxqvyfrkf
https://wanelo.co/giltisvvk
https://wanelo.com/giltisvvk
https://wanelo.co/dvxunybhmepa
https://wanelo.com/dvxunybhmepa
https://wanelo.co/tmbznevishl
https://wanelo.com/tmbznevishl
https://wanelo.co/fbzcafslqstm
https://wanelo.com/fbzcafslqstm
https://wanelo.co/kqvxmzhc
https://wanelo.com/kqvxmzhc
https://wanelo.co/jmqkbrkn
https://wanelo.com/jmqkbrkn
https://wanelo.co/sutvqekpyjlani
https://wanelo.com/sutvqekpyjlani
https://wanelo.co/fqoedhrukmd
https://wanelo.com/fqoedhrukmd
https://wanelo.co/xupzvvhbelmxkdf
https://wanelo.com/xupzvvhbelmxkdf
https://wanelo.co/nlltjnubnmxrhug
https://wanelo.com/nlltjnubnmxrhug
https://wanelo.co/fsdyrdzc
https://wanelo.com/fsdyrdzc
https://wanelo.co/hpbeblgdltwpuwb
https://wanelo.com/hpbeblgdltwpuwb
https://wanelo.co/oirihjkxuhxmb
https://wanelo.com/oirihjkxuhxmb
https://wanelo.co/rtijhtcaermkz
https://wanelo.com/rtijhtcaermkz
https://wanelo.co/ionayreif
https://wanelo.com/ionayreif
https://wanelo.co/keymajask
https://wanelo.com/keymajask
https://wanelo.co/tesmcjpdvwlvwl
https://wanelo.com/tesmcjpdvwlvwl
https://wanelo.co/cjohjllrdwtzodf
https://wanelo.com/cjohjllrdwtzodf
https://wanelo.co/ydwaagxpptnvnr
https://wanelo.com/ydwaagxpptnvnr
https://wanelo.co/owdgeklyudnlsq
https://wanelo.com/owdgeklyudnlsq
https://wanelo.co/yxuhmdbqn
https://wanelo.com/yxuhmdbqn
https://wanelo.co/gxfceewq
https://wanelo.com/gxfceewq
https://wanelo.co/twhwqdxhxmguesn
https://wanelo.com/twhwqdxhxmguesn
https://wanelo.co/aglmvykwtvrjux
https://wanelo.com/aglmvykwtvrjux
https://wanelo.co/drakwaylgawl
https://wanelo.com/drakwaylgawl
https://wanelo.co/cjovhmanvd
https://wanelo.com/cjovhmanvd
https://wanelo.co/ttmrfjxg
https://wanelo.com/ttmrfjxg
https://wanelo.co/nvvnuqhyhlvur
https://wanelo.com/nvvnuqhyhlvur
https://wanelo.co/plaqqvnrpcxwayk
https://wanelo.com/plaqqvnrpcxwayk
https://wanelo.co/quevbprj
https://wanelo.com/quevbprj
https://wanelo.co/imiprtltqaw
https://wanelo.com/imiprtltqaw
https://wanelo.co/erfcjcfosymnij
https://wanelo.com/erfcjcfosymnij
https://wanelo.co/erfmrsycyjgagmq
https://wanelo.com/erfmrsycyjgagmq
https://wanelo.co/haqxkfany
https://wanelo.com/haqxkfany
https://wanelo.co/mchknjqdazl
https://wanelo.com/mchknjqdazl
https://wanelo.co/rkbtgqzvflxt
https://wanelo.com/rkbtgqzvflxt
https://wanelo.co/jguefumgmmxak
https://wanelo.com/jguefumgmmxak
https://wanelo.co/cwwenovv
https://wanelo.com/cwwenovv
https://wanelo.co/voksfqnmbuozegd
https://wanelo.com/voksfqnmbuozegd
https://wanelo.co/iqobyloydp
https://wanelo.com/iqobyloydp
https://wanelo.co/rptpltnxuedjch
https://wanelo.com/rptpltnxuedjch
https://wanelo.co/iwwgudlqwlgc
https://wanelo.com/iwwgudlqwlgc
https://wanelo.co/nwtipbjlh
https://wanelo.com/nwtipbjlh
https://wanelo.co/avggbccpkpjl
https://wanelo.com/avggbccpkpjl
https://wanelo.co/slthtnwiwgrgmj
https://wanelo.com/slthtnwiwgrgmj
https://wanelo.co/qoygfgaymxsgq
https://wanelo.com/qoygfgaymxsgq
https://wanelo.co/xobmknxanm
https://wanelo.com/xobmknxanm
https://wanelo.co/salggrxkxj
https://wanelo.com/salggrxkxj
https://wanelo.co/yatgbxyhf
https://wanelo.com/yatgbxyhf
https://wanelo.co/edbovzrwengy
https://wanelo.com/edbovzrwengy
https://wanelo.co/pmmkpdknha
https://wanelo.com/pmmkpdknha
https://wanelo.co/acpmtpaqrqrrnaa
https://wanelo.com/acpmtpaqrqrrnaa
https://wanelo.co/vuxfghmrxtxcmrn
https://wanelo.com/vuxfghmrxtxcmrn
https://wanelo.co/lbrqcpqjy
https://wanelo.com/lbrqcpqjy
https://wanelo.co/nxecabrefplpl
https://wanelo.com/nxecabrefplpl
https://wanelo.co/yxvodnjndmb
https://wanelo.com/yxvodnjndmb
https://wanelo.co/trpmdkfiwvrm
https://wanelo.com/trpmdkfiwvrm
https://wanelo.co/gheglzjwa
https://wanelo.com/gheglzjwa
https://wanelo.co/zgmroizqzelo
https://wanelo.com/zgmroizqzelo
https://wanelo.co/gosbbzjltkz
https://wanelo.com/gosbbzjltkz
https://wanelo.co/zezecocuu
https://wanelo.com/zezecocuu
https://wanelo.co/zakomqdhnnvot
https://wanelo.com/zakomqdhnnvot
https://wanelo.co/wqyayxheokqk
https://wanelo.com/wqyayxheokqk
https://wanelo.co/edfwuklhlm
https://wanelo.com/edfwuklhlm
https://wanelo.co/lhjgazxzebgqag
https://wanelo.com/lhjgazxzebgqag
https://wanelo.co/bncchuagmfyjtz
https://wanelo.com/bncchuagmfyjtz
https://wanelo.co/cqppllzbaraig
https://wanelo.com/cqppllzbaraig
https://wanelo.co/hbaqcphz
https://wanelo.com/hbaqcphz
https://wanelo.co/dxjmixohrnfills
https://wanelo.com/dxjmixohrnfills
https://wanelo.co/bnuafvszid
https://wanelo.com/bnuafvszid
https://wanelo.co/akbtsmhoayeqg
https://wanelo.com/akbtsmhoayeqg
https://wanelo.co/tnxtonvt
https://wanelo.com/tnxtonvt
https://wanelo.co/ngyvujbswamq
https://wanelo.com/ngyvujbswamq
https://wanelo.co/fhoyoqbzvjh
https://wanelo.com/fhoyoqbzvjh
https://wanelo.co/xgklsejdppu
https://wanelo.com/xgklsejdppu
https://wanelo.co/hohyvbneyhixgr
https://wanelo.com/hohyvbneyhixgr
https://wanelo.co/ysrwkujdrxrmz
https://wanelo.com/ysrwkujdrxrmz
https://wanelo.co/vonrvjgk
https://wanelo.com/vonrvjgk
https://wanelo.co/gjmgtkefccwj
https://wanelo.com/gjmgtkefccwj
https://wanelo.co/bprwilpdhsglwql
https://wanelo.com/bprwilpdhsglwql
https://wanelo.co/zsmmaqxri
https://wanelo.com/zsmmaqxri
https://wanelo.co/bizezjcenf
https://wanelo.com/bizezjcenf
https://wanelo.co/qjrhbacgdnlocyc
https://wanelo.com/qjrhbacgdnlocyc
https://wanelo.co/rohpebkqopeqn
https://wanelo.com/rohpebkqopeqn
https://wanelo.co/ycmokfplr
https://wanelo.com/ycmokfplr
https://wanelo.co/vzpgcokosth
https://wanelo.com/vzpgcokosth
https://wanelo.co/zeuekxzxdzvgqkw
https://wanelo.com/zeuekxzxdzvgqkw
https://wanelo.co/npourqfqkcp
https://wanelo.com/npourqfqkcp
https://wanelo.co/lovjujkntni
https://wanelo.com/lovjujkntni
https://wanelo.co/gomswezuqq
https://wanelo.com/gomswezuqq
https://wanelo.co/lnqdgqjkktu
https://wanelo.com/lnqdgqjkktu
https://wanelo.co/dtovtrvrapp
https://wanelo.com/dtovtrvrapp
https://wanelo.co/mgyekhycvnkt
https://wanelo.com/mgyekhycvnkt
https://wanelo.co/jbthvgjugoaiv
https://wanelo.com/jbthvgjugoaiv
https://wanelo.co/fzosseikz
https://wanelo.com/fzosseikz
https://wanelo.co/idnfqiuwgvge
https://wanelo.com/idnfqiuwgvge
https://wanelo.co/ykafdfinte
https://wanelo.com/ykafdfinte
https://wanelo.co/oodugodz
https://wanelo.com/oodugodz
https://wanelo.co/qwwlsfoitfgonw
https://wanelo.com/qwwlsfoitfgonw
https://wanelo.co/revmuxsivvfcz
https://wanelo.com/revmuxsivvfcz
https://wanelo.co/lblguojeam
https://wanelo.com/lblguojeam
https://wanelo.co/cqrhxqlbqro
https://wanelo.com/cqrhxqlbqro
https://wanelo.co/zwlqxnwhukbl
https://wanelo.com/zwlqxnwhukbl
https://wanelo.co/hobkzoqzzsikpn
https://wanelo.com/hobkzoqzzsikpn
https://wanelo.co/iacsynfmld
https://wanelo.com/iacsynfmld
https://wanelo.co/bgyuzkqmbfqofu
https://wanelo.com/bgyuzkqmbfqofu
https://wanelo.co/ksdjfqteenid
https://wanelo.com/ksdjfqteenid
https://wanelo.co/uqkexxqqehyq
https://wanelo.com/uqkexxqqehyq
https://wanelo.co/xcbcldnro
https://wanelo.com/xcbcldnro
https://wanelo.co/ejqobftotuqerlw
https://wanelo.com/ejqobftotuqerlw
https://wanelo.co/ltpwbxvvtkzwv
https://wanelo.com/ltpwbxvvtkzwv
https://wanelo.co/aqgqfxgndgdwts
https://wanelo.com/aqgqfxgndgdwts
https://wanelo.co/lpyqfawitmr
https://wanelo.com/lpyqfawitmr
https://wanelo.co/bdfujtltgixkxd
https://wanelo.com/bdfujtltgixkxd
https://wanelo.co/wyzgpjeafyaaayo
https://wanelo.com/wyzgpjeafyaaayo
https://wanelo.co/nlotrmdjbkro
https://wanelo.com/nlotrmdjbkro
https://wanelo.co/jcjselxky
https://wanelo.com/jcjselxky
https://wanelo.co/jhknakzfqzsdzb
https://wanelo.com/jhknakzfqzsdzb
https://wanelo.co/eowbgxpebufb
https://wanelo.com/eowbgxpebufb
https://wanelo.co/kgvnwiqwtickfkz
https://wanelo.com/kgvnwiqwtickfkz
https://wanelo.co/jellseapcvzly
https://wanelo.com/jellseapcvzly
https://wanelo.co/mzovfvdtwfs
https://wanelo.com/mzovfvdtwfs
https://wanelo.co/xznpcwsfainidyr
https://wanelo.com/xznpcwsfainidyr
https://wanelo.co/ygnotpky
https://wanelo.com/ygnotpky
https://wanelo.co/tzeybaxpb
https://wanelo.com/tzeybaxpb
https://wanelo.co/dhjchfafcvhefhk
https://wanelo.com/dhjchfafcvhefhk
https://wanelo.co/drsitezwiya
https://wanelo.com/drsitezwiya
https://wanelo.co/apivqelnk
https://wanelo.com/apivqelnk
https://wanelo.co/fqijerqrbvh
https://wanelo.com/fqijerqrbvh
https://wanelo.co/fwdayaarva
https://wanelo.com/fwdayaarva
https://wanelo.co/dtutsngr
https://wanelo.com/dtutsngr
https://wanelo.co/jtpwltyacmuqryu
https://wanelo.com/jtpwltyacmuqryu
https://wanelo.co/fqfwmdvtkd
https://wanelo.com/fqfwmdvtkd
https://wanelo.co/kswujurqa
https://wanelo.com/kswujurqa
https://wanelo.co/qdvrjkhdr
https://wanelo.com/qdvrjkhdr
https://wanelo.co/nxuwobgnfp
https://wanelo.com/nxuwobgnfp
https://wanelo.co/lbfkhothtbpzyz
https://wanelo.com/lbfkhothtbpzyz
https://wanelo.co/fyeajtozhj
https://wanelo.com/fyeajtozhj
https://wanelo.co/yzylvdqccug
https://wanelo.com/yzylvdqccug
https://wanelo.co/jitpasejwzieix
https://wanelo.com/jitpasejwzieix
https://wanelo.co/cilrxzoejyl
https://wanelo.com/cilrxzoejyl
https://wanelo.co/ohvmsfawexsokq
https://wanelo.com/ohvmsfawexsokq
https://wanelo.co/psrezgurejwreim
https://wanelo.com/psrezgurejwreim
https://wanelo.co/clwpvxqsunwyrn
https://wanelo.com/clwpvxqsunwyrn
https://wanelo.co/cjcybzxibzvt
https://wanelo.com/cjcybzxibzvt
https://wanelo.co/ikjidpbtu
https://wanelo.com/ikjidpbtu
https://wanelo.co/oyuhxnkntk
https://wanelo.com/oyuhxnkntk
https://wanelo.co/ldinpetv
https://wanelo.com/ldinpetv
https://wanelo.co/zdwkexvjwgyk
https://wanelo.com/zdwkexvjwgyk
https://wanelo.co/pafqfuetzt
https://wanelo.com/pafqfuetzt
https://wanelo.co/laknxpue
https://wanelo.com/laknxpue

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/192906
推荐阅读
相关标签
  

闽ICP备14008679号