0
0
Linux CLIscripting~15 mins

su (switch user) in Linux CLI - Deep Dive

Choose your learning style9 modes available
Overview - su (switch user)
What is it?
The 'su' command in Linux lets you switch from your current user account to another user account without logging out. It is often used to gain administrative privileges by switching to the root user. You enter the password of the target user to switch. This allows you to run commands as that user temporarily.
Why it matters
Without 'su', you would have to log out and log back in to use another user account, which is slow and interrupts your work. 'su' lets you quickly switch users in the same session, making system management and multitasking easier. It also helps keep systems secure by controlling who can become root or other users.
Where it fits
Before learning 'su', you should understand basic Linux commands and user accounts. After mastering 'su', you can learn about 'sudo' for safer privilege escalation and user switching. This fits into the broader topic of Linux user management and system administration.
Mental Model
Core Idea
The 'su' command temporarily changes your user identity to another user, letting you act as them without logging out.
Think of it like...
It's like borrowing someone else's ID card to enter a restricted area without leaving your current room.
Current User
   │
   ▼
[ su target_user ]
   │
   ▼
Switched User Environment
   │
   ▼
Run commands as target_user
   │
   ▼
Exit to return to original user
Build-Up - 6 Steps
1
FoundationBasic user switching with su
🤔
Concept: Learn how to switch to another user using 'su' with a password.
Open a terminal and type 'su username'. You will be asked for the password of that user. After entering it correctly, your prompt changes, showing you are now that user. For example, 'su root' switches to the root user.
Result
You become the target user and can run commands as them until you type 'exit' to return.
Understanding that 'su' changes your user context helps you see how Linux controls permissions and access.
2
FoundationSwitching to root user basics
🤔
Concept: Using 'su' to become the root user for administrative tasks.
Type 'su' or 'su -' to switch to root. The dash '-' loads root's environment variables, making it like a fresh login. Enter the root password when prompted.
Result
You gain full system control with root privileges until you exit.
Knowing the difference between 'su' and 'su -' helps avoid environment-related errors during administration.
3
IntermediateUnderstanding environment changes with su -
🤔Before reading on: Do you think 'su' and 'su -' load the same environment? Commit to your answer.
Concept: 'su -' simulates a full login for the target user, loading their environment variables and settings.
'su' switches user but keeps your current environment, which can cause permission or path issues. 'su -' resets environment variables like PATH, HOME, and shell settings to the target user's defaults.
Result
'su -' gives a clean user session, avoiding conflicts from the previous user's environment.
Understanding environment loading prevents subtle bugs when running commands as another user.
4
IntermediateSwitching users without password using root
🤔Before reading on: Can root switch to any user without a password? Commit to yes or no.
Concept: The root user can switch to any user without needing their password.
When logged in as root, typing 'su username' switches to that user immediately without a password prompt. This is because root has full privileges.
Result
Root can quickly switch users, making system management efficient.
Knowing root's power helps understand Linux security and why root access must be protected.
5
AdvancedUsing su in scripts and automation
🤔Before reading on: Do you think 'su' can be used non-interactively in scripts? Commit to yes or no.
Concept: 'su' is designed for interactive use and does not accept passwords via command line, making scripting tricky.
To run commands as another user in scripts, you can use 'su -c "command" username'. However, it still prompts for a password unless run as root or with passwordless setups. For automation, 'sudo' is usually preferred.
Result
Scripts can run commands as other users but need careful setup to avoid hanging on password prompts.
Understanding 'su' limitations in automation guides you to better tools like 'sudo' for scripting.
6
ExpertSecurity implications and best practices
🤔Before reading on: Is using 'su' safer than 'sudo' for privilege escalation? Commit to yes or no.
Concept: 'su' requires sharing the target user's password, which can be risky. 'sudo' allows controlled access without sharing passwords.
Using 'su' means users must know root's password, increasing risk if leaked. 'sudo' lets admins grant specific command rights without full password sharing. Many modern systems prefer 'sudo' for better auditing and security.
Result
Understanding this helps choose safer privilege escalation methods in production.
Knowing the security tradeoffs of 'su' versus 'sudo' is critical for protecting systems.
Under the Hood
'su' works by creating a new shell process with the user ID and group ID of the target user. It authenticates by asking for the target user's password unless run by root. When switching, it can optionally load the target user's environment variables and settings by simulating a login shell. The shell process inherits permissions and environment, allowing commands to run as the new user.
Why designed this way?
'su' was designed in early Unix systems to allow users to switch identities securely without logging out. It uses password authentication to verify identity and process user permissions at the OS level. The option to load a login environment was added to mimic a full login session, ensuring correct environment setup. Alternatives like 'sudo' came later to improve security and granularity.
User Session
  │
  ├─> Run 'su username'
  │      │
  │      ├─> Prompt for username's password
  │      │
  │      ├─> If authenticated:
  │      │       ├─> Create new shell process
  │      │       ├─> Set process UID/GID to target user
  │      │       └─> Load environment (optional with '-')
  │      │
  │      └─> User runs commands as target user
  │
  └─> Exit returns to original user
