When you switch users in Linux you can seamlessly transitions between accounts without logging out, this is useful for administrative tasks or when you need to access files with different owners. When switching users, always ensure the target user has the necessary permissions for the task.
You can check your current session with the command whoami which returns what user you are currently using. .
sudo allows you to execute commands with the privileges of another user, typically the root user, and is commonly used when you need to perform administrative tasks.
1. After that, open a terminal.
2. Now, type sudo su – “username” to switch to the other user.
3. Finally, authenticate by entering your own password when prompted.
With this method, you do not need the target user’s password (unless you are switching to a restricted user). It’s helpful when you need temporary root access, without issuing commands directly as the root user or, if you’re working with users that need elevated privileges.
The su (substitute user) command allows you to switch users directly within your current session, requiring the password of the target user.
1. To begin, open a terminal.
2. Next, type su – “username” to switch to the target user.
3. Then, enter the target user’s password when prompted.
This is a direct method for switching to another user. Unlike sudo, you will need the password of the user you’re switching to.
It’s often used for tasks where you need to assume the role of another user without logging out and test what access or permissions they have. This is a very powerful tool for administrators to help diagnose issues or malware, specifically privilege escalation.
As a bonus the login command can be used for switching users on local systems, especially in multi-tenant or multi-user environments.
1. First, open your terminal.
2. Next, type login “username” to log in as the other user.
3. Then, enter the target user’s password when prompted.
Usually this method is only necessary when you are accessing a system directly which isn’t supported on our VPS hosting but its good to know. It will start a new session for the target user.