Monday, November 14, 2011

Difference between 'su' and 'su -' or 'su root' and 'su -root'


“su” is equivalent to “su root” and “su -” equivalent to “su – root”. “su root” is the default command for “su”. That is, if you type “su” it will assume you mean “su root”. To su to another user you will have to add the users name, such as “su username”. If you use “su” you will still have the same environment, for example the $PATH variable, as the user who su’d. If you use “su”, you will have the environment of the user you su’d to.
The diff between “su” and “su -” is “su -” you log in as a root with the env setting/profile (i.e you really log in as a root). Just type “pwd” and you will be in the root home dir @ /root. if you “su” only then basically you just “borrow” the root permission without having all the root env setting/profile. Just “su” from your home dir and type “pwd”, you can see that you still in the user dir i.e /home/username.
They’re just different paths to get to the same destination. “su” is short for super user. You’ll use it when you’re logged in as a user and wish to do something that requires root privileges. Using “su” or sudo is the only way to access root privileges while logged in as a user unless one wishes to reboot and log in as root. sudo is nothing more than another path to becoming a super user.
Here is a small demo:
A good reason to log in as root would be to set up a user account so you don’t have to log in as root. It’s just to make things easier for a person to run as user instead of root yet provide an easy way to access root when needed.