How to Switch Users in Linux

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. 

Items from an article
  • KVM NVMe VPS 8 GB
    • Country: Singapore
    • CPU 4 x Epyc Core
    • RAM 8 GB
    • HDD NVMe : 80GB
    • BANDWIDTH 1 Gbps port
    • IPv4 / IPv6
    € 34/month
    34/month
  • KVM NVMe VPS 16 GB
    • Country: Singapore
    • CPU 8 x Epyc Core
    • RAM 16 GB
    • HDD NVMe : 150GB
    • BANDWIDTH 1 Gbps port
    • IPv4 / IPv6
    € 96/month
    96/month
  • KVM NVMe VPS 2 GB
    • Country: Australia
    • City: Sydney
    • CPU 2 x Epyc Core
    • RAM 2 GB
    • HDD NVMe : 30GB
    • BANDWIDTH 1 Gbps port
    • IPv4 / IPv6
    € 10/month
    10/month
  • KVM NVMe VPS 4 GB
    • Country: Australia
    • City: Sydney
    • CPU 2 x Epyc Core
    • RAM 4 GB
    • HDD NVMe : 50GB
    • BANDWIDTH 1 Gbps port
    • IPv4 / IPv6
    € 20/month
    20/month
Blog company: