DEV Community

Cover image for Switching Users with su and sudo (And When to Use Them)
Sana Muhammad Sadiq
Sana Muhammad Sadiq

Posted on

Switching Users with su and sudo (And When to Use Them)

Introduction

As I continue my 30-day Linux challenge in preparation for the RHCSA exam, today's focus is on something every Linux user needs to get comfortable with early on switching users securely and smartly using su and sudo.

Both commands give access to elevated privileges but they’re meant for different situations. And knowing the right way to use them can protect your system, your work and your team.

Index

  1. What is the Difference
  2. Basic Usage with Examples
  3. Key Differences
  4. Real World Scenario
  5. Industry Tip
  6. Quick Summary

🔍 What is the Difference?

  • su stands for substitute user. By default, it switches to the root user unless you specify another username.
  • sudo stands for superuser do. It allows temporary elevation of privileges to run a single command as another user (commonly root).

💡 Basic Usage with Examples

Command Purpose
su Switch to root (requires root password)
su - username Switch to another user
sudo command Run one command as root (requires current user's password)
sudo -u username command Run a command as a specific user

🧪 Examples:

su
su - sana
sudo dnf check-update
sudo -u postgres psql
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

🔐 Key Differences

su sudo
Requires root password Uses current user's password
Starts a new shell Executes a single command
Can be risky if left open Safer, logs activity
Doesn’t require configuration Needs user in sudoers file

🌱 Real World Scenario

Imagine you're managing a server and you need to edit a protected config file. If you use su and forget to exit, your shell stays elevated — risky. Using sudo for just that one command reduces the risk of accidental changes.

sudo nano /etc/ssh/sshd_config
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

This way, you're doing exactly what’s needed — nothing more, nothing less.

💼 Industry Tip

✅ Many sysadmins disable root login and rely entirely on sudo for tighter security and clearer audit logs.
✅ Always use sudo unless you must become another user with a full shell.

💬 Pro Tip: You can limit sudo access by role or command in /etc/sudoers, offering precision control in enterprise environments.

If you’ve ever been unsure when to use su vs sudo, I hope this cleared it up!

✅ Quick Summary

  • Use su to fully switch users (like debugging as another user).
  • Use sudo for privilege elevation without changing users.
  • Prioritize security and visibility.

Image description

I'd love to hear your thoughts, insights or experiences with Linux. Feel free to share and join the conversation [ Connect with me on LinkedIn www.linkedin.com/in/techwithsana ]💜

#30dayslinuxchallenge #redhat #networking #cloudcomputing #cloudengineer #cloudarchitect #cloud #RHCSA #RHCE #RHEL #WomeninTech #Technology

Top comments (0)