su root 和 su - root 区别

in Linux with 0 comment

su 命令默认参数即为 root ,因此 su rootsu - root 区别即为 susu - 的区别。

搜索即可得到:

su - root  is   the same as su -

just like login as root, then the shell is login shell,
which mean it will expericene a login process,
usually .bash_profile and .bashrc will be sourced

su  root    is    the same as su
like you open an interactive shell in root name,
then only .bashrc will be sourced.

举个例子:

cd /home/kane
su root
pwd
/home/kane
exit

su - root  
pwd
/root  

简单理解:

su 只获得根用户的执行权限,不能获得根用户的环境变量。
su - 是切换到根用户并获得根用户的环境变量及执行权限。

区别实质:

实质区别就在于是否执行 .bash_profile,即
su只能获得root的执行权限,不能获得环境变量;
su -是切换到root并获得root的环境变量及执行权限

回复