Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of the su command in linux

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail what is the use of the su command in linux. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Original question: in linux, I log in as root, what's the difference between logging in as another user and then using the su command to switch to the root user? if so, are the other users still in the system after I execute the su command? After performing su, I tried to delete other users, linux prompts, other users logged in? Why

Answer: let me explain it to you. In the unix system, when a user logs in, a shell process is created for each user to provide interactive functions. if you are using bash, then the shell process is bash, the ksh is used, this process is ksh, and so are other shell.

As for the process, in its process PCB (process control block), there are two values: the actual user ID and the effective user ID. The so-called actual user ID refers to the identity in which the process was created, for example: I am currently a root user, and the actual user ID of the process I created is the ID of root, usually 0. The valid user ID refers to the ID of the user who currently has permission to use the process. For example, I have a car and I group it out. The car is owned by me, but the permission is not mine.

In general, the actual user ID and the effective user ID are the same. The su command has such a function, so that a process can run under the authority of the command owner, that is, it can make the actual user ID of the process different from the effective user ID. You can use ls-l to view the su command, its owner's x permission bit must be written in the s (small s), the same is true of the passwd command, they have the same function. This s bit means that the user who executes the command can execute it with the owner permission of the command. If you cancel this s bit, only the owner of su can su.

Now let's explain it further. I hope you can understand it.

When we execute su with root, the su command creates a new shell process with the current shell process as the parent process, this shell process is provided to the user we want to su to, while the root shell process still exists, and vice versa, this is the reason for your question, because the previous user's shell process has not been closed, you can use exit to exit, you can see Instead of logging out, I went back to the previous user. Because they are parent-child processes, the end of the child process certainly does not affect the execution of the parent process. But if we forcibly kill the parent process, we just quit.

In fact, many people do not understand the difference between whoami and whoami, their roles are different.

Who am i displays the actual user ID of the current shell process.

Whomai displays the valid user ID for the current shell process.

In many cases, the two outputs are the same, so much so that we think the two commands have the same effect.

Example: root@yunhw-Aspire-4750G:~# who am i

Root pts/0 2011-12-10 13:13

Root@yunhw-Aspire-4750G:~# whoami

Root

Root@yunhw-Aspire-4750G:~#ps-ef

Root 2939 2935 0 13:18 pts/0 00:00:00 bash

Root@yunhw-Aspire-4750G:~# su-yunhw

Yunhw@yunhw-Aspire-4750G:~$ who am i

Root pts/0 2011-12-10 13:13

Yunhw@yunhw-Aspire-4750G:~$ whoami

Yunhw

Root@yunhw-Aspire-4750G:~#ps-ef

Root 2939 2935 0 13:18 pts/0 00:00:00 bash

Yunhw 2958 2939 0 13:19 pts/0 00:00:00 su-yunhw

Yunhw 2966 2958 7 13:19 pts/0 00:00:00-su

Root@yunhw-Aspire-4750G:~#sudo kill-9 2939

As soon as the order was carried out, it withdrew completely.

This is the end of this article on "what is the use of su commands in linux?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report