su Command in Linux

Introduction to the Linux su Command

This lab provides a comprehensive guide to the Linux su command. Discover how to effectively switch user accounts, particularly to gain root privileges or access specific user environments. Master the basics of su usage, including switching to the root user and other user accounts. Learn practical techniques for privilege management on Linux systems, enhancing your skills in system administration.

Understanding the "Switch User" (su) Command

This section introduces the su command in Linux, short for "switch user." This command is fundamental for systemadmin tasks, enabling you to assume the identity of another user, often the root user with elevated permissions.

Let's begin with a basic example to illustrate the su command's functionality:

sudo su

Example output:

root@labex:/home/labex/project#

The example showcases switching to the root user using sudo su. The root user possesses ultimate control over the Linux system and can execute any operation.

The su command isn't limited to the root user; it can switch to any specific user account. To switch to the labex user, use the following command:

su - labex

Example output:

labex@labex:/home/labex/project$

Notice the prompt change, indicating a successful switch to the labex user account.

The - option is crucial. It ensures the new user's environment is fully loaded, including their shell, environment variables, and home directory, providing a complete user experience.

Omitting the - option results in only the user being changed, while the environment remains that of the previous user, potentially leading to unexpected behavior.

How to Switch Users with the su Command

This section provides hands-on experience in switching between user accounts using the su command.

First, switch to the all-powerful root user account:

sudo su

Example output:

root@labex:/home/labex/project#

Next, switch to the labex user account:

su - labex

Example output:

labex@labex:/home/labex/project$

The altered prompt verifies that you are now operating as the labex user.

To revert to the root user, simply execute the su command again:

su -

Example output:

root@labex:/home/labex/project#

The su - command without a username automatically switches to the root user.

You can use the su command to switch to any user account on the system, assuming you possess the necessary authorization.

For example, to switch to the ubuntu user, use:

su - ubuntu

Example output:

ubuntu@labex:/home/labex/project$

Remember that when switching users, your permissions and environment will reflect those of the target user, potentially differing significantly from your original user account.

Mastering Privilege Management with the su Command

This segment focuses on utilizing the su command to manage privileges and execute commands requiring elevated permissions.

As previously demonstrated, the su command facilitates switching to different user accounts. When you become the root user, you gain unrestricted privileges, allowing you to perform any system operation.

Let's attempt a task demanding elevated privileges, such as installing a software 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) ...

The example shows switching to the root user with sudo su, enabling the execution of apt-get commands to refresh the package lists and install the htop utility.

Even without becoming the root user, you can execute commands with elevated privileges using the sudo command. For example:

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

The sudo command permits executing a command with the privileges of the root user without a full user switch.

Exercise caution when using the sudo command, as it grants elevated permissions that could be misused or lead to unintended consequences if handled improperly. It is a vital tool in Linux systemadmin.

Conclusion

This lab explored the su command in Linux, which serves as a "switch user" tool. It enables switching to another user account, often one with elevated privileges like the root user. We covered how to use su to switch to the root user and specific user accounts like labex. The - option is key, ensuring the new user's environment is fully loaded, including their shell, environment variables, and home directory. Furthermore, you learned to manage privileges with the su command, enabling actions with elevated permissions, essential for any systemadmin working with Linux.

400+ Linux Commands