Linux is a multi-user operating system. Whether it's using the superuser account to execute administrative tasks or modifying the current user's access to a certain directory, you'll have to move between users at some point.
Linux has several options for dealing with such difficulties. The obvious solution is to log out and log in as the desired user. But you have a couple of different options available through which you can switch users without logging out of the current user.
In this article, you will learn about all the different ways to switch between users in a Linux system like Ubuntu, Debian, or Rocky linux.
You will need terminal access and an account with sudo privileges on the Linux-based system.
Note: The commands in this tutorial are performed on the Linux Mint system. All the methods in the tutorial will provide the same results for any Linux-based system.
Change User Using su Command
You might have used the su command to become a superuser or root. You can also use this command to switch between users during another user’s session. To switch to another user, just add the specific username to the su command.
You can verify the change in the user by running the whoami command.
You might be prompted for a password in case the user is password protected or an administrator.
If you want to execute a small task and do not want to change between users, the su command with the -c option can help you run terminal commands as another user.
su -c <command> <username>
Alternatively, you can open the shell as another user using the -s option to execute a string of commands. To open the shell, use the following command with your shell path.
su <username> -s /bin/sh
Change User Using Sudo Command
Another way to switch between users is to use the sudo command. Run the sudo command with the -u option and your username to change users.
sudo -u <username>
You can run a single command as another user without switching users with the following command.
sudo -u <username> <command>
You can open a shell as another user and run the batch of commands without changing users. To open a shell as another user, run the following command with your shell path.
sudo -u <username> <shellpath>
Changing Users Using the Desktop Environment
The last and most commonly used method is to log out or switch users using your system GUI. This method is not exclusive to Linux systems and is used by all multiuser operating systems.
To change between users using a desktop environment, Go to the startup menu and click on the shutdown icon.
You will be prompted with the session dialog.
Choose either the switch user or the logout button. Both options will take you to the following login form.
Now, select the user you want to log in as and proceed with the login.
Although other multi-user operating systems, such as Windows and macOS, employ a graphical user interface (GUI) to switch between multiple users, you can also use the command line to efficiently log in to other user accounts on Linux.
This article covers all the ways to switch between users in Linux. Advanced knowledge of the Linux command line offers value to any Linux user as it provides us with easy and efficient solutions.