Home >System Tutorial >LINUX >How to switch users in linux command line

How to switch users in linux command line

下次还敢
下次还敢Original
2024-04-11 17:45:17583browse

There are three ways to switch users in the Linux command line: use the su command (su - [username]), use the sudo command (sudo -u [username] [command]) or use the runuser command (runuser -l [username]). All methods require the target user's password and may require the -H flag in some cases.

How to switch users in linux command line

How to switch users in the Linux command line

Question:How to switch users in the Linux command line Switch users mid-line?

Answer:

You can switch users in the Linux command line through the following methods:

Use the su command

<code>su - [username]</code>

where[username] is the target user. For example, to switch to user tom, enter:

<code>su - tom</code>

Using the sudo command

The sudo command allows execution of commands as an administrator. To switch to another user and execute a specific command, you can use the following format:

<code>sudo -u [username] [command]</code>

For example, to switch to user bob and create a file, execute:

<code>sudo -u bob touch testfile.txt</code>

Use the runuser command

The runuser command is specifically used to switch users and provides more secure options than the su command. The syntax is as follows:

<code>runuser -l [username]</code>

where the -l flag specifies the user login shell. For example, to switch to user alice, enter:

<code>runuser -l alice</code>

Tip:

  • In all methods, the target user password is required to switch.
  • In some cases it may be necessary to use the -H flag to preserve the hosting user's home directory and environment variables.
  • Use these commands with caution as they can grant access to other user accounts.

The above is the detailed content of How to switch users in linux command line. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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