Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Commands»su Command in Linux with Useful Examples

    su Command in Linux with Useful Examples

    By RahulDecember 21, 20202 Mins Read

    The su command is also known as switch user. This command is used to become another user during a login session. When invoked without a username, su defaults switch to the super user. Basically, the su command is used to change current logged in user to another user without logged out from system.

    Advertisement

    It is an frequently used command mostly by the Linux terminal users. This tutorial will help you understand the uses of Linux su command with examples.

    Syntax:

    su [OPTIONS] [USER] [OPTIONAL ARGS...]
    

    su Command Examples in Linux

    Let’s begin with a basic example. I’m Logged in to my system with user ‘root’, verified the identify using whoami command. Then switch to root user with su command. Again verified the identify.

    whoami                ### Output: rahul
    
    su - root             ### Become root user
    
    whoami                ### Output: root
    

    Things to know:

    • Invoke the su command without username becomes the superuser (root).
    • Using hyphen (-) with switching invoke login shell scripts. This is used to provide an environment similar to what the user got at direct login
    • The current environment is passed to the new shell with effective environment variables to switched user.

    The sudo privileged users can also prefixed sudo with su command. like:

    sudo su - root 
    

    Sometimes, you may only need to switch user to run a single or few commands only. In that condition, su provides you -c option to run command as another user without actually switching the shell.

    su - root -c "whoami"               ### Output: root
    

    su Command Line Options

    Linux su command has only few but very useful option. These options are very useful while automating tasks with the shell scripts.

    1. -c, --command Use this options to pass COMMAND to the invoked shell. With the help of this, you can run command as another user.
    2. -, -l, --login make the new shell a login shell. So all the environment will be set as similar to user logged in directly.
    3. -s, --shell Change the default SHELL that is specified in /etc/passwd file.
    4. -m, -p, --preserve-environment Use this option to preserve environment variables, This is helpful, while running command temporarily as another user.
      1. Conclusion

        In this tutorial you have learned Linux su command with useful examples.

    command su
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Split Command in Linux With Examples (Split Large Files)

    Test Your Internet Speed from the Linux Terminal

    11 Practical Example of cat Command in Linux

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Split Large Archives in Linux using the Command Line
    • System.out.println() Method in Java: A Beginner’s Guide
    • Split Command in Linux With Examples (Split Large Files)
    • Test Your Internet Speed from the Linux Terminal
    • 11 Practical Example of cat Command in Linux
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.