5

It seems as if there is no difference whatsoever. When the whoami or id commands are run, they all yield root credentials. Is this an illusion? If the root account is disabled by default in Ubuntu, and therefore su gives and authentication error, then why allow sudo -I

Edit: Excuse me, the ONLY difference I have learned of is that sudo -I asks for the password of the user who invoked the command, and su asks for root, or some other target user's password.

Is there any OTHER difference?

Braiam
  • 69,112

2 Answers2

1

EDITED. Note: This answer has been heavily edited since its last iteration based on Eliah Kagan's comments.

sudo -i runs a login shell with root privileges, simulating an initial login with root, acting similar to su -. The primary difference between sudo -i and su - is that sudo -i can be executed using a sudoer's password, while su - must be executed with the root account's password. Hence, if you are on a default *buntu install, where root login is disabled, sudo -i can be used while su and its variants cannot.

If you run the following commands:

$ sudo -i
[sudo] password for <username>: <enter user's password>
# cd ~
# pwd

you will get the output:

/root

Hence, you can see that sudo -i simulates an initial root login, including changing the home folder ($HOME) to root's, rather than your own. This also means sudo -i reads login files like .profile.

Meanwhile, sudo -s starts a new shell but without simulating initial login - login files are not read and $HOME is still set to your user's home folder.

If you run the following commands:

$ sudo -s
[sudo] password for <username>: <enter user's password>
# cd ~
# pwd

you will get the output:

/home/<username>

From this, you can see that sudo -s does not simulate an initial login, and does not change $HOME.

0

sudo -i tries to become the user whose password you use, it runs that user's login specific resources (.profile etc) and tries to run from the user's home directory.

su on the other hand logs you in as other users, in the other user's home directory. And that account's login specific resources will be run. By default su logs you in as root.

I recommend using sudo -i over su, unless you know what you're doing.

Seth
  • 59,332