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

Summary of the difference among sudo, su and su-commands in Linux

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Preface

In Linux systems, because of the excessive authority of root, it is generally not used. Login root is used to perform administrative tasks only in some special cases. In general, temporary use of root permissions often uses su and sudo commands.

The su command is a tool for switching users. How do you understand it? For example, we log in as an ordinary user tom, but to add user tasks and execute useradd, the tom user does not have this permission, which is precisely owned by root. There are two solutions: one is to quit tom users and log in as root users again, but this is not the best solution; the other is that we do not need to quit tom users, we can use su to switch to root to add users, and then quit root after the task is completed.

We can see that switching through su is of course a better way; you can switch between users through su, while super-privileged user root does not need a password to switch to a normal or virtual user, while an ordinary user switching to any other user requires password authentication.

Sudo

Sudo is a privilege management mechanism that relies on / etc/sudoers, which defines which user can execute what administrative commands as an administrator.

Format: sudo-u USERNAME COMMAND

By default, only root users can execute sudo commands on the system. Root users are required to use the visudo command to edit the configuration file / etc/sudoers of sudo in order to authorize other ordinary users to execute sudo commands.

There is a process for sudo to run:

1)。 When a user runs sudo, the system looks in the / etc/sudoers file to see if the user has permission to run sudo

2)。 If the user has permission to run sudo. So let the user enter the user's own password, and notice that the user's own password is entered here.

3)。 Assume that password is correct. Start with the command after sudo. Root does not need to enter password to run sudo and switch to the same identity as the operator. There is no need to enter password.

Su

Su is switch user, that is, the abbreviation for switching users.

Su is the simplest identity switch name, with su we can switch no matter what users, generally su-username, and then enter password on ok, but root does not need to enter password when switching to other identities with su.

There are two formats:

Su-l USERNAME (- l is login, short for login)

Su USERNAME

If you do not specify USERNAME (user name), the default is root, so the command to switch to the identity of root is: su-root or su -, su root or su.

Su USERNAME differs from su-USERNAME in the following ways:

After su-USERNAME switches users, it also switches to the work environment of the new users. After switching users, su USERNAME does not change the original user's working directory and other environment variable directories.

How do I set the initial su password?

When we enter su, we will find that we need to enter a password. We try to enter the password of our current user and find failure.

But we don't know what the su password is? In fact, the initial su password can be set in the following ways:

Sudo passwd

After setting up, enter the su command and type the password just set to enter ~

Su-

When the su -, su-l or su-- login commands change identity, they also change the working directory, as well as HOME,SHELL,USER,LOGNAME. In addition, the PATH variable is changed. By default, the su-command is converted to a root user.

The "su command" without arguments does not change the current working directory and HOME,SHELL,USER,LOGNAME. It's just that I have access to root.

Note: su-uses root's password, while sudo su uses user's password

Summary

The above is the whole content of this article, I hope that the content of this article has a certain reference and learning value for your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.

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