Myth Busters - 4 Common Misconceptions
Quick: Does 'su' always load the target user's environment variables? Commit to yes or no.
Common Belief:People often think 'su' always switches to the target user's full environment.
Tap to reveal reality
Reality:'su' without '-' keeps the current environment, which can cause permission or path issues.
Why it matters:This misunderstanding can lead to commands failing because environment variables like PATH are incorrect.
Quick: Can any user use 'su' to become root without knowing root's password? Commit to yes or no.
Common Belief:Some believe any user can switch to root using 'su' without restrictions.
Tap to reveal reality
Reality:Only users who know root's password or have root privileges can use 'su' to become root.
Why it matters:Assuming otherwise can cause security risks or confusion about access control.
Quick: Is 'su' the best tool for running commands as another user in scripts? Commit to yes or no.
Common Belief:Many think 'su' is ideal for scripting user switches.
Tap to reveal reality
Reality:'su' is interactive and not designed for scripts; 'sudo' is better suited for automation.
Why it matters:Using 'su' in scripts can cause hangs or security issues due to password prompts.
Quick: Does root need to enter a password when switching users with 'su'? Commit to yes or no.
Common Belief:Some think root must enter passwords when switching users.
Tap to reveal reality
Reality:Root can switch to any user without a password prompt.
Why it matters:Misunderstanding this can lead to unnecessary password sharing or confusion in administration.
Expert Zone
1
When stacking multiple 'su' commands, environment variables can become inconsistent, causing subtle bugs.
2
Using 'su' with the '-' option resets environment variables, but some scripts rely on inherited variables, leading to unexpected behavior.
3
The PAM (Pluggable Authentication Modules) system controls 'su' authentication, allowing fine-grained access control beyond just passwords.
When NOT to use
'su' is not ideal for fine-grained permission control or automation. Use 'sudo' when you want to allow specific commands without sharing passwords. For scripting, 'sudo' with NOPASSWD is safer and more reliable. Avoid 'su' in multi-user environments where auditing and access control are critical.
Production Patterns
In production, 'su' is often used by system administrators for full root access during maintenance. Scripts rarely use 'su' directly; instead, they use 'sudo' or run as root. Some systems restrict 'su' usage via PAM to enhance security. 'su' is also used in containers or chroot environments to switch users quickly.
Connections
sudo
'sudo' builds on the idea of 'su' by allowing controlled privilege escalation without sharing passwords.
Understanding 'su' helps grasp why 'sudo' was created to improve security and usability in multi-user systems.
User Authentication
'su' relies on user authentication to verify identity before switching users.
Knowing how authentication works clarifies why 'su' asks for passwords and how security is enforced.
Role-Based Access Control (RBAC)
'su' is a basic form of role switching, while RBAC systems provide more granular control over user permissions.
Seeing 'su' as a simple role switch helps understand more complex access control models in security.
Common Pitfalls
#1Trying to run 'su' in a script without handling password prompts.
Wrong approach:su -c "apt update" root
Correct approach:Use 'sudo apt update' with proper permissions or configure passwordless sudo for automation.
Root cause:'su' requires interactive password entry, so scripts hang waiting for input.
#2Using 'su' without '-' and expecting the target user's environment.
Wrong approach:su root # Then run commands expecting root's PATH
Correct approach:su - root # This loads root's environment variables correctly
Root cause:Not understanding that 'su' keeps the current environment unless '-' is used.
#3Sharing root password with multiple users to allow 'su' access.
Wrong approach:Giving root password to all admins for 'su' access
Correct approach:Use 'sudo' with individual user permissions to avoid sharing root password.
Root cause:Misunderstanding security risks of password sharing and lack of auditing.
Key Takeaways
'su' lets you switch user accounts temporarily without logging out, by starting a new shell as that user.
Using 'su -' loads the target user's full environment, preventing many common permission and path issues.
Root can switch to any user without a password, but normal users must know the target user's password.
'su' is mainly for interactive use; for scripting and fine-grained control, 'sudo' is safer and more flexible.
Understanding 'su' helps grasp Linux user management, security, and the evolution of privilege escalation tools.