Sunday, February 28, 2010

Do It Sudo

I've come across a couple sudo tricks during the past few days and thought I would pass them along.

The first thing you need to know before using sudo is that you should use the visudo command to edit the sudo config file. This script does a number of useful things, like creating a lock to prevent mulitple editing, and doing some sanity check. But if you have the problem that setting $EDITOR and $VISUAL is ignored when running something with sudo (like sudo visudo or sudo crontab -e), that's a sudo config issue. Add the following in your /etc/sudoers file:

Defaults env_keep += "EDITOR VISUAL"

And you can use sudo from within your normal Emacs window by using the Emacs' Tramp Mode, which allows all kinds of editing, even remote editing. Just use the sudo "protocol" when editing a system file, like /etc/fstab:

Find file: /sudo::/etc/fstab

This will prompt you for your password (not the root password - remember, we're doing this via sudo, which wants your password, not root's). Type it in and you can edit the file in place, rather than running a special Emacs process as root. Very nice!

1 comment: