Sudo

From Jonathan Gardner's Tech Wiki
Jump to: navigation, search

Whenever you see sudo on these pages, what I really mean is "do this as root."

There are three ways to do things as root:

$ su -
# <command>

The first way is to become root, then run the command as root. You'll need the root password each time you do this. I do this if there is a lot of things I need to do as root, or if I am learning how to configure something properly and need to keep changing config files.

$ su -c "command"

This is an alternate way to run a single command. You'll need the root password each time you do this.

$ sudo command

This is the sudo way, and it is my preferred way to run a single command. You'll need your password only once every while. I use this if I know what I need to do and I probably won't have to do it twice, or if most of my work in that session should be done as a non-root user.

Here's how you set up your account to become a sudoer.

First, edit the /etc/sudoers file as root:

$ su -c "vim /etc/sudoers"

Add or uncomment the line:

%wheel  ALL=(ALL)       ALL

This basically says, "If the user is in group 'wheel', they can do anything as anyone using sudo."

Save that file (keep it read only) and then go and edit /etc/group.

$ su -c "vim /etc/group"

In that file, change the line for group wheel to include yourself:

wheel:x:10:root,jgardner

(Of course, unless you want me to have sudo privileges on your box, you should use your own login.)

One last note, when you change /etc/group, your group doesn't change! You can tell by running the groups command:

$ groups
jgardner

Only when you have completely logged out and logged back in will you see the change.

$ groups
jgardner wheel

With the change to /etc/sudoers and your group set as wheel, you are now able to run sudo commands.

There are other ways to give yourself sudo privileges on a box, but I am sure you can figure them out by reading the man pages.