Linux su Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn about the Linux su command, which allows you to switch to another user account, typically a user with elevated privileges, such as the root user. You will understand the basic usage of the su command, including switching to the root user and a specific user account. Additionally, you will explore how to manage privileges using the su command. The lab provides practical examples to help you become proficient in user and permission management on Linux systems.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux/UserandGroupManagementGroup -.-> linux/useradd("`User Adding`") linux/UserandGroupManagementGroup -.-> linux/userdel("`User Removing`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/UserandGroupManagementGroup -.-> linux/su("`User Switching`") subgraph Lab Skills linux/useradd -.-> lab-422936{{"`Linux su Command with Practical Examples`"}} linux/userdel -.-> lab-422936{{"`Linux su Command with Practical Examples`"}} linux/sudo -.-> lab-422936{{"`Linux su Command with Practical Examples`"}} linux/su -.-> lab-422936{{"`Linux su Command with Practical Examples`"}} end

Understand the su Command

In this step, you will learn about the su command in Linux, which stands for "switch user". The su command allows you to switch to another user account, typically a user with elevated privileges, such as the root user.

To understand the basic usage of the su command, let's start with a simple example:

sudo su

Example output:

root@labex:/home/labex/project## ```

In the example above, we used the `sudo` command to switch to the root user account. The `root` user has the highest level of privileges in the Linux system and can perform any operation.

The `su` command can also be used to switch to a specific user account, rather than the root user. For example, to switch to the `labex` user, you can run:

```bash
su - labex

Example output:

labex@labex:/home/labex/project$

Notice that the prompt has changed to indicate that we are now logged in as the labex user.

The - option in the su command is important, as it ensures that the new user's environment is loaded, including the user's shell, environment variables, and home directory.

Without the - option, the su command will only change the user, but the environment will remain the same as the previous user's environment.

Switch User with su Command

In this step, you will learn how to use the su command to switch to a different user account.

First, let's switch to the root user account:

sudo su

Example output:

root@labex:/home/labex/project## ```

Now, let's switch to the `labex` user account:

```bash
su - labex

Example output:

labex@labex:/home/labex/project$

Notice that the prompt has changed to indicate that we are now logged in as the labex user.

To switch back to the root user, you can simply run the su command again:

su -

Example output:

root@labex:/home/labex/project## ```

The `su -` command without a specific username will switch to the root user account.

You can also use the `su` command to switch to any other user account on the system, as long as you have the necessary permissions.

For example, to switch to the `ubuntu` user, you can run:

```bash
su - ubuntu

Example output:

ubuntu@labex:/home/labex/project$

Remember, when switching to another user account, you will have the permissions and environment of that user, which may be different from your current user account.

Manage Privileges with su Command

In this step, you will learn how to use the su command to manage privileges and execute commands with elevated permissions.

As you've learned in the previous steps, the su command allows you to switch to a different user account. When you switch to the root user account, you have the highest level of privileges in the system, which means you can perform any operation.

Let's try executing a command that requires elevated privileges, such as installing a package:

sudo su
apt-get update
apt-get install -y htop

Example output:

Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [114 kB]
...
Setting up htop (3.0.5-7ubuntu1) ...

In the example above, we first switched to the root user using sudo su, then we were able to execute the apt-get commands to update the package lists and install the htop package.

If you're not the root user, you can still execute commands with elevated privileges using the sudo command. For example:

sudo apt-get update
sudo apt-get install -y htop

The sudo command allows you to execute a command with the privileges of the root user, without actually switching to the root user account.

It's important to use the sudo command carefully, as it grants elevated privileges that can potentially be misused or cause unintended consequences if not used properly.

Summary

In this lab, you learned about the su command in Linux, which stands for "switch user". The su command allows you to switch to another user account, typically a user with elevated privileges, such as the root user. You learned how to use the su command to switch to the root user account and to a specific user account, such as the labex user. The - option in the su command is important, as it ensures that the new user's environment is loaded, including the user's shell, environment variables, and home directory. You also learned how to manage privileges with the su command, allowing you to perform operations with elevated permissions.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like