Explanation of Linux su commands for switching users
1. Background
For the sake of security, we recommend that you use a general account for general jobs in Linux.
This is because the root account has too many permissions and is often prone to system errors due to accidental misoperations. Serious root account causes system paralysis and crash.
Therefore, we recommend that you switch to the root account only when necessary.
Su is the Operation Command for switching users in Linux.
2. su command
When using this command, many friends usually use the following form:
For example, switch from the general account moon.pdf to the root account
$ Su
So what's wrong with this?
Let's take a look at the data comparison before and after account switching.
[Moonlogs @ host ~] $
Run the following statement to check the environment variables that contain moonlogs:
$ Env | grep 'moonregion'
The screen is shown as follows:
USER = moonworkflow
MAIL =/var/spool/mail/moon.pdf
PATH =/usr/java/jdk1.7.0 _ 60/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin: /usr/local/sbin:/usr/sbin:/home/moonlogs/bin
PWD =/home/moon.pdf
HOME =/home/moon.pdf
LOGNAME = moonlogs
Perform switchover:
$ Su
Password:
After the password is entered, the screen is converted:
[Root @ host ~] #
Enter # pwd to view the current path
/Home/moontasks
Why is it still in the home directory of the moontracing account?
Enter # env | grep 'moonregion'
Screen output:
USER = moonworkflow
PATH =/usr/java/jdk1.7.0 _ 60/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin: /usr/local/sbin:/usr/sbin:/home/moonlogs/bin
MAIL =/var/spool/mail/moon.pdf
PWD =/home/moon.pdf
LOGNAME = moonlogs
See it? The environment has changed.
Because the root environment is not changed, many root commands may be wrong or invalid.
To fully convert a user to the root user, run the following command:
[Moonlogs @ host ~] $ Su-
Enter the root password and change it to [root @ host ~]. #
# Pwd:
/Root
# Env | grep 'moonregion:
No.
# Env:
At this time, the screen will display all the variables, which are relatively large and not listed one by one. You can see that the current home directory, PATH, MAIL, and so on have all changed to the root variable.
Finally, you can issue # exit to exit the current root user and return moonlogs.
3. Summary
In fact, when there is no-, the system switches users in non-login shell mode,
When "-" is added, the system Switches Based on the login shell.