What is the difference between "su" and "su -" ?

If we check the man page for su
su - run a shell with substitute user and group IDs

which means that if you use

# su -

you are switching user to the root user and asking the system to change all the environment variables applicable to root and take me to the root's home directory

Using su - is same as logging into a fresh session on a terminal

For example

[deepak@server ~]$ pwd
/home/deepak
[deepak@server ~]$ echo $PATH
/usr/bin:/usr/sbin:/usr/local/bin:/bin:/usr/bin:/home/deepak/bin
[deepak@server ~]$ sudo su -

[root@server ~]# pwd
/root
[root@server deepak]# echo $PATH
/usr/bin:/usr/sbin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

But when you type only su without a dash(-) in that case the session is switched to root(if any username is not mentioned along with su command) without applying any of the environment variable of the root user.

Also the new user stays in the same directory as he was before using su

[deepak@server ~]$ echo $PATH
/usr/bin:/usr/sbin:/usr/local/bin:/bin:/usr/bin:/home/deepak/bin
[deepak@server ~]$ pwd
/home/deepak
[deepak@server ~]$ sudo su

[root@server deepak]# pwd
/home/deepak
[root@server deepak]# echo $PATH
/usr/bin:/usr/sbin:/usr/local/bin:/bin:/usr/bin:/home/deepak/bin

I hope the article was useful.

Leave a Comment