Difference Between sudo -i and sudo su – in Linux

bashcommand-line-interfacedebianlinuxUbuntu

What is the difference between commands sudo -i and sudo su -?

Are they the same?

Best Answer

They may provide functionally close to the same thing, but it seems 'sudo -i' is lighter weight and keeps some handy back references in your environment.

You can see the extra processes by looking at 'ps auxf' (f gives you a forest view)

sudo -i yields this process tree

jkrauska  4480  0.0  0.0  76828  1656 ?        S    23:38   0:00  |   \_ sshd: jkrauska@pts/0
jkrauska  4482  0.0  0.0  21008  3816 pts/0    Ss   23:38   0:00  |       \_ -bash
root      4675  0.6  0.0  19512  2260 pts/0    S+   23:42   0:00  |           \_ -bash

sudo su - yields this process tree

jkrauska  4480  0.0  0.0  76828  1656 ?        S    23:38   0:00  |   \_ sshd: jkrauska@pts/0
jkrauska  4482  0.0  0.0  21008  3816 pts/0    Ss   23:38   0:00  |       \_ -bash
root      4687  0.5  0.0  43256  1488 pts/0    S    23:42   0:00  |           \_ su -
root      4688  0.5  0.0  19508  2252 pts/0    S+   23:42   0:00  |               \_ -su

Note that they are starting from the same bash process pid, 4482, but that su - seems to spawn another step.)

Your first 'sudo' is already elevating your access level to root. Running su without specifying a username inside sudo changes the current user to root twice.

Another way to investigate this is by running both commands with strace -f.

strace -f -o sudoi sudo -i

vs

strace -f -o sudosu sudo su -

If you diff those two straces, you'll see more exeve's being run for sudo su -.

One more thing.

sudo -i maintains the extra environment variables set by SUDO.

SUDO_USER=jkrauska
SUDO_UID=1000
SUDO_COMMAND=/bin/bash
SUDO_GID=1000

sudo su - clobbers those variables.