switch between users in Linux

How to switch between users in Linux Terminal ?

To secure unauthorized access to the Linux Kernel, the Operating System provides option to create user accounts with different privileges. You can change the user associated with the current session in the Linux Terminal, provided if you know another user’s login name. Hence it is necessary to switch between users from one Linux Terminal to perform some operations. In this sneppet you will learn how to use “su” command to change users from Linux Terminal.

Switch between users in Linux using su command

The Linux command “su” stands for ‘Substituting User‘.  Developers/administrators wanted to execute Linux commands with another user account which is having more privileges to do certain operations, mainly ‘superuser‘.

switch between users in Linux

Syntax 1:

su <username>

Please note, the above method/syntax launches a new shell using the current user environment settings without using other username shell variables.

When this command is executed in Terminal by typing “su” and “username“, where “username” is the another user that you wanted to switch to. It asks for a password. Once authenticated, you will get access to that user account and the files and directories that user account has access permissions.

For example,

$ su user1
Password:
$exit
logout
$

Syntax 2:

su – <username>

In this method hypen “-” is used along with “su“. This method is used to launch a new login shell. It will setup the shell environment for the target user as if this user is a system user with proper login.

For example,

$ whoami
user1
$ su - user2
Password:
$ whoami
user2
$ exit
logout

The “whoami” command displays a username associated with the current session in the Linux Terminal.

Syntax 3:

su – 

In this method, you will see that username is not present in the command. Please note, in the absence of any username,  “su –” will invoke the root shell.

$ whoami
user1
$ su -
Password:
$ whoami
root
$ exit
logout

Switch between users in Linux using sudo command

The Linux command “sudo” stands for ‘Super User Do‘. This command is generally used to execute various Linux commands as a root or super user. But you can also use sudo command to switch between users in Linux Terminal.

Syntax:

sudo -u <username> /bin/bash

  • -u flag – to specify the username that you want to switch to
  • /bin/bash – a bash shell for this user

For example, if you want to switch to the user user2, use the following sudo command:

sudo -u user2 /bin/bash

Once you execute the above command, you will be asked to enter the root user’s password. Type the root password and hit enter. The prompt will now show the switched username.

$ sudo -u user2 /bin/bash
[sudo] password for sneppets:
user2@sneppets-laptop:/home/sneppets

To switch to root user, you need to run the following sudo command.

$ sudo -u root /bin/bash
root@sneppets-laptop:/home/sneppets#

When you run the above command, you will be prompted to enter root password. After entering correct password and hitting enter, you will see the prompt showing root username.

That’s all, you had learnt how to switch between users from Linux Terminal.

Hope this helped 🙂

References

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments