13

Both commands:

sudo -i -u username
sudo su - username

Will log me in as username if I enter my password. Is there actually any difference between these commands?

Jan Dudulski
  • 753
  • 1
  • 7
  • 14
  • 3
    Run `sudo -i -u username` and check your Environment Variables then run `sudo su - username` and check your Environment Variables You should see a difference – Mischa Jul 29 '15 at 09:28
  • 1
    probably this answer might be of some help to you, am also trying to find the answer fot the same question. http://askubuntu.com/questions/376199/sudo-su-vs-sudo-i-vs-sudo-bin-bash-when-does-it-matter-which-is-used – Radan Nov 25 '15 at 11:38
  • 1
    There are also very significant configuration changes. If you use `sudo su`, then your PAM configuration for `su` matters in addition to your `sudoers` config; if only using `sudo`, then you're depending only on `sudo` (and all the configuration included therein by reference -- its PAM modules, its configuration, etc), whereas using `su` in addition means you're depending on *two* tools with independent behavior and configuration (and on the former to be configured to allow the latter). – Charles Duffy Dec 12 '16 at 21:32
  • Interestingly, I've just seen a case where `sudo -i -u someuser` doesn't set variables from `/etc/environment` but `sudo su - someuser` does. (`sudo -i` on this Ubuntu 14.04 system used to work.) – cjs Apr 18 '19 at 09:08

1 Answers1

8

The su command stands for "substitute user", and allows you to become different user(super user). sudo su changes the current user to root but environment settings (PATH) would remain same. It allows user who have permissions to execute a command as the superuser or another user, as specified in the sudoers file.

With sudo -i you get a clean root shell. The ‑i (simulate initial login) option runs the shell specified by the password database entry of the target user as a login shell. This means that login-specific resource files such as .profile or .login will be read by the shell. If a command is specified, it is passed to the shell for execution via the shell's ‑c option. If no command is specified, an interactive shell is executed.

agbb
  • 2,030
  • 2
  • 21
  • 23
sTg
  • 3,848
  • 12
  • 60
  • 105
  • 4
    Afaik `su` stands for 'substitute user' and not 'switch user' (or even 'super user'). – NaN Jul 29 '15 at 10:00
  • thank you NaN..I corrected..apologies for the typo. – sTg Jul 29 '15 at 10:12
  • "changes the current user to root but environment settings (PATH) would remain same" - actually this is not true for me – Jan Dudulski Jul 29 '15 at 12:18
  • sudo su - username, in his case bash is called as login shell, so /etc/profile, .profile and .bashrc are executed and you will find yourself in users home directory with user's environment. there is difference between sudo su and sudo su - please check http://askubuntu.com/questions/376199/sudo-su-vs-sudo-i-vs-sudo-bin-bash-when-does-it-matter-which-is-used – Radan Nov 25 '15 at 11:37