Ramblings on technology with a dash of social commentary
RSS icon Email icon Home icon
  • How to edit your hosts file

    Posted on January 2nd, 2012 phpguru 1 comment

    Why do I care about my hosts file?
    You’re developing a website, and DNS is still pointing to the old site, or it’s parked at the domain name registrar, and you want to be able to test the site before DNS is updated. Maybe you don’t have access to the DNS registry for the domain but you need to see the site working how it will on the production URL.

    This is especially handy when you’re making a WordPress site on your local machine, but you want it to “think” it is the full .com domain, since WordPress stores the site name in the database, this can be a handy way to develop locally, but on a fully-qualified .com domain.

    Another example is to create your own “websites” on your local development environment using a “.dev” extension. So you can actually tell your local machine that “mywebsite.dev” is located on the local box. Very handy for web development.

    What we can do is trick our computer into thinking that a website is at a different IP address than the one global DNS records are reporting.

    This is accomplished by editing your hosts file.

    Every system has one. The file is located in different places on Linux, Mac or Windows. On Linux systems, the file is typically located at /etc/hosts.

    Note: Before editing your hosts file, make a copy of it as a backup!

    Finding your Host File…

    …on the Mac
    If you have TextMate you can type
    mate /private/etc/hostsYou can enter your username & password when you save the file.

    If you know how to use vi you can type
    sudo vi /private/etc/hostsOr, if you just have the default Mac text editor, follow these steps:

    1. From Finder’s Go menu, choose Go to Folder…
    2. Type in /private/etc and click Go
    3. When the folder opens, right click on hosts and choose Open with other…
    4. Choose TextEdit from your Applications list

    …on Windows
    The process is a little more complicated. Here’s how I do it:

    1. Go to Start -> Run, or otherwise browse your installed program files
    2. Find Notepad (or Notepad++) and right click
    3. Choose Run as… or Run as Administrator
    4. Now in Notepad, navigate to C:\Windows\System 32\drivers\etc\
    5. Switch to open all files (*.*) not just text files (.txt)
    6. Now open the file named hosts – it has no extension

    What to put into your hosts file
    Now that you have your hosts file open, you’ll see some default entries. The file has two columns to it. Whitespace is ignored — that is, you can use tabs or spaces to separate the IP address on the left, from the DNS names on the right. Comments are done by preceding the line with a hash or number symbol (#). Put each entry on its own line.   localhost
    You’ll also see something like this: broadcasthost
    ::1             localhost
    fe80::1%lo0     localhost

    This tells your system that localhost is located at the IP address Entries like these are standard on all systems — don’t change them or your system could become borked (restore your backup!).

    You can add new entries above or below the default entries, but I recommend adding a few line breaks ABOVE the default stuff and adding your custom changes at the top of the file.

    You can use this technique to point your domain name to the IP address of your hosting account before the DNS record is updated, create staging domains, development domains and so on. Hope you found this useful.


    1 responses to “How to edit your hosts file” RSS icon

    Leave a reply