Explanation of Linux su commands for switching users

Source: Internet
Author: User

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.

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.