MacOS – difference b/w various commands for getting root access in terminal

command linemacosrootsudoterminal

I have used linux:Kali for a while and to get root access i just needed to type: su in terminal. But in OSX i need to type in su - what does this - mean?

If i just type su in terminal than i get sh-3.2# in next line

whereas if i type su - then i get root# in next line.

What's the difference between all of these commands.

Also i see some other commands to get root access in terminal:
(1) su bash
(2) sudo su

Please help to understand difference b/w all these commands.

Best Answer

From man su:

-l      Simulate a full login.  The environment is discarded except for
        HOME, SHELL, PATH, TERM, and USER.  HOME and SHELL are modified
        as above.  USER is set to the target login.  PATH is set to
        ``/bin:/usr/bin''.  TERM is imported from your current environ-
        ment.  The invoked shell is the target login's, and su will
        change directory to the target login's home directory.

-       (no letter) The same as -l.

As for su versus sudo, it's generally accepted in Apple circles that sudo is the preferred method of working in root. By default, su is disabled on OS X (as is the root account). There are only a few occasions when I've had to enable root to get things done.

For more information, I recommend reading both manpages.

man su man sudo