linux系统命令su和su - 区别验证
以下内容为reed个人见解,不保证内容的准确性,仅供参考讨论,如有错误之处,万望提出!
由于工作需要,经常在各个用户之间切换,所以经常用到这个命令: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@yunwei14 ~]$ pwd
/home/reed
[reed@yunwei14 ~]$ id
uid=510(reed) gid=510(reed) groups=510(reed)
[reed@yunwei14 ~]$ su
口令:
[root@yunwei14 reed]# pwd
/home/reed
[root@yunwei14 reed]# id
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)
[root@yunwei14 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@yunwei14 reed]# useradd test
bash: useradd: command not found
[root@yunwei14 reed]#
例子1用[su]命令切换root用户,从例子看出的确是切换到root用户了,但是工作目录和环境变量竟然还是reed的,用useradd命令竟然提示找不到。 2.例子2,用su - 切换root用户
[reed@yunwei14 ~]$ pwd
/home/reed
[reed@yunwei14 ~]$ id
uid=510(reed) gid=510(reed) groups=510(reed)
[reed@yunwei14 ~]$ su -
口令:
[root@yunwei14 ~]# pwd
/root
[root@yunwei14 ~]# id
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)
[root@yunwei14 ~]# 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@yunwei14 ~]# useradd test
[root@yunwei14 ~]#
转载于:https://blog.51cto.com/luweikai/1029922
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
