To secure the Linux kernel from unauthorized access, the operating system provides the option of creating different users with different privileges. You can easily switch users in Linux using commands. In this article, we will explore some different methods on how to switch users right from your Linux terminal, regardless of if you want to perform administrative tasks or simply need to execute commands as a super user.
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:
su <options> <username>
In the above syntax, replace <username> 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:
Options Descriptions -c To pass a command to the new shell of the switched user. -s To specify a different shell instead of the default /bin/sh. -P When used, creates a new pseudo-terminal for the session which is not shared with the original session for better security -l Start the shell as a login shell with an environment similar to a real login -g Used 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:
su <options> <username>
For example, to switch to the ‘test’ user, use the following command:
su -l test1
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:
/bin/bash/ -i
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:
su -
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:
sudo <options> <parameters> <command>
In the above syntax, the <command> specifies which command you need to execute as a different user and the <parameters> to pair along with the command. Some of the common options you can pair with the sudo command are:
Options Description -b to run the given command in the background -u to run the given command with a specific username passed as a parameter -n when used, returns a non-interactive shell and avoids prompting the user for input of any kind -T to set a timeout for the command. If the timeout expires before the command has exited, the command will be terminated -i run 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:
sudo -u <username_to_switch_to> /bin/bash
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:
sudo -u test1 /bin/bash
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:
sudo -u root /bin/bash
Next, type the root password and hit enter. Your shell prompt will now change to the root username.
All user information is stored in the /etc/passwd file. Use the cat command to view the file contents as: cat /etc/passwd
No, you need to know the password of the user you want to switch over to. If you are logged in as the root user, then you can switch without knowing their password.