歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux系統命令su和su - 區別驗證

Linux系統命令su和su - 區別驗證

日期:2017/2/28 15:29:03   编辑:Linux教程

以下內容為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@linuxidc ~]$ pwd
/home/reed
[reed@linuxidc ~]$ id
uid=510(reed) gid=510(reed) groups=510(reed)
[reed@linuxidc ~]$ su
口令:
[root@linuxidc reed]# pwd
/home/reed
[root@linuxidc reed]# id
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)
[root@linuxidc 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@linuxidc reed]# useradd test
bash: useradd: command not found
[root@linuxidc reed]#

例子1用[su]命令切換root用戶,從例子看出的確是切換到root用戶了,但是工作目錄和環境變量竟然還是reed的,用useradd命令竟然提示找不到。

Copyright © Linux教程網 All Rights Reserved