Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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

share|improve this question
    
If anything, Linux would handle them differently than OS X. OS X is certified Unix, which means it conforms to a certain standard of behavior. Linux is Unix-like, but is not certified Unix and doesn't necessarily conform to the Unix standard. So if there are differences (other than the root password issue noted in Aaron Lake's answer), they are non-conformance on the part of the implementers of the particular Linux distribution in question. –  alesplin May 2 '12 at 5:13
add comment

1 Answer

up vote 18 down vote accepted

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.

share|improve this answer
3  
In simple language terms sudo can be though of as super user and do. As an additional point, on OS X, once you have successfully authenticated you may then use sudo without a password for a short time (5 minutes by default, unless overridden in sudoers) after which you will have to type the password again. –  binarybob Apr 27 '12 at 18:26
    
+1 @AaronLake very nice and well explained answer. –  E1Suave Apr 27 '12 at 18:55
1  
the 'without additional options' is a pretty significant qualifier ... they'll both let you become users other than root ... eg, sudo su -lm _www will let you have as shell as if the webserver user (by running the command as root, as it doesn't have a valid password) –  Joe Apr 27 '12 at 19:19
2  
@Joe, I considered adding that information but omitted for brevity. As you describe, sudo is quite a robust utility and covering even basic functionality here would ultimately confuse or overwhelm the target audience looking for a differentiation between sudo and su. –  Aaron Lake Apr 27 '12 at 19:21
2  
Again for brevity's sake I omitted the many alternative methods of using sudo. Also, you can't do su -c command by default on OS X, since you don't know root's password. You can, however sudo su, change the password, then su -c command. I feel that the question was answered, and there wasn't a need to dive deeper in to alternate uses of sudo and su. However I'll update the post for the curious folks. –  Aaron Lake Apr 27 '12 at 20:37
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.