Understanding and Using The Hosts File-Beginners Guide

hosts fileWhat is the hosts file? and What is it used for?

The hosts file performs a name to IP address mapping and was used on early computer networks for name resolution before DNS was developed.

The hosts file is still present on computer systems (windows,Linux, MAC), tablets (e.g. android) etc and can be very useful for testing purposes.

The hosts file is a simple text file that can be edited with a text editor like notepad.

However it is important to note the hosts file doesn’t have a file extension like .txt.

Host Names

Before we look at the host file itself it is important to understand host names.

On a windows network all machines have a name (computer or host name), and an address (IP Address)

On a windows network the computer name is also called the host name.

The computer name is assigned at install, but can be changed later (click on start menu and right click my computer>computer name>change).
change computer name on windows

To make using it easier make it something meaningful and short. I use WS1 (workstation 1) etc.

Note: All computer names on a windows network must be unique.

You may also find the computer name referred to as the Netbios name as well the host name.

The netbios name arises from the fact that early Microsoft networks used the netbios networking protocols which required all devices to a have a unique name.

On a Microsoft network then

computer name = Netbios name = host name

On a Linux or Unix network the host name is purely optional but useful and so it is normal to give each computer/device a name.

The host name can be used instead of the computer IP address by using the hosts file to map the name to an IP address.

To find the host name of a machine type the command hostname and a command prompt.(Windows and Linux)

hostname-windows

You could also use the ipconfig (windows) and ifconfig (Linux) commands.

Host File Location – Windows and Linux

On windows XP,windows 7 the hosts file is located in c:\windows\system32\drivers\etc\,

Note: It may be hidden so will need to enable view hidden files to see it.

In addition, you may not be able to save it unless you have opened it as an administrator. Take look at this article for a guide.

On Linux systems the hosts file is located in /etc directory.

Here is the basic default host file taken from my PC. It comes with all Windows machines.

Host file on Windows PC

Most systems have a single default entry for the local host being mapped to the 127.0.0.1 loopback address.

When is The Hosts File Used?

When you enter a network name into a browser bar,command line etc as shown in the simple ping command line below:

ping-hostname

The computer must first resolve this name into an IP address.

Most systems will have several ways of doing this, and they will do it in a particular order.

This is known as the name resolution process. See understanding the name resolution process.

On most devices the hosts file is examined first before DNS (if DNS is configured configured).

Note on Windows machines (windows 10,8,7,XP etc) changing the name resolution order is done using registry settings.

However on Linux machines a simple text file called /etc/hosts.conf (older versions) and /etc/nsswitch.conf is used. See this article

In the screen shot above my hosts file didn’t have an entry for Google.com and so the system used DNS to resolve the name.

However if I edit the host file, and create an entry for www.google.com that points to the loopback address (127.0.0.1).edited-hosts-file

Here is what happens when I ping www.google.com.

ping-hostname-hostsfile

Notice that this time the entry in the hosts file is used and not DNS.

In the example above I used the fully qualified domain name (FQDN) of www.google.com.

I could have used an alias e.g. Google and achieved the same result.

Some viruses will target the host file as it is an easy way of getting unsuspecting users to rogue websites.

If you develop websites, setup local networks etc then you will find the hosts file very useful.

Common Questions and Answers

Q- What is the difference between a host name and a domain name.

A- A host name is used on local networks without DNS and is the name assigned to a computer. It looks like this ws1. Whereas a domain name looks like this ws1.mydomain.com.

Q- On my Windows machines I also have an LMhosts file what is it?

A- LMhosts is used by applications that use Netbeui protocol and contains NetBIOS names and IP address mappings . It isn’t really used anymore.

The Hosts file Video

I’ve created a video –What is the host file? which covers the main points above.

Summary

The hosts file is used as part of the name resolver process, and is usually the first option.

Most people will not ever need to edit the host file, but you should know that it exists, and what the hosts file does just in case.

Was This Useful?

Related tutorials and resources:

Save

Save

Save

Save

Please rate? And use Comments to let me know more