How To Switch Users In Linux Terminal 2024 Guide

Three Types of Users in Linux

Before we can begin switching users, let’s see the different types of users in Linux. There are three different types of Linux users:

System User or Non-login user: These types of users have neither login access nor shell access. They are commonly used to run certain services or applications. To learn more about non-login users, do check out our article on how to create a non-login user in Linux. Regular User: These types of user accounts have limited access to the file system according to their file permissions. However, they can use su or sudo commands to gain administrative rights. Root User (Super User/ Administrator): These types of user accounts have complete unrestricted access to the entire file system with the highest level of security clearance.

How to Switch User in Linux using su Command

The su command, which stands for switch user, is a powerful tool to switch from one user account to another. The syntax to switch users using the su command in the Linux Terminal is:
In the above syntax, replace with the name of the user account you want to switch over to. Some of the common options to pair with the su command are: OptionsDescriptions-cTo pass a command to the new shell of the switched user.-sTo specify a different shell instead of the default /bin/sh.-PWhen used, creates a new pseudo-terminal for the session which is not shared with the original session for better security-lStart the shell as a login shell with an environment similar to a real login-gUsed to specify the primary group.

1. Switch to a Regular User

To switch to regular users using the su command in Linux, use the following syntax: For example, to switch to the ‘test’ user, use the following command:
Now, type the password of the user you want to switch over to and hit enter. You may notice that there is no username in the shell prompt or you cannot even use the arrow keys to navigate or use syntax auto-completion using the tab key. This means that the shell is not a stable one. To spawn a stable shell, use this command:
Your shell prompt will now show the new username, also you can navigate with the arrow keys and use auto-completion with the tab key.

2. Switch to the Root User

Sometimes, you may need to use the root user account for some administrative tasks. To switch to the root user or the Super User in Linux, use the su command like:
You will now be prompted for the password for the root user. Type the root password and hit enter. Your shell prompt will now display the root username. You can now execute all commands as the root user.

How to Switch User in Linux using sudo Command

The sudo command stands for “Super User Do”. This command is generally used to execute various commands as a root or super user. But, with a small trick, you can use the sudo command to switch users in Linux. Before that, let’s see the syntax for the sudo command:
In the above syntax, the specifies which command you need to execute as a different user and the to pair along with the command. Some of the common options you can pair with the sudo command are: OptionsDescription-bto run the given command in the background-uto run the given command with a specific username passed as a parameter-nwhen used, returns a non-interactive shell and avoids prompting the user for input of any kind-Tto set a timeout for the command. If the timeout expires before the command has exited, the command will be terminated-irun the shell specified by the target user’s password database entry as a login shell

1. Change to Regular User

To switch to regular Linux users with the sudo command in Linux, use this syntax:
In the above syntax,

the -u flag is used to specify the username you want to switch to /bin/bash specifies to spawn a bash shell for this user

For example, if you want to switch to the user test1, use the following sudo command: Once you execute the above command, you will be prompted for the root user’s password. Type the root password and hit enter. The prompt will now show the switched username.

2. Switch to the Root User

To switch to the root user with the sudo command:
Next, type the root password and hit enter. Your shell prompt will now change to the root username.