What is the difference between the sudo
and su
command? Why does OS X handle these different than Linux?
Asked
Active
Viewed 2.4k times
14

Cajunluke
- 17,704

whitefleaCH
- 243
-
2If 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 02 '12 at 05:13
1 Answers
23
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.

Aaron Lake
- 3,702
- 23
- 22
-
5In simple language terms
sudo
can be though of assuper 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 insudoers
) after which you will have to type the password again. – binarybob Apr 27 '12 at 18:26 -
1the '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 betweensudo
andsu
. – Aaron Lake Apr 27 '12 at 19:21 -
"The key difference between sudo and su is sudo runs a command as root, whereas su makes you root" - false,
sudo -s
runs shell ("makes you root"), andsu -c command
runs command as root. – el.pescado - нет войне Apr 27 '12 at 20:33 -
2Again for brevity's sake I omitted the many alternative methods of using
sudo
. Also, you can't dosu -c command
by default on OS X, since you don't know root's password. You can, howeversudo su
, change the password, thensu -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