Linux Tutorial

Linux File/Directory Management

Linux Packaging And Compression

Vim Text Editor

Linux Text Processing

Linux Software Installation

Linux User/User Group Management

Linux Permission Management

Linux Filesystem Management

Linux Advanced Filesystem Management

Linux System Management

Linux Backup and Recovery

Linux System Service Management

Linux System Log Management

Linux Boot Management

LAMP/LNMP Environment

SELinux Management

Linux su Command: Switch Between Users

In this tutorial, we will explain the usage of the su command in Linux. The su command (short for "substitute user" or "switch user") allows you to run commands or switch to a different user account without logging out and logging in again.

Basic Usage of the su Command

To switch to a different user account, use the following syntax:

su [OPTIONS] [USERNAME]

Replace [USERNAME] with the target user account's username.

For example, to switch to the root user account:

su root

When you run the su command, you'll be prompted for the password of the target user account. Enter the password, and you'll be logged in as that user.

Running a Command as a Different User

To run a command as a different user, use the following syntax:

su [OPTIONS] -c 'COMMAND' [USERNAME]

Replace [USERNAME] with the target user account's username, and replace COMMAND with the command you want to run.

For example, to run the whoami command as the root user:

su -c 'whoami' root

You'll be prompted for the target user account's password. Enter the password, and the command will be executed as that user.

Switching to the Root User Account with a Login Shell

When using su to switch to the root user account, you can use the - or -l option to start a login shell. This will load the target user's environment variables and set their home directory as the working directory.

su - root

or

su -l root

Preserving the Current Environment

If you want to switch to a different user account but keep your current environment variables, you can use the -m or -p option.

su -m [USERNAME]

or

su -p [USERNAME]

Summary

The su command is a versatile tool that allows you to switch user accounts or run commands as another user. It is especially useful when you need to perform administrative tasks as the root user or execute a command with the privileges of another user. Remember to use the su command with caution, as it can potentially grant you access to sensitive files and system settings. Always switch back to your normal user account when you're done with tasks that require elevated privileges.

  1. How to switch users in Linux using su:

    • Description: The su (switch user) command in Linux allows you to change to another user account.
    • Code:
      # Example: Switching to a different user
      su username
      
  2. su command examples for user switching:

    • Description: The su command has various options and can be used to switch to different users, including the root user.
    • Code:
      # Example: Switching to a different user
      su username
      
      # Example: Switching to root
      su
      
  3. Switching to root user with su in Linux:

    • Description: By default, using su without specifying a username switches to the root user.
    • Code:
      # Example: Switching to root
      su
      
  4. Using su for temporary user access in Linux:

    • Description: su can be used to gain temporary access to another user's account without the need to log out and log back in.
    • Code:
      # Example: Temporary access to another user
      su - username
      
  5. Security considerations with the su command:

    • Description: Security considerations with su include avoiding using it as root unless necessary, using the - option for a clean environment, and logging out after use.
    • Code:
      # Example: Using su with security considerations
      su - username
      
  6. Switching to a specific user with su:

    • Description: su can be used to switch to a specific user by specifying the username.
    • Code:
      # Example: Switching to a specific user
      su username
      
  7. Troubleshooting su command issues in Linux:

    • Description: Troubleshooting su issues involves checking user permissions, verifying correct syntax, and inspecting system logs for any error messages.
    • Code:
      # Example: Checking logs for su issues
      journalctl -xe | grep su