What's the difference between `sudo -i -u user` and `sudo su - user`?

Jan Dudulski picture Jan Dudulski · Jul 29, 2015 · Viewed 25.4k times · Source

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?

Answer

sTg picture sTg · Jul 29, 2015

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.