4

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?

1 Answers1

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.

Ravexina
  • 2,638
  • 1
  • 19
  • 33