As I mentioned earlier, you must be root to edit the /etc/hosts file. It
is possible to log on to the machine directly as root, but it is safer
to log in as a normal user first, then execute the superuser command
in your shell and enter the root password.
The pico editor is probably installed on your system, so try
to start editing. If the shell complains that the command was not found,
you can turn to nano, which is almost always present when pico is not.
Both pico and nano have the same basic functionality. Whichever one you
are using, you can use the arrow keys to navigate through the file. If
I wanted to add a new static IP to my /etc/hosts file, which looks like
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 localhost.localdomain centaur localhost
at present, I would go down below the last line and enter the new IP
address, then press <Tab>, then enter the new hostname and press <Enter>.
To leave the editor, type <Ctrl>-X and answer Y if you want to save the
changes, or N if you've accidentally erased a bunch of stuff and want
to quit without saving.
Once you're done editing, execute
to leave the root session.
If your system has neither pico nor nano installed, let me know through a
Clarification Request and give me a chance to suggest further alternatives
before you rate this answer.
Clarification of Answer by
12 May 2006 14:01 PDT
There absolutely must be vi, however. This classic program is not the
easiest editor to use, but it is the most powerful. I'll give you the
basics of vi, just enough to edit /etc/hosts .
The most important thing to know is that vi has two modes: command
mode and insert mode. When vi launches, for instance when you execute
as root, you start out in command mode. You can move around the file using the keys
H : left
J : down
K : up
L : right
and delete single characters using the X key.
Move to the end of /etc/hosts and press the O key, which switches you
into insert mode and lets you enter a new line of text. Type your
/etc/hosts entry , then press <Esc> (at the upper left of the
keyboard) to return to command mode.
followed by <Enter> to write the file to disk. Use
For a more thorough tutorial, see the following.