How to Switch Users in Linux/Ubuntu Terminal?


Linux/Ubuntu is a multi-user operating system, i.e., Linux enables one or more users with different user accounts to log in and perform tasks without affecting the other user account. A system administrator can create multiple user accounts and grant each user account a different set of privileges. These user accounts can easily be switched to perform administrative tasks.

This article will demonstrate the below-listed methods of switching users via Terminal in Linux/Ubuntu:

  • Method 1: Using SU Command
  • Method 2: Using Sudo Command
  • Method 3: Using SSH Command

Method 1: Using SU Command

SU (Switch User) is a Linux utility that enables us to switch users or execute commands with other users’ privileges. In the following sections, multiple scenarios to switch user accounts via Terminal are demonstrated:

How to Switch User Accounts via SU Command?

To switch between user accounts, run the following command:


The above image demonstrates switching user accounts from “linuxuser” to “linuxgenie”. The successful user account switch is then verified by the “whoami” command.

How to Forcefully Switch User Accounts via SU Command as Login User?

Use the su command with the -l flag to force a user to specify a password before switching accounts:


Where the “-l” flag simulates a full login. The system will require a user password to switch user accounts:

The above image demonstrates switching user accounts from “linuxuser” to “linuxgenie”. The successful user account switch is then verified by the “whoami” command.

How to Execute a Command as a Different User via SU Command?

To switch user accounts to execute a single command, run the following command:


Where

  • c: The flag enables the execution of a single command to Ubuntu’s shell.
  • whoami: the command that the user would like to run.
  • linuxgenie: User account where the user would like to run a command.

 

The above image demonstrates switching user accounts from “linuxuser” to “linuxgenie” to execute the command “whoami. After the “whoami” command is executed, the user account is switched back to “linuxuser”.

How to Log In to a User Account in a Different Shell via SU Command?

To login to a user in a different shell environment, execute the following command:

$ su -l linuxgenie -s /bin/bash

Where

  • l: Login flag
  • linuxgenie: User account where the user would like to login.
  • s: it enables a user to login to different shell environments for example, /bin/bash, /usr/bin/zsh, etc.
  • /bin/bash: shell.

The above image demonstrates the successful switching of user accounts from “linuxuser” to “linuxgenie” in /bin/bash shell. Additionally, the “echo $SHELL” command is executed to verify the shell.

Method 2: Using Sudo Command

The Sudo (Superuser do) command enables a user to run a command as a superuser. In the following sections, multiple scenarios to switch user accounts via Terminal are demonstrated:

How to Execute a Command as a Different User via Sudo Command?

To switch a user account by executing a single command, run the following script:

$ sudo -u linuxgenie whoami

Where

  • u: User flag
  • linuxgenie: User Account where the user would like to run a command.
  • whoami: the command that the user would like to run.

The above command switches the user account from “linuxuser” to “linuxgenie”. After the “whoami” command is executed, the user account is switched back to “linuxuser”.

How to Login to a User Account in a Different Shell via Sudo Command?

To login to a user in a different shell environment, execute the following command:

$ sudo -u linuxgenie /bin/sh

Where

  • u: User flag
  • linuxgenie: User Account where the user would like to login.
  • /bin/sh: shell.

The output demonstrates the successful switching of user accounts from “linuxuser” to “linuxgenie” in /bin/bash shell which is verified by the “whoami” command.

How to Switch to Root User via Sudo Command?

To switch to the root user, execute the below script:


Where

  • u: User flag.
  • /bin/bash: shell

The output demonstrates the successful switching of user accounts from “linuxuser” to “root” in /bin/bash shell which is verified by the “whoami” command. Additionally, the user can use the “exit” command to switch back to linuxuser.

Method 3: Using SSH Command

SSH (Secure Shell) is a secure network protocol widely used to establish a secure connection between devices over the network. It can also be utilized to switch from one user account to another.

How to Switch User Accounts via SSH Command?

To switch to a specific user account via SSH, execute the following command:


Where:

  • anees: user account
  • 127.0.0.1: local host

From the output, it can be verified that the user account is successfully switched from “linuxuser” to “anees”.

Bonus Tip: How to Switch Users Using GUI?

In addition to switching users via Terminal, a user can use GUI to switch user accounts by the below steps:

  • Press the “Highlighted” icon in the upper right corner of the screen. It will open a drop-down list.
  • From the menu, press the “Power Off/LogOut” option. This will further open a drop-down list.
  • Next, select “Switch User”.

  • After that, the system will log out from the current session and will display a list of all user accounts.
  • Finally, a user can select the desired User Account.

Conclusion

Users can easily switch between user accounts to access different privileges or to run different commands as a different user by SU, Sudo, or SSH commands. This article demonstrated three different ways of switching users via Terminal in Linux/Ubuntu Terminal. Additionally, this article demonstrated switching users via GUI.

Print Friendly, PDF & Email