Both commands will log me in as root. I know there is a difference in what environment settings are set.
Which one do you use? Which one is better practice?
su
has only one purpose: to start a shell running as the given user (root
by default).
sudo
is a highly configurable program that lets you run any command as any user, with the configuration allowing the local administrator to place limits on what commands you can run using sudo
, as well as which commands require your password.
su
is (I believe) the older program which allows you to do anything the given user can do, which is often more than you need. sudo
is safer in that it can be tailored to allow you just enough authority to run the commands you need, nothing more.
sudo -i
and a hater re: sudo su
, I'm happy to see the question asked, even if the forum selection wasn't on-point).
Mar 14, 2016 at 23:33
http://linux.die.net/man/8/sudo
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. sudo attempts to change to that user's home directory before running the shell. The security policy shall initialize the environment to a minimal set of variables, similar to what is present when a user logs in. The Command Environment section in the sudoers(5) manual documents how the -i option affects the environment in which a command is run when the sudoers policy is in use.
sudo su
is common practice, but it's common practice because people don't know any better.