Mastering the su Command in Linux: A Complete Guide for Linux Users

The su command in Linux is a powerful utility that allows users to switch between different user accounts, typically to gain root privileges. Whether you’re a system administrator managing a server or a developer troubleshooting permission issues, mastering the su command in Linux can streamline your workflow and enhance security.

In this guide, we’ll explore the su command in Linux in detail, covering syntax, real-world examples, security best practices, and troubleshooting tips.

📌 For a step-by-step visual guide, watch this video:


What is the su Command in Linux?

The su command in Linux (short for Substitute User) allows you to switch from the current user to another user on a Linux system. By default, it switches to the root user, providing elevated administrative privileges.

Why Use the su Command in Linux?

✅ Gain root access for system-wide changes
✅ Switch to another user account for troubleshooting
✅ Perform administrative tasks without logging out
✅ Run commands with another user’s environment settings


Basic Syntax of the su Command in Linux

To understand how the su command in Linux works, let’s look at its basic syntax:

su [OPTION] [USER]
  • If no user is specified, the command defaults to switching to the root user.
  • Various options modify how the command behaves.

To learn more, check the manual page:

man su

Switching to the Root User with su Command in Linux

The most common use of the su command in Linux is switching to the root user:

su

After entering the root password, you gain administrative privileges. This is useful for:

✅ Installing software (dnf install package_name)
✅ Editing system configuration files (nano /etc/hosts)
✅ Managing services (systemctl restart service_name)

Example: Installing Vim on Rocky Linux:

su
dnf install vim

Switching to Another User Using su Command in Linux

You can also switch to a different user by specifying their username:

su john

After entering John’s password, you’ll be working in his environment. This is useful for:

✅ Troubleshooting user-specific issues
✅ Running commands as another user
✅ Checking file permissions and configurations


Common su Command Options in Linux

🔹 su - (Login Shell)

When you use the su - option, you get a fresh login shell, loading the target user’s full environment variables.

su -

📌 Why use it?
Ensures you’re using the correct environment for system tasks, such as managing services with systemctl.

🔹 su -c "command" (Run a Command as Another User)

This allows you to execute a single command as another user without switching to their shell:

su - john -c "mkdir /home/john/new_folder"
su - john -c "whoami"

📌 Why use it?
Useful for running specific commands without a full user switch.

🔹 su -s /bin/zsh (Use a Specific Shell)

If you need to use a different shell, such as zsh, run:

su - john -s /bin/zsh

📌 Why use it?
Ensures you’re using a preferred shell for the target user.

To check available shells on Rocky Linux:

cat /etc/shells

Security Considerations for Using su Command in Linux

While the su command in Linux is powerful, it should be used with caution:

⚠️ Avoid unnecessary root access – Run commands as a normal user whenever possible.
⚠️ Use sudo for specific tasks – Instead of switching to root, consider using sudo command.
⚠️ Limit access to su – Restrict access using the /etc/pam.d/su configuration.


Real-Life Example: Fixing User Permission Issues with su Command in Linux

Imagine you’re logged in as john and can’t modify a file due to incorrect ownership:

ls -l /home/john/project_file

Output:

-rw-r--r-- 1 root root 1234 Jan 6 12:34 /home/john/project_file

Since the file is owned by root, John cannot edit it. To fix this:

1️⃣ Switch to Root

su

2️⃣ Change Ownership

chown john:john /home/john/project_file

3️⃣ Verify Changes

ls -l /home/john/project_file

New output:

-rw-r--r-- 1 john john 1234 Jan 6 12:34 /home/john/project_file

Now John has the correct ownership.


Frequently Asked Questions (FAQs) About su Command in Linux

1️⃣ How is su different from sudo?

  • su switches to another user (usually root), requiring their password.
  • sudo runs specific commands as root without a full user switch.

2️⃣ How can I disable su for non-admin users?

Modify the /etc/pam.d/su file and restrict access using wheel group permissions.

3️⃣ What should I do if I forget the root password?

Boot into single-user mode and reset it using:

passwd root

4️⃣ Is su available on all Linux distributions?

Yes, but some distros (like Ubuntu) disable the root account by default, favoring sudo.

5️⃣ How can I log out from an su session?

Simply type:

exit

or press Ctrl + D.


Conclusion

The su command in Linux is an essential tool for system administrators and developers working with Rocky Linux. Whether switching users, running privileged commands, or troubleshooting, understanding the su command in Linux enhances your control over the system.

✅ Use su responsibly to maintain system security.
✅ Prefer sudo for one-time administrative tasks.
Watch the video tutorial for a hands-on demonstration.

🔗 Related Reading:


See also:

List of monitoring tools 

Linux Blogs

AWS Cloud Blogs

Database Blogs

DevOps Blogs

Interview Questions & Answers

Docker Blogs

Google Cloud Blogs