5

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?

2
  • 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. Commented Aug 8, 2017 at 11:20
  • What parts didn't you understand? Commented Aug 8, 2017 at 11:21

1 Answer 1

12

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.