Linux can handle many users on the same system but when you only have a black screen to do everything, it’s not necessarily clear how you can switch from one user to another. In this article, I will mention 3 ways to do this.
The traditional way to log in as another user on Linux is to quit the current session with “exit” and log in again as the other user. But commands like “su” or “sudo” can also be used to switch or run a command as another system user.
Don’t worry, I’ll explain everything in detail in this article. I’ll start with the traditional method (sign off and log in as another user), but then introduce the two commands so you’ll have the answer to whatever your situation is.
1. Switch to another tab in your Linux terminal
If you know the user and password of the other account and need to use it for a while, the best option is simply to log in with the other user’s credentials.
You can either exit your current session and open a new one as the other user or, less often known, open another session without closing the current one (in a new tab).
Exit the current session and log in with another account
I guess if you’re reading this you don’t have a desktop environment and just have access to a server with one black screen where you’re logged in.
To close the current session, you can use this command:
A new prompt will appear, asking you for a login and password. You can then type the username and password of the other user and access their files.
Note: If you have a desktop environment it won’t work in the terminal app. You need to log out via the graphic interface (generally there is a shortcut in the top-right corner or in the main menu).
Keep the current session open and switch to another user
The thing that is less known on Linux is that it’s possible to have several sessions opened at the same time. And it works on all distributions and versions (desktop environment or not).
By default, the system will start one main display with a login prompt when you start the computer. I guess you are now logged in on this display.
But you can then switch to another display by using this shortcut:
CTRL + ALT + FxWhere Fx is one function key, generally between F1 and F6.
If a graphic interface is installed, the GUI is generally started on F7, and F1 to F6 are simple consoles (black screens). So, for example:
- CTRL + ALT + F2: Open a console terminal with a login prompt, where you can log in as another user.
- CTRL + ALT + F1: Get back to your previous session (if no GUI).
- CTRL + ALT + F7: Get back to the GUI (if you have one).
It might be slightly different depending on distributions, so check the documentation for more details, but it works like that for the most common ones.
Also, you can generally quickly switch from one console to another with “ALT+Right Arrow” or “ALT+Left Arrow”.
If you’re new to the Linux command line, this article will give you the most important Linux commands to know, plus a free downloadable cheat sheet to keep handy.
2. Use the “su” command to switch to another user
When you’re logged in via SSH, or want to stay in the same terminal in your graphic interface, the previous solution doesn’t work. Fortunately, there are commands for everything on Linux and I have two that are really useful for you.
The first command you can use to switch to another user on Linux is “su”.
The “su” command stands for “substitute user” (or “switch user”, which might be easier to remember). It’s used to change the current user in your terminal session. It’s typically used to switch to the administrator account (“root”), but you can also use it to switch to any other user.
Here is the main syntax:
You’ll then be prompted to type the USER password (not yours).
So, you need to know the user password to use this command. If you don’t, check the next option, which works even if you don’t know the other user’s password (but have administrator privileges).
Note: If no user is passed in the parameter, the default behavior is to switch to the “root” user.
3. Use the “sudo” command to switch to another user
When your current user has administrator privileges, another option is to use “sudo”. Sudo stands for “superuser do” and will run the command in the parameter as root.
You probably already know that you can run a command as root, for example:
sudo apt upgradewill install system updates using administrator privileges without really switching to the administrator’s session.
But you can also use sudo to either switch to another user, or run only one command as if it was run by this user.
To switch to another user, the syntax is:
sudo -i -u <USER>
You can also run a command as another user without switching, like this:
sudo -i -u <USER> <CMD>
The “whoami” command shows the current user. As you can see, it returns “raspberry” even if the command is run by the user “pat”.
Also, notice that no password is asked for. As I have administrator privileges (permission to use “sudo”), the system considers I don’t need the password.
Overall, “sudo” is the easiest method to switch to another user or run a command as another user when you have the right privileges. You can read this article to know how to get access to sudo with your current user (if it’s not already the case).
That’s it, you should now have at least one method that works to switch to another user on your Linux system. And if you liked this tutorial, you should also check out these on the website:
- 50 Most Useful Linux Commands You Need To Know (Cheat Sheet)
- 17 Security Tips From A Pro To Protect Your Linux Server
- How To Easily Reset a Forgotten Password On Linux (2 ways)
Want to chat with other Raspberry Pi enthusiasts? Join the community, share your current projects and ask for help directly in the forums.
Overwhelmed with Linux commands?
My e-book, “Master Linux Commands”, is your essential guide to mastering the terminal. Get practical tips, real-world examples, and a bonus cheat sheet to keep by your side.
Grab your copy now.
If you just want to hang out with me and other Linux fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.
Need help building something with Python?
Python is a great language to get started with programming on any Linux computer.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now.