contact

Loading enum info ...

/etc/hosts in Mac OSX

Index

About this document
Edit /etc/hosts
Reload NetInfo database
Test
Troubleshooting

About this document

On *NIX systems as well as on windows there is a hosts file you can use to link ip addresses to hostnames.
There are a couple of uses for this:

  1. overwrite DNS results because you want to test a new server
  2. Add non existing hostnames to your resolving

If you use Mac OSX you will find out it's a bit more pain to do this. Here we show you how it can be done.

Edit /etc/hosts

As on most *NIX systems OSX comes with a file /etc/hosts.
The format of this file is:
ipaddress hostname1 hostname2 hostnameN
BUT... you will notice that editing this file has no effect.
To get the desired effect you do need to edit this file to put your desired ipaddress/hostname pair in this file
and read the next paragraph.

Reload NetInfo database

OSX comes with what they call the NetInfo database. Here a lot of your network config is stored.
It also holds all the static configured hostnames (like we always do in /etc/hosts on all other *NIX systems)
You can add hosts here, but it's a bit of a pain because it takes 3 to 4 entries in some cryptic form to get it done.
We added the ipaddress/hostname link in the /etc/hosts file already so it would be nice if we could use that.
It turns out we can import/merge that information with the running config in the NetInfo database.

sudo niload -v -m hosts . < /etc/hosts

This will give you some output about processing and merging information.
If it finishes without error it's time to test it

Test

This is the easy part.
Simply type: ping hostname

Troubleshooting

If the ping does not work you can try 2 things in this order

  1. reload lookupd
  2. reboot

Reloading lookupd is done by sending a HUP signal to the process.

Find the processid with:

ps auxw | grep lookupd

send it a HUP signal with:

sudo kill -HUP <processid>

Now try to ping the hostname again.
If it still not works we do like all novice sysadmins do: reboot



 Feedback on this page or article:

This article has no feedback yet

 Give feedback:

To give feedback on this article, click here
Copyright (c) 2006-2008 Michiel van Baak.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2
or any later version published by the Free Software Foundation;
with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
A copy of the license is included in the section entitled "GNU Free Documentation License".
< back | print | text | /etc/hosts in Mac OSX