Home >System Tutorial >LINUX >How to switch users in linux command line
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 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:
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!