Skip to content

Different types of user switch in Linux

homepage-banner

Introduction

In Linux, there are different types of user switches that can be used to change the current user to another user or to gain administrative privileges. User switching is an essential feature in Linux, especially for system administrators, who need to perform various tasks with different user accounts. In this blog post, we will discuss the different types of user switches available in Linux.

su (Switch User)

The su command is used to switch to another user account in Linux. The su command is mostly used to switch to the root user account, which has administrative privileges. The syntax for using the su command is as follows:

su [username]

If you do not specify a username, the default user is the root user. You will be prompted to enter the password for the user you want to switch to. Once you enter the correct password, you will be logged in as that user.

sudo (Superuser Do)

The sudo command is used to execute a command with administrative privileges. The sudo command allows a user to execute a command with the privileges of the root user without actually logging in as the root user. The syntax for using the sudo command is as follows:

sudo [command]

You will be prompted to enter your password to authenticate yourself. Once you enter the correct password, the command will be executed with administrative privileges.

Switching types

1. Switching users with login shell

su - username

The login shell reads the configuration files in the following order:

  1. /etc/profile
  2. /etc/profile.d/*.sh
  3. ~/.bash_profile
  4. ~/.bashrc
  5. /etc/bashrc

2. Switching users with non-login shell

su username

The non-login shell reads the configuration files in the following order:

  1. ~/.bashrc
  2. /etc/bashrc
  3. /etc/profile.d/*.sh

Appendix: Bash Configuration Files

Where:

  • Profile configuration: sets environment variables, executes commands or scripts automatically after login
  • Bashrc configuration: sets local variables, sets aliases
Leave a message