Linux su command: switch between users (and contain su su - differentiated)

su is the simplest user handover command, may be implemented by switching the identity of any command, including switching from ordinary users as root, the root user is switched from the switching between the ordinary users and ordinary users.

 

Switching between ordinary users and ordinary users to switch to the root user, you need to know the other's password, only enter the correct password in order to achieve switching; switching from the root user to another user, password without knowing each other, it can be switched directly to success.

Su command basic format is as follows:

[Root @ localhost ~] # su [option] username

Options:

  • -: The current user can not only switch to specify the identity of the user, while the use of the work environment also switch the user's environment for this purpose (including the PATH variable, MAIL variables, etc.), use the - option to omit the user name, the default will switch to the root user.
  • -l: same - similar use, that is, while switching user identity, complete switch work environments, you need to add user accounts to be behind the switch.
  • -p: represents changes to the specific identity of the user, but does not change the current working environment (without the use of switch user profiles).
  • -m: -p and the same;
  • -c command: Switch user to perform only one command, automatically switch back after the execution, this option is usually with a command to be executed.

【Example 1】

[lamp @ localhost ~] $ su -root
Password: <- Enter the root password
# "-" represents an environment variable associated with the switching, can not be omitted

[Example 2]

[lamp @ localhost ~] $ whoami
LAMP
# current I LAMP
[lamp @ localhost ~] $ su - -c "useradd user1" root
password:
# does not switch to root, but run useradd user1 add user
[lamp @ localhost ~ ] $ whoami
LAMP
# I LAMP
[LAMP @ localhost ~] $ grep "user1 '/ etc / passwd
userl: the X-: 502: 504 :: / Home / user1: / bin / bash
#user users have been added

Example 2 except like this, the user can execute a command automatically switch back immediately, otherwise users will not automatically switched handover, only the exit command to be switched manually, for example:

[LAMP @ localhost ~] $ whoami
LAMP
# current I LAMP
[LAMP @ localhost ~] $ su - lamp1
Password: <- Enter lamp1 user password
# to switch to lamp1 user's work environment
[LAMP @ localhost ~] $ whoami
lamp1
# do nothing, immediately exit switch environment
[LAMP1 @ localhost ~] $ exit
Zimbabwe Logout
[LAMP @ localhost ~] $ whoami
LAMP

su sum su - target-ku, separately

Note that when using the su command, there is - and is not - is completely different - while switching option indicates that the user's identity, even the current use of environment variables switch to the specified user. We know that the environment variable is used to define the operating system environment, so if there is no system environment with the user identity switch, many commands can not be executed properly.

For example, the average user lamp is switched by the su command to become root, but did not use - the option, under such circumstances, although appears to be the root user, but the system is still the $ PATH environment variable lamp (rather than the root) , so the current work environment, does not include / sbin, / usr / sbin and so save path superuser command, which led to many administrative commands can not be used. Not only that, when the root user accepts the message, you will find the user receives a message lamp, because the environment variable $ MAIL have not switched.

Beginners can understand the differences between them, that is - the option to switch user identity more thoroughly; on the contrary, only a part of the switch, which can cause some problems running the command or error (for example, can not use the service command) appears.

By the following example, and can be seen su su intuitive - differences:

[LAMP @ localhost ~] $ whoami
LAMP
# query user, I LAMP
[LAMP @ localhost ~] $ su root
Password:
<- Enter the root password
# to switch to root, but not switched environment variable. Note: ordinary users to switch to the root password is required
[root @ localhost ~] # env | grep lamp
# viewing environment variable, extract the line containing lamp of
the USER = lamp
# username or lamp, rather than root
the PATH = / usr / lib / qt -3.3 / bin: / usr / local / bin: / bin: / usr / bin: / usr / local / sbin: / usr / sbin: / sbin: / Home / LAMP / bin
# command PathFinder does not contain superuser path
mAIL = / var / spool / mail / lamp
PWD = / Home / lamp
LOGNAME = lamp
# mailbox, home directory, user name or a lamp current

It can be seen without the use of su - in the case, although the identity of the user to switch successfully, but still use the environment variables of the original user, switching is not complete.

Published 33 original articles · won praise 0 · Views 384

Guess you like

Origin blog.csdn.net/linuxanz/article/details/103653602