What does the linux su password refer to

03-20-2023

The content of this article mainly explains what the linux su password refers to. Interested friends may wish to take a look. The method described in this article is simple, quick and practical. Let the editor take you to learn what the linux su password refers to!


The password of su is the password of the root root user; install Linux or Unix operation When running the system, there will be a prompt for the user to enter the root password, which is also the password for the user to enter after executing the sudo su command. The full name of su is switch user, which allows the user to temporarily change the login identity. Except for root, the user account and password to be changed must be entered when changing.

What does the su password mean?

The password of su is the password of the root root user. When installing the Linux or Unix operating system, There will be a prompt asking you to enter the password of root, which is the password that will appear after executing the sudo su command. Note that this password will not display asterisks or other symbols, and it will always be blank. After the input is completed, return directly. The car is fine.

Introduction to the su command

su (full spelling in English: switch user). In Linux, the su command allows the user to temporarily change the login identity. When changing other than root, the user account and password to be changed must be entered.

Access: All users.

Function: Change user identity, if no user account is specified, it will be changed to root by default.

Syntax


su [-fmp] [-c command] [-s shell] [--help] [- -version] [-] [USER [ARG]]


Parameter description:

  • -f or --fast not Must-read startup file (such as csh.cshrc, etc.), only used for csh or tcsh

  • -m -p or --preserve-environment does not change the environment variable when executing su< /p>

  • -c command or --command=command Change to a user whose account is USER and execute the command (command) before returning to the original user

  • -s shell or --shell=shell specifies the shell to be executed (bash csh tcsh, etc.), the default value is the user (USER) shell in /etc/passwd

  • --help display documentation

  • --version display version information

  • - After the parameter -l or --login is added, it is like re-login as the user. Most of the environment variables (HOME SHELL USER, etc.) are based on the user (USER), and the working directory It will also change, if USER is not specified, the default is root

  • USER to change the user account

  • ARG incoming New shell parameters

Common example:

Example 1: Change account to root and execute ls Exit after the command and return to the original user

Command: su -c ls root

  Change the account to root and execute the ls command Log out and return to the original user.


[root@localhost ~]# su -c ls root public video documentation music anaconda-ks.cfg Template Image Download Desktop initial-setup-ks.cfg


Example 2: Switch User

Command: su root

  testuser switches to root, which is equivalent to changing the user to root but operating in the testuser environment
  Switching from small permissions to large permissions requires entering a password; switching from large permissions to small permissions does not require enter password.
  Switching users can only obtain the user's execution authority, but cannot obtain environment variables.


[testuser@localhost ~]$ whoami //Display current user testuser [testuser@localhost ~]$ pwd //Display the current directory /home/testuser [testuser@localhost ~]$ su root //Switch to root user password: [root@localhost testuser]# whoami root [root@localhost testuser]# pwd /home/testuser


Example 3: Switch users, change environment variables

Command: su - root

  testuser switches to root, which is equivalent to changing the user to root, and the operation is the root environment. That is, change the account to root and change the working directory to root's home directory.
  Switch to the user and obtain the user's environment variables and execution permissions.


[testuser@localhost ~]$ whoami testuser [testuser@localhost ~]$ pwd /home/testuser [testuser@localhost ~]$ su - root password: [root@localhost ~]# whoami root [root@localhost ~]# pwd /root


The difference between su and su -

Note that when using the su command, with- and without- are Completely different, the - option means that while switching the user identity, even the currently used environment variable is switched to the specified user. We know that environment variables are used to define the operating system environment, so if the system environment does not switch with the user identity, many commands cannot be executed correctly.

For example, the ordinary user lamp switches to the root user through the su command, but does not use the - option. In this case, although it seems to be the root user, the $PATH environment variable in the system is still lamp (not root), so the current working environment does not contain the storage path of super user commands such as /sbin, /usr/sbin, which makes many administrator commands unusable at all. Not only that, when the root user accepts the mail, it will find that the mail received is from the lamp user, because the environment variable $MAIL has not been switched.

Beginners can understand the difference between them in this way, that is, there is a - option, and switching user identities is more thorough; on the contrary, only a part is switched, which will cause problems or errors in the operation of some commands (eg the service command cannot be used).

Through the following example, you can intuitively see the difference between su and su -:


[lamp@localhost ~]$ whoami lamp #Query user identity, I am lamp 

[lamp@localhost ~]$ su root 

password:<- 

Enter root password 

#Switch to root, but do not switch environment variables. Note: A password is required for ordinary users to switch to root 

[root@localhost ~]# env | grep lamp 

#View environment variables and extract lines containing lamp USER=lamp #The user name is still lamp, not root 

PATH=/usr/lib/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/lamp/bin 

#The path searched by the command does not contain the superuser path 

MAIL=/var/spool/mail/lamp 

PWD=/home/lamp 

LOGNAME=lamp 

#Mailbox, home directory, current user name is still lamp


You can see that without using su -, although the user identity is successfully switched, the environment variable is still used The ones belong to the original user, and the switchover is not complete.


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us