12

I was wondering if there were any difference (in environment ?) between doing

sudo su - user

and

sudo -iu user

From what I know: By using sudo su - user I open a new shell as user, and the dash gives user's environment variables (while sudo su user does not) and using sudo -iu user as well (-u for precising user, -i for "simulating initial login").

So, is there any difference?

Except the fact that the first one needs the user to be able to execute su as root, and the second one to execute bash as user

Thanks !

1 Answers1

8

here is what it says on the sudo man page about the "-i" option


   -i  The -i (simulate initial login) option runs the shell specified in
       the passwd(5) entry of the user that the command is being run as.
       The command name argument given to the shell begins with a `-' to
       tell the shell to run as a login shell.  sudo attempts to change to
       that user's home directory before running the shell.  It also ini-
       tializes the environment, leaving TERM unchanged, setting HOME,
       SHELL, USER, LOGNAME, and PATH, and unsetting all other environment
       variables.  Note that because the shell to use is determined before
       the sudoers file is parsed, a runas_default setting in sudoers will
       specify the user to run the shell as but will not affect which
       shell is actually run.

which is basically telling me you are just simulating the shell of the refernced user but some things may not be exactly as the intended user logged in directly. If you ask the question "what" I can not tell off the top of my head. But if you use sudo su - user command, there are no questions asked as you are launching a new shell process with the settings of the referenced user on its own. In some cases, these two commands can result in the same environment but in some others, one will encounter discrepancies in my opinion.

MelBurslan
  • 6,966