正确的切换方式:
0 1 2 3 |
[byrd@nginx ~]$ su - Password: [root@nginx ~]# whoami root |
正确的环境变量:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[root@nginx ~]# env HOSTNAME=note.t4x.org SHELL=/bin/bash TERM=xterm HISTSIZE=1000 USER=root MAIL=/var/spool/mail/root PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin PWD=/root HOME=/root LOGNAME=root CVS_RSH=ssh LESSOPEN=|/usr/bin/lesspipe.sh %s G_BROKEN_FILENAMES=1 _=/bin/env |
不正确的切换方式:
0 1 2 3 |
[root@nginx ~]# su - byrd [byrd@nginx ~]$ su root [root@nginx byrd]# whoami root |
不正确的环境变量:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[root@nginx byrd]# env HOSTNAME=note.t4x.org TERM=xterm SHELL=/bin/bash HISTSIZE=1000 USER=byrd PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/byrd/bin MAIL=/var/spool/mail/byrd PWD=/home/byrd HOME=/root SHLVL=2 LOGNAME=byrd CVS_RSH=ssh LESSOPEN=|/usr/bin/lesspipe.sh %s G_BROKEN_FILENAMES=1 _=/bin/env [root@nginx byrd]# |
主要区别与用户的环境变量:
0 1 2 3 4 5 6 7 8 |
[root@nginx byrd]# su - byrd [byrd@nginx ~]$ su root Password: [root@nginx byrd]# env | grep 'byrd' USER=byrd PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/byrd/bin MAIL=/var/spool/mail/byrd PWD=/home/byrd LOGNAME=byrd |
0 1 2 3 |
[byrd@nginx ~]$ su - Password: [root@nginx ~]# env | grep 'byrd' [root@nginx ~]# |
已经切换到root,但是环境变量依然是byrd的环境变量。因此正确的切换root的用户方式为su - $users,而不是su $users,特别是$PATH,但是若是5.X系列,则区别非常明显。涉及到login-shell与non-login shell相关知识。
申明:本文由BYRD原创,参考了鸟哥教程(基于Centos6.4 X64),未经许可禁止转载!SourceByrd's Weblog-https://note.t4x.org/environment/centos-su-different/
SourceByrd's Weblog-https://note.t4x.org/environment/centos-su-different/
申明:除非注明Byrd's Blog内容均为原创,未经许可禁止转载!详情请阅读版权申明!