OS X and sudo
- 4 minutes read - 741 wordsUNIX-family operating system users will be familiar with the awesome
power of the sudo command. For anyone not familiar with how it works,
there’s a quick guide here.
On the offchance that that hasn’t made everything clear, here’s how
the sudo command is meant to work: if you need to run a command that
needs more privileges than you have from your normal account, you type
sudo before that command, and then enter your own password. If you’re
allowed, the system runs the command as root, not as your normal
user account. Thus you can temporarily raise your privilege level if you
need to. If you need more than one command, you can keep starting your
command lines with sudo. The authentication times out after a while —
but you just need to re-enter your password and you can continue.
Since Apple’s OS
X is basically UNIX at its core, it
also has the sudo command. Like many OS X users, I run my Mac from an
unprivileged account. The first account I create is an Admin account,
which I only use for administration. The second account I create is a
normal account, which I use for all work on the machine. This works
really well. When I need extra privs, the system prompts me for them in
the GUI, and even tells me what permission is being asked for. I
authenticate as the Admin account, and on I go. This works really well
from the GUI.
However, I’m a bit of a command-line junkie. If you’re using the
command line, your only option is to do something like su - admin,
authenticate, and then remember to quit the shell when you’re done. I
can’t really blame Apple for this, as I’m sure most people don’t have
this requirement. But I like to be able to bend my computer to my Mighty
Will(tm). And so I’d like the sudo command to work.
If you are running from a non-admin account (and I’d suggest that you
should be), you can launch a Terminal and type a command, preceded by
the sudo command: sudo ls. The system prompts for your password.
After you type it, the system then says
"mauvedeity is not in the sudoers file. This incident will be reported".
This is not quite what I had in mind. Bother. So, what to do?
The clue is in the reference to the sudoers file. This is a file that
the system uses to control access to the sudo functionality. There is
a command that will allow you to edit the file — since it’s a special
system file, you can’t simply go edit it — you need to use the visudo
command. Here’s what you need to do. By the way, I am assuming that you
know how to use the vi editor at this point. If you don’t, this trick
will not work. If you fancy learning it, practice on some safe text
files first!
Authenticate as a privileged account, and at the prompt, enter the
command sudo visudo. Enter that privileged account’s password at the
prompt, and after a short delay (well, I am writing this on my
PowerBook) it will open the sudoers file. Scroll down through the
file, and you will see a line that says something like:
# Uncomment to allow people in group wheel to run all commandsfollowed by a line that says
# %wheel ALL=(ALL) ALLDuplicate that line (I’m not going to tell you how to do that!). Then, change
the “# %wheel” bit to be your short name. If you’re not sure what it is, type
“whoami” at a Terminal prompt (not the one where you’ve done the su bit.
Once you’re done, you should have a line that looks like
mauvedeity ALL=(ALL) ALL"You can optionally copy the line that lets you do this without a password, but I wouldn’t recommend it.
Exit vi, saving your changes. (For those of you who did this and
don’t know how to use vi, I suggest typing “:w” (colon, q,
exclamation mark), which will write changes and quit).
Now, when you try to use sudo, it should work fine, and you’re good
to go. If you don’t believe me, type "sudo whoami" — it should reply
“root”, indicating that your account is temporarily changed.
Any other good command-line tips? What do you think? Feel free to leave me a comment below!