Su Root
Let’s clearly compare the difference between:
🔸 su root
vs
🔸 su -
(which is shorthand for su - root
)
📌 su root
- Switches user: to
root
- Keeps your current shell environment
- Your
$PATH
,$HOME
,$USER
, etc., are mostly unchanged
- Your
- Does not simulate a full login
- You remain in the same working directory
🔍 Example:
$ whoami
user
$ su root
Password:
# whoami
root
# echo $HOME
/home/user
💡 You’re root
, but using your regular user’s environment.
📌 su -
(or su - root
)
- Switches user: to
root
- Starts a login shell for root
- Loads root’s
.bash_profile
,.profile
, etc. - Changes
$HOME
to/root
,$USER
toroot
, etc. - Updates
$PATH
to root’s path
- Loads root’s
- Changes directory to
/root
🔍 Example:
$ whoami
user
$ su -
Password:
# whoami
root
# echo $HOME
/root
💡 You’re root
, and using root’s proper environment.
✅ Summary Table
Feature | su root |
su - / su - root |
---|---|---|
Switches to root? | ✅ Yes | ✅ Yes |
Loads root’s environment? | ❌ No | ✅ Yes |
Changes $HOME to /root ? |
❌ No | ✅ Yes |
Changes $PATH ? |
❌ No (may miss /sbin ) |
✅ Yes (includes /sbin , etc.) |
Starts login shell? | ❌ No | ✅ Yes |
🧠 When to Use Which?
- Use
su root
: when you just need root temporarily without changing your environment. - Use
su -
: when running root tasks that depend on environment variables, root’s.bashrc
, or access to/sbin
, etc.
🧪 su -
vs sudo -i
Command | Meaning | Key Differences |
---|---|---|
su - |
Switch to root login shell | Requires root password |
sudo -i |
Run an interactive login shell as root | Uses your password (with sudo rights) |
🔍 1. su -
(Substitute User)
-
Starts a new login shell as root (like logging in directly).
-
Loads root’s environment,
.bashrc
,.profile
, etc. -
You must know root’s password.
-
Syntax:
su - # become root su - otheruser # become another user
🔍 2. sudo -i
(Sudo Interactive Shell)
-
Simulates a login shell as root using your user’s sudo privileges.
-
Loads root’s environment like
su -
. -
Uses your password, not root’s.
-
Requires you to be in
sudoers
.
✅ Summary
Feature | su - |
sudo -i |
---|---|---|
Requires root password | ✅ Yes | ❌ No |
Uses sudo | ❌ No | ✅ Yes |
Loads root env | ✅ Yes | ✅ Yes |
Safer audit/logging | ❌ Minimal | ✅ Logged |
Recommended for sudo-based systems (Ubuntu, etc.) | ❌ Not really | ✅ Yes |
🛡️ Which Should You Use?
-
On Ubuntu and other sudo-based systems: prefer
**sudo -i**
-
On older systems or when root password is known:
**su -**
is fine -
For audit and logging:
sudo -i
is better (it logs who switched to root)