su Command in Linux

Learn via video courses
Topics Covered

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Overview

The su command, short for 'substitute user' or 'switch user', is a Linux command that allows you to execute commands with the privileges of another user. It is commonly used to switch to the root user, granting access to administrative commands and functions.

Linux su Command Syntax

The syntax for the su command is as follows:

Where:

  • options: The options allow you to modify the behavior of the su command.
  • username: The username of the user you want to switch to. If not provided, it defaults to the root user.

su Command Options:

  • -c: Execute a single command as the specified user and exit.
  • -l: Simulate a full login by the specified user, setting environment variables and changing the working directory to the user's home directory.
  • -p: Preserve the current environment variables when switching users.
  • -s: Specify the shell used to execute commands, overriding the default shell for the specified user.
Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Example Usages

  • Switch to the root user:

    Output:

    Explanation: This command switches to the root user, prompting for the root password. After successful authentication, the command prompt changes to the root user's prompt.

  • Switch to a different user:

    Output:

    Explanation: This command switches to the user 'john', simulating a full login by setting environment variables and changing the working directory to John's home directory.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Tips

  • Always use 'su' with caution, especially when switching to the root user, as it grants access to administrative commands that can potentially harm your system.

Advanced Use Cases of su Command in Linux

  • Execute a single command as another user:

    Output:

    Explanation: This command executes the 'cat /etc/shadow' command as the root user, displaying the contents of the /etc/shadow file, which is normally not accessible by regular users.

  • Switch to a different user and use a custom shell:

    Output:

    Explanation: This command switches to the user 'john' and starts the tcsh shell, overriding the default shell for that user.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Conclusion

  • The su command is used to switch to another user, granting access to their permissions and privileges.

  • It can be used to execute a single command as another user or to simulate a full login.

  • Always use 'su' with caution, especially when working as the root user.