Editing a file you don’t have permissions for
Regardless of which editor you use, sometimes you’ll want to edit a file that your user doesn’t have write permissions for. For example, if you’re a regular user and you want to edit /etc/hosts
– a file owned by root, and writable only by root – you’ll need to either become root or use the sudo
command. See Chapter 7, Users and Groups, for more details.
While a command like sudo $EDITOR /etc/hosts
can be used to edit files as root, a better approach is to use sudoedit
to execute your editing command as root:
sudoedit /etc/hosts
EDITOR=nano sudoedit /etc/hosts
EDITOR=vi sudoedit /etc/hosts
The first example will use whatever editor you’ve got set in your EDITOR
environment variable, while the other two commands pass in (or override) the EDITOR
environment variable as part of the command.