Using /etc/hosts on MacOS X

How to do host resolution on OS X.

Note: It should be noted that this is not a UW-Madison Help Desk or DoIT Middleware supported procedure, and, naturally, we can't take responsibility for any damage you do while following or attempting to follow these procedures. Be sure you understand what you are doing.

N.B.: YMMV. This works for me. It may not work for you. I can't believe Apple created a wonderful new OS and made it next to impossible to add an arbitrary hosts entry.

  1. Edit /etc/hosts to include the host you want to add. For example:
    ##
    # Host Database
    #
    # localhost is used to configure the loopback interface
    # when the system is booting.  Do not change this entry.
    ##
    127.0.0.1       localhost
    255.255.255.255 broadcasthost
    ::1             localhost 
    128.104.1.160   login.wisc.edu
    
    Leave the other entries (localhost and broadcasthost) alone.
  2. Reload the NetInfo Database:
    $ sudo niload -v -m hosts . < /etc/hosts
    4 items read from input
    Netinfo /machines contains 4 items
    
    Processing input item:
    ip_address: 127.0.0.1
    name: localhost
    
    merging directory /machines/localhost
    
    Processing input item:
    ip_address: 255.255.255.255
    name: broadcasthost
    
    merging directory /machines/broadcasthost
    
    Processing input item:
    ip_address: ::1
    name: localhost 
    
    merging directory /machines/localhost
    
    Processing input item:
    ip_address: 128.104.1.160
    name: login.wisc.edu
    
    merging directory /machines/login.wisc.edu
    
  3. Test it:
    $ ping login.wisc.edu
    PING login.wisc.edu (128.104.1.160): 56 data bytes
    64 bytes from 128.104.1.160: icmp_seq=0 ttl=62 time=0.963 ms
    ^C
    --- login.wisc.edu ping statistics ---
    1 packets transmitted, 1 packets received, 0% packet loss
    round-trip min/avg/max/stddev = 0.963/0.963/0.963/nan ms
    
  4. If that doesn't work (wrong address is listed), try sending a HUP to lookupd:
    $ ps auxwwww | grep lookupd
    root     17320   0.0  0.1    29204   2008  ??  Ss    1:35PM   0:05.84 /usr/sbin/lookupd
    $ sudo kill -HUP 17320
    
  5. If that doesn't work, I don't know. Try rebooting. Pretend you're running Windows and you moved the mouse or something.



Keywords:/etc/hosts hosts file macos mac os x osx   Doc ID:4506
Owner:Jon M.Group:Middleware
Created:2006-02-15 19:00 CDTUpdated:2010-08-13 19:00 CDT
Sites:Middleware
Feedback:  63   23