Wednesday, March 17, 2010

Switching Users: su in Linux

Switching Users: su

To switch to another user, we use su command

Running su without any parameter will automatically try to login you to root user.

You’ll be prompted for the root password and, if you enter it correctly, will drop down to a root shell.

Example:

[tirumal@myhostname ~]$ su

It will prompt for the password, if you give the correct password you will be logged into the root shell.

If you are already in the root user(shell) and want to switch to another ID, you don’t need to enter the password when you issue this command.

If u want to login to with a particular user, then

[tirumal@myhostname ~]$ su raj
(Or)
[tirumal@myhostname ~]$ su - raj

The difference is,if we are using optional hyphen (-), this is tells us to switch identities and run the login scripts for that user (use the user specific environmental variables).
If there is no hyphen, the shell will take the globally set environment variables.