Tutorials UPDATED: 21 September 2024

The hosts File: A Powerful Tool for Users and Developers

Tassos Antoniou

8 min read
Hosts file a powerful tool for users and developers

You might have heard of a hosts file before but been unsure of what it is or what it does. In this article we’re going to clear this up and explain exactly what a host file is, how it works, and how you can edit it with your favorite editor. There are plenty of ways you can benefit from the hosts file by editing it and we will look at some examples of things you can achieve with it. Let’s get started!

What is a Hosts File?

A hosts file is found on your computer (regardless of whether it’s a Mac or Windows, it will have one). Hosts files are what’s known as ‘plain text’ and they originate from several decades ago where they were originally used as an early form of ‘name resolution’ whereby early internet activity was directed to target servers by these files. This use was superseded by the invention of the Domain Name System (or DNS) which is still in use today. 

Today’s internet traffic relies on the DNS system to ‘tell it where to go’. When you type the address of a website into your browser, the browser ‘looks up’ the location of the server hosting this website using the DNS system. 

That request starts at your local computer’s hosts file. If the required details (i.e. the location or IP address of the required server) are not found there, then request continues to external DNS servers until the address is found. This then allows the browser to connect with the website and display the content you were looking for. All of this happens in a matter of milliseconds which is truly remarkable.

Take a quick look at the graphic above. As you can see, requests sent by your computer are first routed via your computer’s hosts file. If the hosts file has the IP address of the website you requested saved then it will send your browser straight to it. If it doesn’t then it will direct your browser to a DNS server which should have the required information. 

Drawing on this, it’s logical to conclude that by editing the hosts file we can bypass the DNS and make any domain resolve to any IP address while using our browser. The ability to do this can have a number of benefits and we’ll see.

Note: Before looking further at the hosts file and how it can be edited it’s worth noting that the hosts file can be a target for malicious attacks. Because of this, it’s worth changing its properties to ‘read only’ when you have finished making any modifications. 

Modifying the hosts file

Modifying the hosts file on your computer is super easy to do and can be incredibly useful at times. Let’s take a look at how you can easily modify this file on Windows, Linux and Mac.

How to modify hosts file on Windows 10

The file can be located at “C:\Windows\System32\drivers\etc”

Hosts file location - Windows

When opening hosts file with a text editor like notepad you will see something like this (do not forget to “Run as administrator” when prompted).

Hosts file content

Below the existing content (around line 24 in our example above) you can type in the target IP address you want to reach, then a space, then the domain name. This will look approximately like this:

Note that the “#” symbol is used for comments. Whatever is written after the “#” in the same line will be ignored. You can use this as a label to label edits to your hosts file, for example: 

# my router’s alias
192.168.1.1 myrouter.com

How to modify the hosts file on a Linux distribution

On Ubuntu, for example, the full path to the hosts file is /etc/hosts. You can open the file with your editor (we used Nano editor in this example). Enter your sudo password when prompted. 

Hosts file location - Linux distribution

Once that’s done you can add line(s) as we did for Windows. 

How to modify hosts file on MacOS

On MacOS, the full path to the file is /etc/hosts.  After you launch a terminal window, open the hosts file using your text editor. 

  1. Type sudo nano /private/etc/hosts and press Return. 
  2. Enter the requested password. You will now have the hosts file open in the Nano editor. 
  3. Navigate below the original content in the hosts file and edit the file as described for Windows and Linux. > target IP address > a space > domain name
  4. Press Control-O to save the file.
  5. If the changes do not display, flush the cache with > dscacheutil -flushcache

Now that we understand how the hosts file can be edited on your local machine, let’s take a look at a few useful things you can do with this. 

Top reasons to edit the hosts file

There are lots of reasons why you might want to edit your hosts file. We’ll run through a few of them below. 

When testing a website’s development

Suppose you are a WordPress developer working on a new version of your client’s website that is currently live under the domain “mycompany.com”. You’re making changes to a local version of the website (that has been saved on your computer). 

Try our Award-Winning WordPress Hosting today!

The problem you’ll have is that any reference in the code to the domain “mycompany.com” will see your browser take you to the live version of the site rather than the local version of the site which means it’s almost impossible to test the changes you’ve made. In this instance you have a few options:

  1. Buy a temporary domain and point it to the IP where the version under development is located.
  2. Deploy the live site to a staging server
  3. Use the hosts file to temporarily point “mycompany.com” to a different IP address/server locally.

So, unless you like throwing away money on temporary domains a great solution to this problem is to use the hosts file and make the domain resolve to where your work is in progress or will be.

To do this all you have to do is enter the domain name and the IP of the server you are working on (as we described earlier). If for example, the IP address is 138.201.141.25 you’d edit the hosts file to read:

The same applies if you want to add the www domain. You can do this by just adding a second line like this:

Or combine both in the same line like this:

Also, note that using the hosts file to point to your local files will also slightly increase the speed of your browsing as your computer can directly “ask” for a local file and skip requesting the IP address from a DNS server and the wait incurred to receive its response.

Editing the hosts file like this is extremely useful for developers for testing their under-development websites or undertaking other kinds of work where it’s useful to override DNS servers and in doing so control access to websites and network traffic.

Block a website

There are a LOT of websites out there, many of which that are not particularly pleasant. As such, you might want to block access to these (for example if you have children who use your computer). This can be accomplished by routing a specific website’s request to your localhost IP address (which is normally 127.0.0.1). 

We should point out though that:

  • HTTPS sites will not get blocked unless you include www in its address.
  • During these modifications, keep in mind that you’ll need to clear your browser cache in order to see these changes take effect.

So, for example let’s say you wanted to block access to a website called www.badwebsite.com. To do this you would enter the following into your hosts file:

127.0.0.1    badwebsite.com   www.badwebsite.com 

The result of this will be that this website will be inaccessible from your computer.

You could also use the hosts file to block Spyware/Ad Networks. To do this make a list of all the Spyware sites and Ad Networks domain names you want to block and add them to your hosts file, mapping them to the IP address 127.0.0.1 which will always point back to your machine. That way your browser will be unable to reach these domains.

One benefit of doing this is that after blocking such unwanted sites your browsing will be more secure as you will not be able to reach known malicious sites. It can also speed up your browsing as you no longer have to wait while you download ads from ad network sites.

Conclusion

In summary, editing the hosts file gives you complete control over what domain resolves to what IP address locally on your machine. This has a number of possible uses and benefits, like testing your website before changing DNS records. It’s always nice to have control and knowing your way around your computers hosts file gives you just that… control. A powerful tool indeed!

Start Your 14 Day Free Trial

Try our award winning WordPress Hosting!

OUR READERS ALSO VIEWED:

See how Pressidium can help you scale
your business with ease.