Edit the hosts file on the mac

Posted on Aug 18, 2010

Sometimes, you may need to edit the hosts file on the mac.

The hosts file is a file that controls the routing to websites, both localhost and other websites. For example, I can say that whenever I enter http://www.kensodev.com in the browser, the address is then traced back to 127.0.0.1 instead of going to the DNS server and lookup the address there.

This is a very efficient way to do testing to websites, so you don’t have to use localhost or everything. For example, I always use a local prefix to a website to identify it’s on my machine. http://local.kensodev.com will be the local version of this website and so on and so forth.

There are many cases where you might want to edit the hosts file. In mac it’s not that intuitive to find it like in windows so here’s a quick tip (I am using textmate bug you can use whatever you want).

To edit the file simply enter this command into the terminal:

[shell]

sudo mate /private/etc/hosts

[/shell]

You will then be prompted to enter a password and after you edit the file and press save, you will have to enter the password again.

This is it, that is how you edit the hosts file on the mac.