红联Linux门户
Linux帮助

Linux系统命令su和su-区别验证

发布时间:2014-08-06 10:59:40来源:linux网站作者:reeddeer

以下内容为reed个人见解,不保证内容的准确性,仅供参考讨论,如有错误之处,万望提出!

由于工作需要,Linux下经常在各个用户之间切换,所以经常用到这个命令:su -,因为一直用这个命令,所以也没留意su后面为什么要加一个横杠,不加的话有什么区别。今天脑海里一直浮现su和su - 有什么区别,既然有疑问,就要解决之,哪怕是最基本最简单的问题。


A.用info su命令查看帮助如下(不用翻译啦,大概看的明白吧):
1)默认的su,也就是不加参数
By default, `su' does not change the current directory.  It sets the
environment variables `HOME' and `SHELL' from the password entry for
USER, and if USER is not the super-user, sets `USER' and `LOGNAME' to
USER.  By default, the shell is not a login shell.


2)su加'-'/'-l'/'--login'参数
`-'
`-l'
`--login'
Make the shell a login shell.  This means the following.  Unset all
environment variables except `TERM', `HOME', and `SHELL' (which
are set as described above), and `USER' and `LOGNAME' (which are
set, even for the super-user, as described above), and set `PATH'
to a compiled-in default value.  Change to USER's home directory.
Prepend `-' to the shell's name, intended to make it read its
login startup file(s).  Additionaly `DISPLAY' and `XAUTHORITY'
environment variables are preserved as well for PAM functionality.


B.再看看2个例子:
1.例子1,现有reed普通用户,用su切换root用户。

[reed@linux ~]$ pwd
/home/reed
[reed@linux ~]$ id
uid=510(reed) gid=510(reed) groups=510(reed)
[reed@linux ~]$ su
口令:
[root@linux reed]# pwd
/home/reed
[root@linux reed]# id
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)
[root@linux reed]# echo $PATH
/usr/kerberos/sbin:/usr/lib/qt-3.3/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/java/jdk1.5.0_11/bin:/usr/local/mysql/bin:/usr/apache/bin:/home/reed/bin
[root@linux reed]# useradd test
bash: useradd: command not found
[root@linux reed]#

例子1用[su]命令切换root用户,从例子看出的确是切换到root用户了,但是工作目录和环境变量竟然还是reed的,用useradd命令竟然提示找不到。


2.例子2,用su - 切换root用户

[reed@linux ~]$ pwd
/home/reed
[reed@linux ~]$ id
uid=510(reed) gid=510(reed) groups=510(reed)
[reed@linux ~]$ su -
口令:
[root@linux ~]# pwd
/root
[root@linux ~]# id
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)
[root@linux ~]# echo $PATH
/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/java/jdk1.5.0_11/bin:/usr/local/mysql/bin:/usr/apache/bin:/root/bin
[root@linux ~]# useradd test
[root@linux ~]#

例子2用[su - ]命令切换root用户,从例子看出已切换到root用户,工作目录和环境变量也是root的,用useradd也创建用户成功。


到此,通过翻阅相关资料和例子看出,总结如下:

su:只能切换到root用户权限,但环境变量还是切换前用户的环境变量,读取的变量配置方式为 non-login shell的方式,这种方式很多原本的变量不会被改变,执行命令时要输入绝对路径。

su -:若要完整的切换到root,必须要使用su -或者su -l, 才会连同 PATH/USER/MAIL 等变量都转成root的环境,这里使用的是login shell方式。


到了这里,大家可能又有疑问了,什么是Non-login shell和login shell呢?直接引用鸟哥的资料:

login shell:取得 bash 时需要完整的登陆流程的,就称为 login shell。举例来说,你要由 tty1 ~ tty6 登陆,需要输入用户的账号与密码,此时取得的 bash 就称为『 login shell 』啰;

non-login shell:取得 bash 接口的方法不需要重复登陆的举动,举例来说,(1)你以 X window 登陆 Linux 后, 再以 X 的图形化接口启动终端机,此时那个终端接口并没有需要再次的输入账号与密码,那个 bash 的环境就称为 non-login shell了。(2)你在原本的 bash 环境下再次下达 bash 这个命令,同样的也没有输入账号密码, 那第二个 bash (子程序) 也是 non-login shell 。