Since Linux is a multi-user operating system, sooner or later you'll feel the need to switch between users. Whether it is for performing administrative actions using the superuser account, or simply changing the current user to access a specific directory, Linux provides several ways to tackle such issues.
Let's take a look at how you can change between multiple users on Linux.
Changing the Current User Using su
According to the su man page, the su command is used to either become another user during a login session or switch to the superuser. The basic syntax for the command is:
su options username
…where options are the various flags that you can use with the command and username is the name of the target account.
If you don't mention the username in the command, then su will switch to the root user by default.
su
Simply passing the username as an argument in the command will switch the current login session to the specified user.
su username
You might be asked to enter a password in case the user has one for their account.
For example, to switch to a user named testuser:
su testuser
Issue any of the following commands to verify the change:
whoami
echo $ USERNAME
The output will display the name of the user you just switched to. In this case:
testuser
If you have to run only a single command as another user, you don't need to switch to that user. Instead, you can simply execute the command as another user using the -c flag.
su -c command username
For example:
su -c chmod +w /Downloads testuser
Adding a hyphen (–) to the su command will create a new environment while switching between users.
su - testuser
Use the -s flag with the command to change the shell while switching to another user. Note that you'll have to invoke the command with a shell path as follows:
su testuser -s /bin/zsh
Using sudo to Switch Between Users
Similar to the su command, you can also change the current user using sudo. The syntax for both commands is more or less the same, except for the options.
To change the current login session to another user, use the -u flag:
sudo -u username
If you want to issue a particular command as another user, specify it in the command:
sudo -u username command
For example:
sudo -u testuser chmod 777 /Documents
You can also change the shell while switching between users:
sudo -u username path-to-shell
For example, to change the current shell to zsh and switch to testuser simultaneously:
sudo -u testuser /bin/zsh
Again, verify if the changes were successfully made using the whoami command or the USERNAME environment variable.
Changing Users Using the Desktop Environment
If using the command line is not your cup of tea, you can opt for the graphical approach instead. Most Linux PCs have a desktop environment installed that provides the graphical interface you need to interact with the OS. GNOME and KDE are two of the most widely-used desktop environments, so this guide will only focus on these.
To switch between users on the GNOME desktop environment:
- Click on the downwards arrow icon located at the top-right corner of the screen.
- Click on Power Off/Log Out and select the Switch User option from the dropdown menu.
- GNOME will display a list of available users. Click on the username you want to log in as.
For those who are using KDE, follow the steps given below to change the current user:
- Open the Application Launcher and click on the Leave option located on the bottom right of the window.
- In the dropdown that appears, select Switch User.
- You'll be taken back to the login screen. Highlight the user you want to switch to using the right and left cursor keys.
- Type in the account password and hit Enter to log in.
Note that other desktop environments also offer similar options to log in as another user.
Managing Multiple User Accounts on Linux
Like other operating systems, you can switch between multiple users on Linux as well. Although other OSes like Windows and macOS provide a GUI to carry out such actions, on Linux, you can use the command line to log in to other user accounts efficiently.
As obvious, having advanced knowledge of the Linux command line is a powerful addition to any IT professional's arsenal. Getting practical with the commands and keeping a dedicated learning resource at your disposal is the best way to familiarize yourself with the Linux terminal.