[Simple understanding] sudo and su in ubuntu

Reference:
https://blog.csdn.net/liberty12345678/article/details/87686284

https://cloud.tencent.com/developer/article/1721753

1. Simple understanding of su

1.su is switch user, which is the abbreviation of switch user.

2. Two formats of su

su -l 用户名(-l为login,即登陆,可省)      切换用户后,同时切换到新用户的工作环境中。
su 用户名                                                                    切换用户后,不改变原用户的工作目录,及其他环境变量目录。

3. If you do not specify a user name, the default is root
, so the command to switch to the identity of root is:

su		(切换用户后,不切换环境)
su -		(切换用户后,切换环境)

su root
su - root
su -l root

4. When root uses su to switch to other identities, there is no need to enter a password

2. Simple understanding of sudo su

1. sudo allows an authorized user to run a command in the role of super user or other user . (What can and cannot be done is specified by the security policy.)

2.sudo command parameters: -u <user> Use the specified user as the new identity. If this parameter is not added, root will be used as the new identity by default.

3. So sudo su means that sudo allows you to run the su command in the role of root, that is, it allows you to execute the command to switch users in the role of root and switch to the user root .





If you can understand the above, you can quit. Let’s explain it with actual operation


1. Look at the blue box below, it is the su command, because I have not set a password for root, so the authentication has failed, that is, the switch to the root user failed 2. The red box is the
sudo su command, and the switch to the root user is successful (sudo command It should let you enter your own password, I have entered it before)

First, why can't su, but sudo su can switch root users?
Because you don’t know the root password, you can’t switch to root
because the sudo command first gives you root privileges, you can run a command, you run the command to switch users, that is, su,
and because root uses su to switch to other identities. No password is required, so you can switch to root

insert image description here

Second, when su exits root, it displays exit
su - when exiting root, it displays logout
These are the differences between whether to switch the working environment, which can be understood as
.
su has the root authority
su - is to switch to the root user

insert image description here

How to set root initial password?

sudo passwd

How to exit after ubuntu enters the root authority?

Ctrl+D

Guess you like

Origin blog.csdn.net/tfnmdmx/article/details/129767467