sudo (execute a command as root)

When you need to run a command with root privileges, use the command sudo (“superuser do”), like so:

sudo tail /var/log/messages

This will run the command →tail /var/log/messages as root, displaying the file's last ten lines. As a normal user, you wouldn't be allowed to read /var/log/messages. In some Linux distributions, you're not allowed to use sudo by default (you'll get an error message) because your user name is not recorded in the file /etc/sudoers. You can fix that by adding the following line to /etc/sudoers:

your_user_name ALL=(ALL:ALL) ALL

This will give you the privilege to execute commands as root using sudo. Before you start fiddling with /etc/sudoers, please note that you should not edit the file by hand! You should only edit it using the command visudo.

visudo launches a text editor (Nano by default) and lets you edit and save /etc/sudoers in a safe fashion. This means, for example, that visudo checks the file's syntax before letting you save any changes. In systems where sudo is the only way provided to gain root privileges, you can lock yourself out of being able to fix the file if you mess up its syntax! So be sure to use visudo and nothing else to edit /etc/sudoers.

Here is one more example of how to make good use of sudo. On my system, I often use a handy program for managing my firewall called Firestarter. I keep the following line in my /etc/sudoers:

vibe ALL=(ALL) NOPASSWD: /usr/sbin/firestarter

This means that I (vibe) am allowed to run the program firestarter as a superuser without entering root's password. In this way, I can have my desktop environment launch Firestarter automatically on startup. It goes without saying that you should only use this option with carefully selected, trustworthy software.

When you need to invoke multiple commands as root and don't want to prefix sudo every time, you can log in as root like this:

su -

The dash (-) is a shorthand for the option -l (“login”). Its effect is to switch you over to the root user's shell environment. Among other things, this means that you'll be transferred to root's home directory, /root. Don't forget to log out using exit once you've finished your administrative work.