The difference between the sudo and su command

terminalunix

What is the difference between the sudo and su command? Why does OS X handle these different than Linux?

Best Answer

OS X handles sudo and su identically to Linux.

sudo is a command that, without any additional options, will run a command as root. For example:

% touch /newfile
touch: /newfile: Permission denied
% ls -l /newfile
ls: /newfile: No such file or directory
% sudo touch /newfile
% ls -l /newfile 
-rw-r--r--  1 root  wheel  0 Apr 27 11:45 /newfile

su on the other hand, will switch the current user to root (again without any extra commands). In the example below, I have to run sudo su, since I don't know the root password for my system:

% whoami
alake
% sudo su
$ whoami
root

The key difference between sudo and su is sudo runs a command as root, whereas su makes you root. Much like other command line utilities there are a number of alternative ways to use both sudo and su, if you're interested you can always run man <command> eg. man sudo to get more information.