Guides 11792 Published by

The article explains the subtle but important distinction between invoking su with or without a dash, highlighting how the dash signals a login session that resets the environment variables. It outlines that running plain su preserves the current PATH and HOME, which can be handy for quick privileged actions but risky if legacy libraries are inadvertently used. A real‑world anecdote shows an administrator accidentally executing a script under their own path, causing a crash, whereas using  su – would have loaded root’s clean environment instead. Finally, the piece offers clear commands and warnings about common pitfalls, urging readers to experiment on a test VM to see how switching between these two forms can prevent accidental system damage.



Difference Between su and su – Commands in Linux

You’ll probably see su everywhere on the internet, but that dash after it is a game‑changer. In this quick read you’ll learn when to drop the hyphen and when to keep it, plus a little story about why it matters.

What’s the Story Behind su?

su stands for “switch user.” It was born in the days of single‑user systems where you just typed a password and became root. The command stayed even as multi‑user Linux grew; people still use it to jump into another shell as a different account.

su vs su – – Why the Dash Matters

The dash tells the shell to preserve your environment or to start a login session. Without it, you inherit the current user’s PATH, HOME, and other variables—great for quick debugging, but potentially dangerous if you’re working in a production folder with elevated permissions. With the dash, the target user gets a fresh login environment, just like logging in from scratch.

Real‑World Example: The “Root on a Laptop” Situation

I’ve seen this happen after a bad driver update: a system administrator dropped su instead of su – to run a quick script. Because they kept the laptop’s user PATH, the script pulled an outdated library from their home directory and crashed everything. A simple dash would have forced root to load its own /usr/bin first, preventing that fiasco.

Practical Steps for Each Command

1. su

Type: su username (or just su for root)

Why it matters: You stay in the current environment. Useful when you’re already in a session and want to run a few privileged commands without changing your PATH or HOME.

Example:

   su -c "apt update && apt upgrade"

The -c option tells su to run that command string as the target user.

2. su –

Type: su – username (or su –)

Why it matters: You get a clean login environment. Any custom variables or aliases you set for your normal account are gone; root’s defaults take over. This reduces side‑effects when installing system packages or editing config files.

Example:

   su – -c "vim /etc/hosts"

The double dash after the command (-c) is a quirk of some shells that require it to pass options through properly.

Common Pitfalls and How to Avoid Them
  • Assuming su always gives you root. It only does so if no username follows and you supply the correct password.
  • Using sudo su – when sudo -i would suffice. The latter starts an interactive login shell as root without changing your environment first, which is often what admins actually want.
  • Neglecting to exit the root shell. A stray exit or pressing Ctrl‑D will bring you back to your normal user, but it’s easy to forget and stay elevated for a long time.

Give this a shot on a spare VM—switch between su and su – in a couple of scripts—and notice how the environment changes. It might feel trivial, but those differences can save you from accidental system damage or confusing bugs down the line.