su root和su - root的区别——建议使用su - root

 这两天在练习在linux虚拟机上配置监听器,但是一直报错,怀疑是HOST的设置有问题。
    
      当前HOST设置为主机名,我尝试着设为IP,因此使用su root切换到root下,使用ipconfig命令查询主机的IP地址,但是一直报错,非常纳闷儿,后来在朋友的提醒下,说应该执行 su - root(注意:减号前后都有空格)。果然如他所说,进去后命令顺利执行。
   
      下面是从网上查到的关于两者的解释:
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.

     从中可以看出,区别就在于是否执行.bash_profile,也就是说su只能获得root的执行权限,不能获得环境变量;而su -是切换到root并获得root的环境变量及执行权限。

建议:
有些命令即使你使用su root切换到root用户,仍无法执行,所以切换到root用户时,建议使用su - root。


请使用浏览器的分享功能分享到微信等