linux中切换用户方式su和su -的区别

时间:2021-03-24 16:39:16

Using su

The  su  command allows users to open a terminal window, and from that terminal start a sub shell in which the user has another identity. To perform administrative tasks, for instance, you can log in with a normal user account and type  su  to open a root shell. This brings the benefit that only in the root shell root privileges are used.

If just the command  su  is typed, the username root is implied. But su can be used to run tasks as another user as well. Type  su linda  to open a subshell as the user linda, for example. When using  su  as an ordinary user, you are prompted for a password and after entering that you have acquired the credentials of the target user:

[linda@localhost ~]$ su  Password:

[root@localhost linda]#   
      When using  su , a sub shell is started. This is an environment where you are able to work as the target user account, but environment settings for that user account have not been set. If you need complete access to the entire environment of the target user account, you can use  su -  to start a login shell. If you start a login shell, all scripts that make up the user environment are processed, which makes you work in an environment that is exactly the same as when logging in as that user.   (使用su - 切换到其它用户,不要使用su直接切换)

TIP

Using  su -  is better than using  su . When the  -  is used, a login shell is started, without the  - , some variables may not be set correctly. So, you are better off using  su -  immediately.