Lesson 11 of 29 ↓
- 1.Rhel 9 Administration Guide
- 2.Ubuntu Boot Process Guide
- 3.Linux Basic Commands Guide
- 4.Comprehensive Unix Shells Guide
- 5.Mastering Vim Deep & Practical Guide
- 6.Magic Numbers in Linux Deep Systems Guide
- 7.Wildcards & Globbing in Linux Deep Cli Guide
- 8.Linux Users & Groups Deep Administration Guide
- 9.Journaling in Linux Filesystems Deep Systems Guide
- 10.Getopt & Getoptlong Deep System-level Option Parsing Guide
- 11.Su Command in Linux Complete Guide with Environment Variables
- 12.Linux File Permissions and Ownership Complete Practical Guide
- 13.Ubuntu Package Management Apt Complete Guide
- 14.Bash Scripting Basics
- 15.Linux File System Essentials
- 16.Process and Thread Management
- 17.Session 10 : Introduction to Networking & the Application Layer
- 18.Linux Networking Cli & Introduction to Yocto for Machine Routing
- 19.Comprehensive Guide to Ssh (secure Shell)
- 20.Logical Volume Management (lvm) Guide
- 21.C Build Process & Libraries Guide
- 22.Archiving and Compression in Linux
- 23.Linux Package Managers: a Detailed Guide
- 24.Misra-c Guidelines Detailed Cheat Sheet
- 25.Mqtt Server Setup (pc as Server)
- 26.Detailed Explanation of Tty and Pts in Linux
- 27.Vim Cheat Sheet
- 28.Difference Between apt and apt-get
- 29.Difference Between apt Upgrade and apt Full-upgrade
2 min read
Su Command in Linux Complete Guide with Environment Variables
Introduction
The su (substitute user / switch user) command is one of the oldest and most fundamental privilege-switching tools in Unix and Linux systems. It allows one user to temporarily become another user — most commonly root — either to execute a single command or to start an interactive shell.
Understanding su requires understanding environment variables, login shells, and security implications, which this guide covers in depth.
Environment Variables in Linux
Environment variables are key–value pairs inherited by child processes from their parent shell. They define runtime behavior such as command lookup paths, user identity, language, and shell configuration.
Common Environment Variables
| Variable | Purpose | Example |
|---|---|---|
USER |
Current username | abdallah |
UID |
Numeric user ID | 1000 |
HOME |
User home directory | /home/abdallah |
PATH |
Executable search path | /usr/bin:/bin |
SHELL |
Default login shell | /bin/bash |
PWD |
Current directory | /home/abdallah/projects |
Viewing Environment Variables
printenv
env
# individual variables
echo $USER
echo $HOME
echo $PATHWhat is su?
The su command starts a shell as another user after successful authentication. By default, it switches to root:
suBut it can switch to any user:
su alice⚠ Authentication rule: you must know the target user’s password (unless running as root).
Su Command Syntax
su [OPTIONS] [USERNAME]Important Options
| Option | Meaning |
|---|---|
- or --login |
Start a full login shell |
-c "cmd" |
Run a single command |
-m / -p |
Preserve environment |
-s /bin/shell |
Use specific shell |
su vs su - (critical Difference)
su (no Dash)
-
Keeps current environment
-
Does NOT reset
HOME,PATH,USER -
Working directory remains the same
Example:
su ali
echo $HOME # still original user's homesu - (login Shell)
-
Resets environment
-
Loads
/etc/profile,~/.bash_profile -
Changes working directory to target home
Example:
su - ali
echo $HOME # /home/ali✅ Best practice: Always use su - when switching users.
Environment Variable Behavior Demo
Before Su
echo $USER
echo $HOME
echo $PATHUsing Su
su ali
echo $USER # still original user
exitUsing Su -
su - ali
echo $USER # ali
exitRunning Single Commands
su - root -c "apt update"Useful for scripts and auditing.
Su vs Sudo
| Feature | su | sudo |
|---|---|---|
| Needs root password | ✅ | ❌ |
| Command-level control | ❌ | ✅ |
| Logging | ❌ | ✅ |
| Safer | ❌ | ✅ |
✅ Modern systems prefer sudo.
Security Best Practices
-
Disable direct root login where possible
-
Prefer
sudo -ioversu - -
Limit shell access in
/etc/passwd -
Log usage (
/var/log/auth.log)
Summary
-
suswitches user -
su -emulates a real login -
Environment variables define behavior
-
Prefer
sudoin production systems
Comments
Loading the discussion…