Linux Su Command Tutorial With Examples – POFTUT

Linux Su Command Tutorial With Examples


The su short for substitute super user command using to change currently logged user.  This makes things practical because the user change made without login. Su command can be used to get root privileges too. su and sudo commands have different ways to act for similar aims.

su Command Syntax

su command has very simple syntax and few options.

su OPTIONS USERNAME
  • OPTIONS will set some config about the su command.
  • USERNAME is the user name we want to switch.

su Command Man Page

The manpage of the su command is like below. This page provides detailed information about su command usage even the su command is a very simple command.

$ man su
su Command Man Page

su Command Help

To get fast and simple help the -h parameter can be used like below. As we can see there are very few options about the su command. Alternatively, the full option --help can be also provided to list help information about the available parameters.

$ su -h

$ su --help

Change User with su Command

This commands the main usage area is changing the current user and open the new shell for the specified new user. While changing user current environment variables will not change and stay as old user’s environment variable. In this example, we will change to the user root.

$ su root

$ su ismail

$ su jack
Change User
Change User

Change User With Environment Variables

While changing user environment variables can get with the - parameter. If this parameter is not specified previous user’s environment variables will be used. We can see that after the user is changed the working directory is also changed as root’s home.

$ su - root
Change User With Environment Variables
Change User With Environment Variables

Run Command As Different User with su Command

Some commands may be needed to run as a different user. Every time changing user to run a command is not feasible. Commands can be run as a specified user with the -c parameter like below. In this example, simple ls command will be run as the root user.

$ su -c ls root
Run Command As Different User
Run Command As Different User with su Command

Specify Shell with su Command

While changing to a different user or running a command as a different user shell may become important. The default existing shell can be changed with the -s parameter. In the following example, we will use primitive sh shell for the root user.

$ su -s /bin/sh - root
Specify Shell
Specify Shell with su Command

LEARN MORE  How To Verify Files and Signatures with PGP In Linux?

Leave a Comment