While using the internet, it is common for people to connect to different websites, FTP servers or other internet servers by connecting to a domain name, like www.stemjar.com. However, internet applications communicate using IP addresses instead of domain names. So, when you enter a domain name that you want to connect to, your application has to convert it to an IP address first to facilitate the connection.
The way of resolving these hostnames to their mapped IP addresses is known as Domain Name Resolution. On most of the operating systems, be it Mac, Windows, Linux, Unix or Netware, the resolution from domain names to IP addresses is mainly done by a method called Domain Name System (DNS).
However, there is one more way of carrying out this resolution process without using the DNS service. It is through the Hosts file. Any OS communicating through TCP/IP, the standard of communication on the Internet, possesses this Hosts file which helps in mapping between the domain names and IP addresses.
We will discuss what Hosts file is, its uses and some more aspects in this article to enrich your knowledge about it.
So, let’s get started.
What is the Windows Hosts file?
The Hosts file used by Microsoft Windows operating system is called Windows Hosts file. It is a part of the IP implementation of the OS and serves the purpose of translating user-friendly hostnames into corresponding IP addresses for locating a host in the IP network.
The Hosts file is generally a text file that contains an IP address in the first text-field and is followed by the hostname. Each field has to be separated by a space (or tab). Comment lines begin with an octothorpe (#).
Where is the Windows Hosts file located?
In Microsoft Windows (Versions- NT, 2000, XP, 2003, Vista, 2008, 7, 2012, 8, 10), the Hosts file is located at the following location:
The location of the file may differ from OS to OS. It is generally written as ‘hosts’, without any extension.
Where is the Hosts file located on Mac and Linux?
For finding the location of the Hosts file in the Mac OS, you will require your administrator password. First, open the Nano text editor and type sudo nano /etc/hosts and hit return. Then, enter your password and again hit return.
For Linux, the Hosts file is located at /etc/hosts.
You can also edit the Hosts file, either manually or by using any third party application.
Let us see how it can be done.
How to edit the Hosts file?
You can edit the Hosts file in a few simple steps to fulfill your purpose. But, it is advisable to keep a backup of your file before modifying it. It will help you in quick restoration if you need to, in case of any errors.
For editing the Hosts file, navigate to its location and open it via Notepad or any other text editor. Make the necessary changes and save.
Sometimes, you may receive the following error:
Access to C:\Windows\System32\drivers\etc\hosts was denied.
Cannot create the C:\Windows\System32\drivers\etc\hosts file. Make sure that the path and file name are correct.
In this case, click Start and type Notepad. Right-click the Notepad shortcut and select ‘Run As Administrator.’ Make the required changes by opening the Hosts file and click Save.
You may also use a third-party tool for editing Hosts file instead of manually doing it.
For instance, HostsMan is one such application which allows you to add, remove or modify entries in the Hosts file as well as permits you to add third-party Hosts list that helps in blocking malware sites.
It provides an in-built hosts file updater, hosts file editor and hosts file backup manager along with many other features.
What are the different ways of using the Hosts file?
Hosts file helps in serving multiple purposes. Here, we shall discuss them to understand the versatility of this file.
Generating website shortcuts
The Hosts file can be utilized for creating shortcuts to different websites. It enables quick access to that website. For example:
If you are crazy about Facebook, then you can associate any letter or group of letters (without using spaces and without the need of .com,.org or any extensions) with the IP address of facebook in the Hosts file. Here, let us consider it to be ‘f’.
You can obtain the IP address of facebook by using ping in the command prompt as explained previously.
Fig 1.0 Showing facebook’s IP address using command prompt
Add the following entry into the Hosts file:
Now, just by typing f in any program’s address bar, you will reach facebook.com directly.
Blocking a website and spyware
To block a website, you need to add a simple line like this:
What does it mean? Here,127.0.0.1 means the loopback address of your local computer. When you visit sample.com, the computer will try connecting to itself (127.0.0.1).
The computer is expected not to find a web server running on itself, and therefore the connection will immediately fail, successfully blocking the site from loading.
Similarly, by adding lists of known spyware sites and ad networks, and then mapping the domain names to the IP address of the local computer, you can block these sites.
This, in turn, increases your browsing speed as well as protects you from reaching malicious websites.
To redirect a website to another one
The Hosts file is used to redirect one site to another one. Suppose, you want to type facebook.com and wish to end up in twitter.com.
Isn’t it sound funny? Lets see how can you do this?
You can achieve it through Hosts file. For this, you will need the IP address of twitter. You can find it using the command prompt window of your computer by typing “ping twitter.com” as shown below.
Fig 2.0 Showing Twitter’s IP address using Command Prompt
Now add the following line in the Hosts file:
This line instructs your computer to connect facebook.com to twitter’s IP address, i.e., when you type in facebook.com, you will land up at twitter.com.
Testing a web server that uses host headers
You must be sure that the web server running on your local network functions appropriately before making it live on the Internet. Certain web servers run many websites at the same IP address.
In such cases, the site displayed to you is based upon the hostname that you have entered.
It is not fruitful to access the web servers at their local IP addresses in this situation. You require to access them at the website addresses.
For example, a web server has a local IP address 192.168.0.5 which hosts two websites like sample.com and example.org. In the Hosts file, you make the following entries:
If everything is alright, the server will notice your header specifying sample.com or example.org and will take you to the correct website.
Increasing the browsing speed
Mapping the IP addresses to the sites you use frequently can significantly improve your browsing speed. Your computer does not have to ask the DNS server for the IP address and wait for its response anymore. Instead, it can query a local file quickly.
What are the utilities required for the Hosts file?
Primarily, if you wish to use the Hosts file occasionally and do not modify it much, then the basic text editors like VI, Notepad, and Edit are more than enough for fulfilling your purpose.
However, if you want to use it extensively like for blocking spyware or other reasons, then you can use the tools mentioned below.
On blocking ads on a website by Hosts file, empty boxes tend to appear on that website in the places where the ad was supposed to be present. eDexter replaces these boxes with images from the local machine and prevents your browser from having large empty boxes.
This program helps in maintaining and managing the Hosts file. It reads all the entries of the file and organizes them in a database. You can scan this database for duplicates and manage the listings.
Hosts file permits you to define which domain names are associated to which IP addresses. Also, it is capable of many other functions like blocking websites, redirecting websites, creating shortcuts and more.
You may use this file occasionally or extensively. However, there have been complaints of system slowdown due to use of large Hosts file. To solve it, turn off the DNS Client in the Services control panel under the Administrative tools.
DNS client stores previous DNS requests in the memory to speed up the process but it also reads the whole Hosts file into the cache. This is unnecessary and should be disabled to avert the slowdown.
Some people recommend using this file in “read-only” mode to prevent hijackers or malicious programs to modify it. But making it “read-only” does not mean it is entirely safe from modifications because there exist hijackers like CoolWebSearch which can add entries to the file irrespective of it being in read-only mode.
Sometimes, you may not have a Hosts file in the Windows machine. Instead, a sample hosts file may be present as ‘hosts.sam’ which can be renamed to ‘hosts’ and used as you wish.