1. 程式人生 > 實用技巧 >linux系統中su 和 su - 的區別

linux系統中su 和 su - 的區別

1、linux系統中su 和 su - 都是用於登入用於切換的命令

su:切換使用者,環境變數不變,路徑不變

su - : 切換使用者,環境變數隨切換使用者發生改變,回到家目錄

測試 su 命令:

[root@linuxprobe test]# ls
[root@linuxprobe test]# whoami
root
[root@linuxprobe test]# pwd
/home/test
[root@linuxprobe test]# echo $PATH
/root/miniconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@linuxprobe test]# su linuxprobe  ## 直接切換使用者
[linuxprobe@linuxprobe test]$ whoami
linuxprobe
[linuxprobe@linuxprobe test]$ pwd ## 路徑未改變
/home/test [linuxprobe@linuxprobe test]$ echo $PATH ## PATH環境變數未改變 /root/miniconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

2、測試su - 命令

[linuxprobe@linuxprobe test]$ exit  ## 返回root
exit
[root@linuxprobe test]# whoami
root
[root@linuxprobe test]# ls
[root@linuxprobe test]# pwd
/home/test
[root@linuxprobe test]# echo $PATH
/root/miniconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin [root@linuxprobe test]# su - linuxprobe ## 使用 su - 切換 Last login: Wed Oct 21 15:28:46 CST 2020 on pts/0 [linuxprobe@linuxprobe ~]$ whoami linuxprobe [linuxprobe@linuxprobe ~]$ pwd ## 回到使用者linxprobe家目錄 /home/linuxprobe [linuxprobe@linuxprobe
~]$ echo $PATH ## PATH環境變數改變 /usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/linuxprobe/.local/bin:/home/linuxprobe/bin