3 UNIX / Linux su Command Examples

What is su?
3 su examples
Syntax and Options
Related Commands

What is su?

su command is used to become another user during login session. By default (without username parameter), its become the superuser.

3 su examples

1. Login into another user account

By simply passing the username to the su command, it will provide the login session after the password authentication as shown below

$ su guest
Password:

$

2. Run specific command with another user privilege

Using -c option, su allows to run a specific command in the another user account and the output would be displayed in current login session as shown below.

$ su guest -c date
Password: 
Sat Jan  8 11:18:12 IST 2011

3. Preserve the current environment

Using -p option, you can preserve the current environment as shown below

$ su -p guest

Syntax and Options

Short Option Long Option Option Description
-c COMMAND –command COMMAND to run a specific command that will be invoked by the shell using its -c.
-l –login to provide the similar environment to what the would expect that the user had logged in already
-s SHELL –shell SHELL to invoke the specific shell accordingly
-p –preserve-environment to preserve the current environment


Related Commands

login