To change the current user in a Linux environment, you can use the su (substitute user) command or the sudo command. Here’s how to use both:
Using su Command
-
Switch to Another User: To switch to another user, use the following command:
su - usernameReplace
usernamewith the actual username you want to switch to. The-option ensures that you also switch to the user's environment. -
Enter Password: You will be prompted to enter the password for the specified user.
Using sudo Command
-
Run a Command as Another User: If you want to run a command as another user without switching the entire session, you can use:
sudo -u username commandReplace
usernamewith the target user andcommandwith the command you want to execute. -
Enter Your Password: You will need to enter your own password (not the target user's password) if you have the necessary permissions.
Example
To switch to the user john, you would use:
su - john
Or to run a command as john:
sudo -u john ls /home/john
Note
- Ensure you have the necessary permissions to switch users, especially when using
suorsudo. - If you are using
suto switch to the root user, you can simply usesuwithout specifying a username.
