logo

/etc/hosts – The hosts file

logo

The other day I decided to set up my iBook so that I can do web development on it.

While setting up my new server, I was moving a domain name to the new server, but didn’t want to actually change the DNS for the domain until I knew Apache was configured properly. I needed some way to test Apache’s virtual host configuration without actually making the DNS change. Turns out, since OS X is based on UNIX, you can add dns entries to your computer manually.

Edit the file /etc/hosts, and include the name and IP address for 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
123.123.123.123 domainname.com

You’ll need to do this with root privileges, since it is a system file. You’ll have to do something like

sudo vi /etc/hosts

Update: In OS X 10.5 it is no longer necessary to reload the hosts file. It will happen automatically.
After you save the file, you’ll need to get OS X to refresh its hosts database.

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

After that, you should be all set! Your computer will resolve dommainname.com to 123.123.123.123 without contacting any DNS servers.

Note, this is not an OSX-specific feature. Windows also has a hosts file. It is usually located at:

c:\windows\system32\drivers\etc\hosts

To get Windows to recognize the changes, you need to flush the DNS.

ipconfig /flushdns

You can also read about the hosts file on Wikipedia.

2 Responses to “/etc/hosts – The hosts file”

  1. [...] If you have a firewall that can block outgoing connections to specific servers, you can add the hostname to that. If you’re comfortable editing system files, you can do the same thing without installing additional software, by editing your hosts file to point pagead2.googlesyndication.com to 127.0.0.1. I’ve described how to edit the hosts file for Windows, OS X and Linux in a previous post. [...]

  2. Dick Cochis says:

    Exceptionally enlightening appreciate it, I do think your current audience will likely want significantly more reviews of this nature maintain the great hard work.

  3. I have not checked in here for some time because I thought it was finding boring, but the last few posts are great quality so I guess I will add you back to my everyday bloglist. You deserve it my friend :)

Leave a Reply

logo
logo
Powered by WordPress | Designed by Elegant Themes