Linux is a multi user operating system, meaning that it supports having multiple simultaneous user accounts. Some Linux systems may have a few different accounts, and others could have hundreds, depending on the purpose of the system. Switching between user accounts is a common task for Linux administrators who are in charge of user account management, as it allows them to test permissions or troubleshoot problems.
In this tutorial, you will learn how to switch user accounts on Linux. You will either need to have the necessary credentials to log in as the other user (username and password), or have administrative access to the system via the root user. We will go over both scenarios below.
In this tutorial you will learn:
- How to view a list of user accounts
- How to switch to a different user account
- How to switch to the root user account
- How to execute a command as a different user
- How to switch user via GUI
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
How to Switch Between User Accounts on Linux
Let’s start by listing the user accounts that are already created on our system. You do not need root permissions for this command, as any user is able to view all configured accounts.
$ cat /etc/passwd
The user names are listed in the first field of each line.
Many system services, such as SSH or MySQL, will create their own user accounts upon installation. This is to facilitate various system functions that are required for the services to run. These system accounts are ordinarily not meant to be accessible by people, and will point to an unusable shell by default.
We are able to log in to any of the users you see inside of the
/etc/passwd file, provided we know their login credentials, and while keeping the aforementioned caveat in mind.
Log in to a Different User (as a normal user)
Now that we know which user we want to log in as, we can execute the following command. We will attempt to log in to the
testuser user account in this example:
$ su testuser
We will then be prompted for the password of the
You can see in the screenshot above, that after providing the password for
testuser, we run the whoami command to verify that we have successfully logged in as the other user:
$ whoami testuser
Log in to a Different User (as root)
If you are already logged into the root user account, then logging into another user is really simple. You do not need to know the user’s password. As a matter of fact, the user does not even need to have a configured password at all. Since you are root, you can still log into the account regardless.
We will use the same syntax as before:
# su testuser $ whoami testuser
If, instead, you typically use the
sudo command to execute commands as root, then we can use this syntax:
$ sudo su testuser [sudo] password for linuxconfig: $ whoami testuser
Notice that this time we are prompted for our sudo password, rather than the password for the user account that we are trying to access.
Execute Command as Different User
What if we want to execute a command as a different user, without going through the whole process of having to switch to that user account, and then switch back when done? In that case, we can use this syntax, which will execute the
echo command with our
$ sudo -u testuser bash -c 'echo "I am $USER"' I am testuser
How to Switch User via GUI
From GUI, you can switch to a different user via your desktop environment’s lock screen. You will need to log out of your current user, and then log in to the other user by typing their account credentials on the lock screen:
In this tutorial, we saw how to switch user accounts on a Linux system. User accounts provide a separation of resources and files on a shared system, or can be configured to facilitate certain system services. Whatever the case, being able to switch between accounts swiftly is a useful skill that all Linux administrators should have.