Linux

How to Use the su Command in Linux with Examples

Introduction

If you dive into the depths of Linux administration, you’ll quickly encounter scenarios where you must switch between users to execute commands or access files with different permissions. This is where the su command functions. In this guide, you’ll learn how to utilize the su command effectively, allowing you to switch users within your Linux environment seamlessly. It does not matter if you are an expert sysadmin or just beginning your Linux journey, mastering the su command is an effective skill.

How the su Command Works

How the su Command Works

The su command, an acronym for “substitute user” or “switch user,” enables you to assume the identity of another user on your system. This can be particularly handy when you need to do administrative activities that need raised privileges or simply want to work within a different user account.

When you execute the su command taken up by the username of the target user, you are asked to enter the password associated with that account. Once authenticated, you’re granted access to the target user’s environment, complete with their permissions and privileges. This seamless transition allows you to carry out tasks as if you were logged in directly as that user.

su Command Syntax

The syntax for using the su command is straightforward:

su [options] [username]
su Command Syntax

su Command Options

  • -: The dash option, when used without specifying a username, simulates a complete login experience, including the user’s environment variables and shell settings.
  • -c command: Employing this option allows you to execute a specific command as the target user. For instance, su -c “ls -l” executes the ls -l command within the context of the target user.
  • -s shell: This option permits you to designate a shell other than the default shell for the target user. It is specifically useful when you are required to implement commands within a specific shell environment.
  • -m: The -m option preserves the existing platform when shifting users, ensuring continuity of environment settings such as the current working directory and shell variables.
  • -l, –login: Similar to the dash option, the –login flag initiates a login shell for the target user, replicating the platform as if the user logged in directly.
  • -p, –preserve-environment: This option maintains the current environment, including the user’s home directory and environment variables, when switching users.

By leveraging these diverse options, you gain unparalleled flexibility in managing user sessions and executing commands within distinct user contexts. 

su Command Examples

Switch to a Different User

Let’s dive into practical examples of employing the su command to shift to a different user. Suppose you need to do administrative activities that need raised privileges. You can seamlessly switch to the root user by typing su followed by the root username. Here’s how:

su root
su Command Examples

You’ll then be asked to type in the root password. Once authenticated, you’ll find yourself operating within the root user’s environment, equipped with full administrative privileges. This allows you to execute commands and modify system configurations with authority.

Run Specific Command as a Different User

Sometimes, you may only need to execute a specific command as a different user without fully switching to their environment. The -c option of the su command enables you to acquire this seamlessly. For instance, if you wish to list the directory contents owned by another user, you can use the following command:

su -c "ls /path/to/directory" username
su Command Examples

Replace /path/to/directory with the directory path and username with the target username of the user. This command executes the ls command within the specified directory as the designated user, allowing you to view its contents.

Use a Different Shell

By default, when you switch users using the su command, you’re placed into the target user’s default shell environment. However, you can specify a different shell using the -s option. For instance, if you prefer to use the Bash shell instead of the default shell for the target user, you can do so with the following command:

su -s /bin/bash username
su Command Examples

This command switches to the particular user’s environment but utilizes the Bash shell instead of their default shell. You can replace /bin/bash with the path to any shell available on your system.

Employ a Different User in the Same Environment

There may be situations where you want to execute commands as a different user within your current environment, without fully switching users. The -m option of the su command, also known as the “preserve environment” option, allows you to achieve this. Here’s how:

su -m username
su Command Examples

This command preserves your current environment, incorporating the current working directory and shell variables, while switching to the specified user’s environment. You can then execute commands as the designated user within your existing shell session.

Command Comparison: su vs sudo

Command Comparison: su vs sudo

When it comes to user management and executing commands with elevated privileges in Linux, two commands often come into play: su and sudo. Apprehending the differences between these commands is crucial for efficient system administration. Let’s delve into a comparison of su and sudo to help you navigate their respective use cases effectively.

su Command

The su command, short for “substitute user” or “switch user,” allows you to assume the identity of another user on the system. When you use us without any options, you’re prompted to enter the password of the target user, typically the root user. Once authenticated, you gain access to the target user’s environment with full administrative privileges. However, every command you execute while switched to the target user is performed with their privileges, which can potentially pose security risks if not used carefully. Additionally, su requires the root password, which may not be accessible to all users.

sudo Command

On the other side, the sudo command, short for “superuser do,” provides a more granular approach to executing commands with elevated privileges. Instead of switching to another user’s environment entirely, sudo permits you to implement particular commands as the root user or another privileged user. Users granted sudo privileges can implement authorized commands by prefixing them with sudo. Unlike su, sudo typically requires the user’s password, enhancing security by limiting access to the root password.

Command Comparison

The primary distinction between su and sudo lies in their approach to privilege escalation. su switches the entire user environment, granting full access to the target user’s privileges, while sudo grants temporary elevated privileges for executing specific commands. This makes sudo more suitable for granting selective access to administrative tasks without compromising the security of the root account. Additionally, sudo provides a comprehensive audit trail of privileged commands executed by users, enhancing accountability and traceability.

Also Read: How to Setup Docker Environment Variables?

Conclusion

You’ve now mastered the su command, a pivotal tool for Linux change user and Linux switch user tasks. With its diverse options, including -c for running specific commands and -s for choosing a different shell, you can seamlessly navigate between user environments and execute commands with elevated privileges. Understanding the differences between su and sudo empowers you to make informed decisions when it comes to switching users in Linux and sudo to the user. Remember to use the su command responsibly, prioritizing security by safeguarding passwords and limiting access to privileged commands. By harnessing the power of the su command, you gain unparalleled flexibility and control over user management in your Linux environment, ensuring efficient administration and smooth operation of your system.

Arpit Saini

He is the Chief Technology Officer at Hostbillo Hosting Solution and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *