su

user managementLinux/Unix
The su command is one of the most frequently used commands in Linux/Unix-like operating systems. su Switch user or substitute user

Quick Reference

Command Name:

su

Category:

user management

Platform:

Linux/Unix

Basic Usage:

su [options] [arguments]

Common Use Cases

    Syntax

    su [options] [username]

    Options

    Option Description
    -, -l, --login Start a login shell; clears environment variables except TERM, runs login scripts
    -c, --command=COMMAND Pass a single COMMAND to the shell with -c
    -f, --fast Pass -f to the shell (for csh or tcsh)
    -g, --group=GROUP Specify the primary group
    -G, --supp-group=GROUP Specify a supplemental group
    -m, -p, --preserve-environment Do not reset environment variables
    -s, --shell=SHELL Run SHELL instead of the default in passwd
    --help Display help message and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the su command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    su
    Switch to the root user (will prompt for root's password).
    su john
    Switch to user 'john' (will prompt for john's password).
    su -
    Switch to root user with root's environment (login shell). # Advanced Examples Advanced # Run a single command as another user su john -c "ls -la /home/john/documents" # Switch to another user with their login environment su - jane # Switch to root preserving current environment variables su -p # Switch to root with a specific shell su --shell=/bin/zsh # Switch to another user and specify a command with arguments su john -c "find /home/john -name '*.txt' -type f" # Execute a command as another user from a script echo "password" | su -c "command_to_run" username # Specify which shell to use when becoming the target user su --shell=/bin/bash john # Run a login shell simulation as another user su -l jane # Use a specific configuration file for PAM authentication su -c "whoami" --pty jane

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `su` (substitute user or switch user) command is a fundamental utility in Unix and Linux systems that allows a user to temporarily become another user during a login session. Most commonly, it's used to gain root (administrative) privileges without logging out and logging back in as the root user. When invoked without any arguments, `su` defaults to attempting to become the superuser (root). When a username is provided as an argument, `su` attempts to become that specific user. In either case, authentication is required by entering the target user's password, not the password of the current user (unlike `sudo`, which uses the current user's password). There are two primary modes of operation for `su`: 1. **Non-login Shell Mode**: By default, `su` provides a non-login shell. This means it switches to the target user but keeps most of the current environment variables. The working directory remains the same, and only the USER, LOGNAME, and HOME variables are changed. This mode is useful for quickly running commands as another user without changing the entire environment. 2. **Login Shell Mode**: When used with the `-` or `-l` option, `su` simulates a full login for the target user. It changes to the target user's home directory, sets up their environment variables by reading the standard login scripts (like ~/.profile, ~/.bash_profile, etc.), and provides an experience close to what the user would get if they had logged in directly. This mode is ideal when you want to work extensively as the target user. The `su` command is commonly used for: - Performing system administration tasks that require elevated privileges - Running applications or services as a different user for security purposes - Testing the environment or configuration of another user account - Diagnosing permission-related issues by temporarily becoming the affected user It's important to note some security considerations when using `su`: - Regular users can only switch to another user if they know that user's password - The root user can become any user without providing a password - All `su` attempts (successful or failed) are typically logged in system logs - Modern security practices often favor using `sudo` over `su` for more granular control - Some systems may restrict `su` access to users in specific groups (e.g., the 'wheel' group) While powerful, `su` should be used judiciously, especially when switching to the root user, as mistakes made with root privileges can have serious consequences for system stability and security.

    Related Commands

    These commands are frequently used alongside su or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the su command works in different scenarios.

    $ su
    View All Commands