How to Switch Users in Linux

Published on September 10, 2024, Updated on December 17, 2024

In the world of Linux, multitasking and user management are key components that provide flexibility and security. As a user-friendly operating system, Linux offers a variety of commands to manage users effectively. 

One such command is "su," which stands for "switch user." In this article, we will delve into the intricacies of the su command, its options, compare it to the widely used sudo command, and understand how to switch users seamlessly in a Linux environment.

UNDERSTANDING THE SU COMMAND

The "su" command is a fundamental tool in Linux that allows users to switch to another user account. It's particularly useful for system administrators who need to perform tasks that require higher privileges without logging out of their current session. This feature enables a smooth transition between users, ensuring tasks are carried out efficiently while maintaining security.

In addition, it can be useful if you need to troubleshoot issues or run a command as another user.

To switch users, type “su” followed by the username. For instance, if you want to switch to a user named “rnm,” you’ll type:

su rnm

Afterwards, you’ll be prompted to add the password. Once you enter the user’s password, you can log in to the account. Alternatively, you can use the login shell or the “-l” option if you want to switch temporarily. Run this:

su -l rnm

SU COMMAND SYNTAX

Here’s how to use the su command. Type in:

su [options] [username [arguments]]

su: This is the command, which stands for "switch user."

Options: These are optional flags or parameters that modify the behavior of the command. Username: This is the username of the user account you want to switch to. If no username is provided, the command will assume you want to switch to the root user.

SU COMMAND OPTIONS

The "su" command offers various options to cater to different user-switching scenarios. Here are the options you need to know:

  • -command or -c: This option lets you run a specific command as another user without switching entirely to that user. 
  • Username: This is the actual account username you’d like to switch to. For instance: su john
  • -l [username] or - login: This command runs a login script to change the username. You have to enter the password. 
  • -help or -h: It displays the help and guidance documentation for the su command.
  • -shell or -s: This command allows users to execute commands in a different shell environment.
  • -preserve-environment or -p: This command allows you to protect the particular shell environment. 

HOW TO SWITCH USERS IN LINUX

SWITCHING TO A DIFFERENT USER

If you want to switch the Linux user, key in this command:

su –l [other_user]

The system subsequently prompts for a password. Upon successful authentication, you can login.

RUNNING A SPECIFIC COMMAND AS A DIFFERENT USER

As a different user, you’ll need to run this command:

su –c [command] [other_user]

Similarly, you’ll need to provide a password. Once the command gets executed, your Linux system will run the list of directory contents.

SWITCHING A SHELL ENVIRONMENT

You can also make a switch to a different shell environment by entering this command: 

su –s /usr/bin/zsh

USING A DIFFERENT USER IN THE SAME ENVIRONMENT

In instances when you want to stay in the current user’s environment, use the -p option. Enter this command:

su –p [other_user]

Replace the [other_user] part with the actual username. It's worth noting that while the user account changes, the home directory remains unaltered. This is important if you want to view the current user’s data.

In addition, you can verify that you remained in the same home environment using the echo $HOME command.

SU VS. SUDO: HOW DO THEY COMPARE?

While the “su” command allows you to switch users, the “sudo” command facilitates user management; you can execute a one-time administrative command, and then the account return to the regular user permissions. “su” has more features, and it can duplicate sudo’s functionality using the -c option to pass commands. 

Here’s how they compare:

USER IMPERSONATION

The "su" command allows you to fully switch to another user's account, inheriting their environment and permissions. On the other hand, “sudo” lets you execute specific commands with elevated privileges while remaining within your own user account.

PASSWORD REQUIREMENTS

When using "su," you'll need to enter the target user's password to switch to their account. Sudo requires your own password, but it authenticates you for a limited time, allowing you to execute multiple commands without repeatedly entering the password.

LOGGING AND AUDITING

When you switch users with "su," there might be less accountability, as it doesn't log the specific commands executed. On the other hand, “sudo” logs all executed commands, providing a comprehensive audit trail for system administrators.

CONCLUSION

The "su" command has diverse options that empower administrators to seamlessly transition between user accounts, ensuring tasks are executed effectively. With this powerful tool, you can switch users on Linux and navigate user environments.