sudo vs su

ref

su

  1. switch user by default to root user
  2. opens a shell to run command
  3. prompts for root password

su

run program as root and asks for root password

su username

“su -” vs “sudo -i”

both does same

“su” vs “su -”

  • “su” does not change

    1. current working dir
    2. aliases
    3. env
  • “su -”

    1. change current working dir
    2. sets env as if logged in as root

sudo

  1. runs command as root in the same shell.
  2. sudo -> su do switch user and do a command
  3. sudo su will not prompt for root password, when required it ask current user password every 5 mins

sudo -s

will check /etc/sudoers (and possibly included files) to see if the user running it has permission to run a shell as root.

  1. The user gets a shell as root with no password required
  2. The user is prompted for a password, and if they type in their own password, they get a shell as root.
  3. The user is prompted for the root password (this is a weird ‘sudo’ configuration which I don’t understand the reason behind because it mostly duplicates ‘su’, but whatever).
  4. The user is not allowed at all and an E-mail is sent to root hollering about so-and-so trying to run something as root.

-

  1. “-” really shorthand for “-l” or “—login”
  2. causes you to actually log in as that user (by default the superuser).