Google Answers Logo
View Question
 
Q: Access /etc/hosts file on Unix to edit and add new IP ( Answered 5 out of 5 stars,   1 Comment )
Question  
Subject: Access /etc/hosts file on Unix to edit and add new IP
Category: Computers > Programming
Asked by: thrown2wolves-ga
List Price: $20.00
Posted: 12 May 2006 08:58 PDT
Expires: 11 Jun 2006 08:58 PDT
Question ID: 728110
How do I access /etc/hosts file on Unix to edit and add new IP?

Request for Question Clarification by leapinglizard-ga on 12 May 2006 09:14 PDT
You'll need the root password on the machine. Do you have it?

You will also need to use an editor, such as vim or emacs. A very
simple one is pico. Are you familiar with one of these?

leapinglizard

Clarification of Question by thrown2wolves-ga on 12 May 2006 09:32 PDT
Hello leapinglizard! thanks for the heads up on using an editor!

I do have the root password and can login yet have no idea what to do
from here. How do I access /etc/hosts file on Unix to edit and add new
IP?
Answer  
Subject: Re: Access /etc/hosts file on Unix to edit and add new IP
Answered By: leapinglizard-ga on 12 May 2006 11:13 PDT
Rated:5 out of 5 stars
 
Dear thrown2wolves,


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

    su

in your shell and enter the root password.

The pico editor is probably installed on your system, so try

    pico /etc/hosts

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.

    nano /etc/hosts

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

    192.168.0.100   groucho
    192.168.0.101   harpo

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

    logout

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.

Regards,

leapinglizard


Search strategy:

unix editors
://www.google.com/search?q=unix+editors&start=0&ie=utf-8&oe=utf-8&client=firefox-a&rls=org.mozilla:en-US:official

Request for Answer Clarification by thrown2wolves-ga on 12 May 2006 13:45 PDT
There is no pico nor nano installed! :-+

Clarification of Answer by leapinglizard-ga on 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

  vi /etc/hosts

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.

Now type

  :w

followed by <Enter> to write the file to disk. Use

  :q

to quit.

For a more thorough tutorial, see the following.

http://www.eng.hawaii.edu/Tutor/vi.html#start

leapinglizard
thrown2wolves-ga rated this answer:5 out of 5 stars and gave an additional tip of: $10.00
Excellent! Exactly what I need!

Comments  
Subject: Re: Access /etc/hosts file on Unix to edit and add new IP
From: leapinglizard-ga on 12 May 2006 16:37 PDT
 
Thank you for the fine rating and the kind tip. Both are much appreciated.

leapinglizard

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy