Home > System Tutorial > LINUX > How to use commands to switch user accounts in Linux

How to use commands to switch user accounts in Linux

下次还敢
Release: 2024-04-11 17:51:18
Original
671 people have browsed it

Switch user accounts in Linux: Use the su command: used to temporarily switch users and retain the current session. Use sudo command: used to switch users with administrator privileges without retaining the current session.

How to use commands to switch user accounts in Linux

Use commands to switch user accounts in Linux

In the Linux operating system, you can use the command line to easily switch user account. The two most common methods are introduced below:

Method 1: su command

The su command allows you to temporarily switch to another user. The syntax is as follows:

<code>su [username]</code>
Copy after login

For example:

<code>su bob</code>
Copy after login
Copy after login

This will switch to user "bob" while retaining the current user session.

Method 2: sudo command

The sudo command allows you to execute commands with administrator privileges, including switching users. The syntax is as follows:

<code>sudo -u [username] [command]</code>
Copy after login

For example:

<code>sudo -u bob whoami</code>
Copy after login

This will execute the "whoami" command as user "bob". Note that you need to enter the current user's password to use the sudo command.

Detailed explanation:

su command:

  • su allows you to temporarily switch to another user while No need to exit the current session.
  • After switching, you will have the permissions of the target user.
  • To return to the original user, type "exit".

sudo command:

  • sudo executes commands with administrator privileges, including switching users.
  • Unlike su, sudo does not retain the current session when switching users.
  • You must enter the current user's password to use sudo.
  • -u option specifies the user to switch to.

Examples:

Here are some examples of switching users using su and sudo:

  • Switching using su To user "bob":
<code>su bob</code>
Copy after login
Copy after login
  • Run the command as user "bob":
<code>sudo -u bob mkdir new_directory</code>
Copy after login
  • Switch back to the original user from user "bob":
<code>exit</code>
Copy after login

The above is the detailed content of How to use commands to switch user accounts in Linux. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template