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


以下内容为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@bkjia ~]$ pwd
/home/reed
[reed@bkjia ~]$ id
uid=510(reed) gid=510(reed) groups=510(reed)
[reed@bkjia ~]$ su
口令:

[root@bkjia reed]# pwd
/home/reed
[root@bkjia reed]# id
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)
[root@bkjia 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@bkjia reed]# useradd test
bash: useradd: command not found
[root@bkjia reed]#

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

  • 1
  • 2
  • 下一页

相关内容