2

This question already has an answer here:

I see both does the same thing and both end me up in the same directory.So is it really the same or there is a difference?

Also doing su and su - makes me root user in both the cases.So how is it even benficial?

marked as duplicate by muru, Kusalananda, JdeBP, Romeo Ninov, Christopher Aug 8 '17 at 12:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I know my queston is quite the same but I didn't quite understand the difference between all of them and especially when to use what. – Souvik Ray Aug 8 '17 at 11:20
  • What parts didn't you understand? – muru Aug 8 '17 at 11:21
10

Description

There are differences between them:

First of all using su username you have to provide the password of "username" to be able to switch into its account while with sudo su - username for a second with your own password you will become root, then without using any other password you are running the su - uesrname to switch into the "username".

The other difference is using - with su it will switches to the user while running a login shell, without the - you will get a non-login shell.


Conclusion:

  • su username:
    • You have to provide the target user's password
    • You will be dropped into a non-login shell (things like .profile will not be sourced).
  • sudo su - username
    • You're using your own password to run the su command as root (if you're permitted to)
    • You will get a login shell with the target user access.

Which one to use:

It's not a good idea to share a single password between users, so it's best to not use su at all, instead we can use sudo, also there is no need to run something like:

sudo su - username

we can setup our /etc/sudoers file then use something like:

sudo -i -u username

If you have to use su, then always use it like su - to make sure everything is sourced as it should be and nothing has been compromised with.

Not the answer you're looking for? Browse other questions tagged or ask your own question.