Detailed explanation of Linux switch user su instruction misunderstanding

Source: Internet
Author: User

1. Background

For security reasons, general accounts are generally recommended for general operations in a Linux environment

This is because the root account permissions are too large, often due to careless misoperation caused by system errors, serious will also cause the system to crash and collapse.


It is recommended that you switch to the root account only when you need to do so.

Su is the operating instruction of the switching user in the Linux environment


2.su Instruction

Many friends use this instruction often in the following form:

Example: Switch from general account moonlit to root account

$ su


So what's wrong with doing this?

We'll look at the comparison of the data before and after the account switching.

[[Email protected] ~] $

Execute the following statements before switching to see the variables in your environment that contain moonlit:

$ env | grep ' moonlit '

The screen appears as follows:

User=moonlit
Mail=/var/spool/mail/moonlit
path=/usr/java/jdk1.7.0_60/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/ Sbin:/home/moonlit/bin
Pwd=/home/moonlit
Home=/home/moonlit
Logname=moonlit


To perform a switchover:

$ su

Password:

After entering the password, the screen is converted to:

[[Email protected] ~] #

Enter #pwd to view the current path

/home/moonlit

Hey, how is it still in the home directory of the moonlit account?

Re-enter # env | grep ' moonlit '

Screen output:

User=moonlit
path=/usr/java/jdk1.7.0_60/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/ Sbin:/home/moonlit/bin
Mail=/var/spool/mail/moonlit
Pwd=/home/moonlit
Logname=moonlit


Did you see that? Environment Wood has changed.

Because there is no change to the root environment, it can cause many root commands to be wrong or invalid.


To fully convert the user to root, you can use the following command:

[email protected] ~]$ su -

Type the root password and change to [[email protected] ~]#

#pwd一下:

/root

Again # env | grep ' moonlit ' a bit:

No God, no horse.

# env a bit:

This time the screen will be all the variables displayed, more, not listed, you can see the current home directory, PATH, mail and so on have all been changed to the root of the variable.


Finally, you can release #exit to exit the current root user and return to moonlit.


3. Summary

In fact no-when the system is in accordance with the Non-login shell way to switch users,

Plus-When the system is switched according to the login shell.

Detailed explanation of Linux switch user su instruction misunderstanding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.