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

How to switch users in linux command line

下次还敢
Release: 2024-04-11 17:45:17
Original
582 people have browsed it

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>
Copy after login

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

<code>su - tom</code>
Copy after login

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>
Copy after login

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

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

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>
Copy after login

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

<code>runuser -l alice</code>
Copy after login

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!

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