What's the difference between `sudo -i -u user` and `sudo su - user`? [closed]
Asked Answered
D

1

21

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?

Dharna answered 29/7, 2015 at 9:23 Comment(4)
Run sudo -i -u username and check your Environment Variables then run sudo su - username and check your Environment Variables You should see a differenceKunkel
probably this answer might be of some help to you, am also trying to find the answer fot the same question. askubuntu.com/questions/376199/…Gauzy
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).Deodar
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.)Achromat
W
13

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.

Westernmost answered 29/7, 2015 at 9:51 Comment(4)
Afaik su stands for 'substitute user' and not 'switch user' (or even 'super user').Seymore
thank you NaN..I corrected..apologies for the typo.Westernmost
"changes the current user to root but environment settings (PATH) would remain same" - actually this is not true for meDharna
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 askubuntu.com/questions/376199/…Gauzy

© 2022 - 2024 — McMap. All rights reserved.