The graphical user interface in Linux provides a simple dropdown menu for account changes, but many users still wonder how to switch users on Linux efficiently. Whether you are sharing a workstation or testing different environments, switching user accounts is a fundamental skill. This guide covers every method, from the GUI to the terminal, so you can move between accounts without losing your work or compromising security.
Linux offers multiple ways to handle user switching, and each has its own advantages. You might prefer a quick GUI logout, a seamless terminal command, or a way to keep your current session running. We will explore all these options step by step, ensuring you find the method that fits your workflow.
Understanding User Switching In Linux
Before we get into the commands, it helps to know what “switching users” actually means. In Linux, each user has their own home directory, settings, and running processes. When you switch users, you are essentially changing the active user session, either by logging out or by starting a new session alongside the existing one.
There are two main scenarios: switching to another user while staying logged in (using su or sudo) and switching to a full desktop session (using the GUI or login). This article covers both.
How To Switch Users On Linux Using The GUI
For most desktop users, the graphical interface is the easiest way to switch accounts. The exact steps depend on your desktop environment (GNOME, KDE, Xfce, etc.), but the general idea is the same.
Using The System Menu
Look for the system menu in the top-right or bottom-left corner of your screen. It usually shows your username or a power icon. Click it, and you will see options like “Switch User” or “Log Out.”
- Click the system menu (often a gear or user icon).
- Select “Switch User” from the dropdown.
- The login screen appears, allowing you to choose another user.
- Enter the password for the new user, and their desktop loads.
Your original session remains active in the background. This is perfect if you need to check something quickly without closing your apps.
Using The Login Screen
If you are already at the login screen, you can simply select a different user from the list. Some distros show all available users, while others require you to type the username manually.
This method is straightforward, but it logs out the current user completely. Any unsaved work in the previous session will be lost, so always save your files first.
How To Switch Users On Linux Using The Terminal
If you prefer the command line or are managing a headless server, terminal commands are your best friend. The two primary tools are su (substitute user) and sudo (superuser do).
Using The Su Command
The su command lets you switch to another user account. You will need the target user’s password. Here is the basic syntax:
su - username
The hyphen (-) tells Linux to start a login shell, which loads the target user’s environment variables and home directory. Without the hyphen, you stay in your current directory and keep your own environment.
Example: To switch to a user named “john”:
su - john
You will be prompted for john’s password. Once entered, your terminal prompt changes to reflect the new user. To return to your original user, type exit or press Ctrl+D.
Note: If you are root, you can switch to any user without a password using su - username.
Using The Sudo Command
The sudo command is typically used to run a single command as another user, but it can also start a shell. To switch to another user with sudo, use:
sudo -i -u username
This starts a login shell for the specified user. You will need your own sudo password (not the target user’s). This is useful if you have sudo privileges but do not know the other user’s password.
Example:
sudo -i -u john
After entering your password, you are now in john’s environment. Type exit to return.
Using The Login Command
For a full login session (similar to a new terminal login), you can use the login command. This is rarely needed but can be handy in scripts.
login
You will be prompted for a username and password. This logs you out of your current session, so use it carefully.
How To Switch Users On Linux Without A Password
Sometimes you need to switch to another user without knowing their password. This is common for system administrators managing multiple accounts. The only way to do this is by using root privileges.
Using Sudo As Root
If you have sudo access, you can become root first, then switch to any user:
sudo su - username
This combines sudo and su. You enter your sudo password, then become the target user without needing their password.
Alternatively, use:
sudo -i -u username
Both methods work the same way. Remember, you need sudo privileges for this to work.
Using Root Directly
If you are already root, you can switch to any user with:
su - username
No password is required. This is a powerful capability, so use it responsibly.
How To Switch Users On Linux While Keeping Sessions Active
One of the best features of Linux is the ability to keep multiple user sessions running simultaneously. This is called “fast user switching” and is supported by most desktop environments.
Fast User Switching In The GUI
As mentioned earlier, the “Switch User” option in the system menu does exactly this. Your current session stays active, and the new user gets a fresh desktop. You can switch back and forth without losing any work.
This is ideal for shared computers where multiple people need to use the machine at different times.
Using Tmux Or Screen
In the terminal, you can simulate fast user switching using terminal multiplexers like tmux or screen. These tools let you run multiple sessions under different users.
For example, start a tmux session as user “john”:
su - john -c "tmux new -s john_session"
Then detach with Ctrl+B, D. You can later reattach as needed. This is more advanced but very powerful for server management.
How To Switch Users On Linux From The Login Screen
If you are already logged in and want to switch to a completely different user session, you can lock the screen or log out. Most desktop environments have a “Switch User” option that takes you back to the login screen.
Locking The Screen
Locking the screen does not switch users, but it allows another user to log in from the lock screen. On GNOME, for example, you can lock the screen (Super+L) and then select “Switch User” from the lock screen options.
This is a quick way to let someone else use the machine without logging you out.
Logging Out Completely
If you want to fully log out, use the system menu and select “Log Out.” This closes all your applications and returns to the login screen, where another user can log in.
Be careful: unsaved work will be lost.
How To Switch Users On Linux For Servers
On servers without a GUI, user switching is done entirely through the terminal. The commands we covered earlier (su, sudo, login) are your primary tools.
Best Practices For Server User Switching
- Always use
su - usernameto get a clean environment. - Avoid staying logged in as root for long periods.
- Use
sudofor single commands instead of switching users. - Monitor user activity with
whoorwcommands.
Server user switching is essential for maintenance tasks, software updates, and troubleshooting. Knowing the commands saves time and reduces errors.
Common Issues When Switching Users
Even experienced users run into problems. Here are some frequent issues and how to fix them.
Permission Denied Errors
If you get a “Permission denied” message, you likely do not have the right privileges. Use sudo or check with your system administrator.
Environment Variables Not Loading
When you switch users without the hyphen (su username), the new user’s environment may not load correctly. Always use su - username to avoid this.
Session Conflicts
If you have multiple sessions, some applications may conflict. For example, two users cannot both use the same audio device simultaneously. Close unnecessary apps to free up resources.
Forgotten Passwords
If you forget a user’s password, you can reset it as root using passwd username. This requires root access.
Security Considerations For User Switching
Switching users can introduce security risks if not done properly. Always log out of unused sessions, especially if you are using a shared computer. Use strong passwords and consider using sudo instead of sharing root access.
For servers, disable root login over SSH and use key-based authentication. This reduces the risk of unauthorized access.
How To Switch Users On Linux With A Script
If you need to switch users frequently, you can automate the process with a script. Here is a simple bash script that switches to a user named “john”:
#!/bin/bash
echo "Switching to user john..."
su - john
Save it as switch_to_john.sh, make it executable (chmod +x switch_to_john.sh), and run it. You can expand this to accept a username as an argument.
Scripting is especially useful for system administrators managing many accounts.
Frequently Asked Questions
What Is The Difference Between Su And Sudo?
su switches to another user and requires that user’s password. sudo runs a command as another user and requires your own password (if you have sudo privileges). sudo is generally more secure for single commands.
Can I Switch Users Without Closing My Applications?
Yes, using the GUI “Switch User” option or fast user switching keeps your applications running in the background. In the terminal, you can use tmux or screen to manage multiple sessions.
How Do I Switch To Root User?
Use su - (with the hyphen) and enter the root password. Alternatively, use sudo -i if you have sudo privileges. Be careful: root has full system access.
Why Does Su – Username Ask For A Password?
For security reasons, Linux requires the target user’s password to prevent unauthorized access. Only root can switch users without a password.
Is It Possible To Switch Users In A Script?
Yes, you can use su - username -c "command" to run a command as another user in a script. For interactive sessions, use su - username directly.
Conclusion
Now you know how to switch users on Linux using both the GUI and the terminal. Whether you prefer a quick dropdown menu or a powerful command-line tool, Linux gives you the flexibility to manage multiple user accounts efficiently. Practice these methods on your own system, and you will soon switch between users with confidence. Remember to always save your work and consider security best practices, especially on shared or server environments.