For those in the know, the HOSTS file is a great way to locally cache DNS to commonly visited websites, or to give quick access to local machines via hostname on your network. There are many other uses for the HOSTS file, but today we're not really focused on what it is, but rather, how to enable it.
By default, Mac OS X does not use the HOSTS file. For many people, this is just an oversight, as they will probably never need to use one. Since you're reading this, you are probably not one of those people. Enabling the HOSTS file is very simple. Firstly, open your Terminal.app program (located in the /Applications/Utilities folder), and now, run these commands:
$ sudo niutil -create . /locations/lookupd/hosts $ sudo niutil -createprop . /locations/lookupd/hosts LookupOrder FFAgent DNSAgent $ sudo killall lookupd
That's it! OS X will now refer to the HOSTS file before attempting to look up DNS externally! Please note, if you have a web browser running when you run the commands, it will not detect the changes until you restart your browser.
That's great, but... what do these commands actually do? Well, let's break it down, line by line.
$ sudo niutil -create . /locations/lookupd/hosts
The "$" denotes the type of terminal prompt you are at. "$" means you are a normal user, whereas "#" denotes super user. The sudo command temporary elevates a non-admin user to run the following command as a super user... simply, super user do - sudo. niutil was removed with 10.5 Leopard, but serves a purpose in Tiger and below. niutil's options allow you to create, examine, and destroy NetInfo directories or properties. Many of the things you can do with niutil you can also do with NetInfoManager. In this particular command, we are creating the NetInfo folder.
$ sudo niutil -createprop . /locations/lookupd/hosts LookupOrder FFAgent DNSAgent
This command takes the previously created directory, and adds the LookupOrder, FFAgent, and DNSAgent properties to it.
$ sudo killall lookupd
Finally, in this command, we are restarting the lookup daemon, so the system will utilize the new preferences we have set. The killall command will kill a process (application) matching the given string. To see what other processes are running on your machine, experiment with the "ps -ax" command in your Terminal.app.
Fantastic! I get it! Now, how do I add things to my hosts file?
Simple, really. Grab a handy text editor - we'll use TextEdit.app, because it's simple and easily found in our /Applications folder. Open a Finder window, press +⇧+G (that's cmd+shift+G for all you non-Apple computer folks), and type "/etc" in the box that opens up, and hit enter. You will see plenty of files in the /etc directory, but we're obviously only interested in the hosts file. Locate it, and double click it. It will ask you what text editor to open with, as it is not a MIME filetype, so go ahead and choose TextEdit.app. Editing this file will require administrative access, so you will need to supply a username and password at some point in the process. When it opens, it will look something similar to this:
## # 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 fe80::1%lo0 localhost
The existing lines, for the most part, are simply loopback addresses. Leave them there, whatever you do - changing or removing those lines may cause damage to your operating system, and I am not liable for any changes you make. Edit this file at your own risk!
That said, here's a simple entry that we could add to our HOSTS file:
64.233.167.99 gg # Google
If we append this line to our HOSTS file, and save it, typing "gg" in our web browser will now take us directly to Google. Neat! Why does this work? Well, we just told our computer that "gg" is actually the IP address 64.233.167.99 - which resolves to google.com. By typing "gg" in our browser, the browser attempts to resolve DNS for "gg", first by checking the HOSTS file, and then, if nothing matches in the HOSTS file, an external DNS server. Since it can find "gg" in our HOSTS file, it reads that "gg" resolves to 64.233.167.99, and complies by taking us to Google. It's that simple.
There are many other uses for the HOSTS file - adding a local machine with a static IP address for quick access, blocking ads or websites, and even redirecting websites. Be creative!
As always, if you have any questions, feel free to drop me a line.
You must be logged in to post a comment.