whatsmydns.net - DNS Propagation Checker
  1. What's My DNS?
  2. Hosts File - How and why to edit your DNS Hosts File

Hosts File - How and why to edit your DNS Hosts File

The hosts file is a file on your computer or other device that is used as the first step in the DNS lookup process for DNS hostname resolution.

This is the process of converting a domain name like www.example.com into an IP address like 192.168.2.1 which computers and network devices use to communicate with each other.

Making changes to your systems hosts file lets you intercept the normal DNS lookup process and set any IP address for any domain name you like which can be useful for many reasons, but also potentially harmful in some circumstances.

The hosts file can be found in the following locations depending on your operating system:

  • Windows: C:\Windows\System32\Drivers\etc\hosts
  • Linux: /etc/hosts
  • MacOS: /etc/hosts

There are several reasons why you may be interested in viewing or editing your hosts file which include:

Testing DNS changes - A common use for making changes to the hosts file is to test changes to DNS records before making them live on the internet. This allows you to make sure a new server setup is behaving as expected before making the changes to your DNS setup and having them live for everyone.

Forcing DNS changes - Often when having made changes to your domain names DNS settings, you will need to wait for DNS propagation as you may have locally cached DNS records and are not seeing the expected updated results. You can flush your DNS cache, or modify your hosts file to temporarily force changes which are not updating fast enough.

Local development - Often setting up custom hostnames for local testing and development can be quite useful. This allows you to configure your system to respond to your local web server on https://www.mysite.test and have your real site live on https://www.mysite.com.

Ad & content blocking - As the hosts file lets you override DNS entries for any domain name, people often create hosts file entries for common tracking, spyware, malware domains as well as product activation servers to assist with software piracy. There are lists of common ad networks and tracking servers available online which you can use to keep an up-to-date list of sites to block. Blocking is achieved by setting the IP address of the ad networks to a loopback address like 127.0.0.1 which will not return anything.

Checking for compromise - Often if your device is infected with malware, then your hosts file may be compromised to included entries which could cause you harm. For example, if your hosts file had been unknowingly changed to point your banks domain name to the IP address of a hacker. This type of DNS attack is known as DNS pharming, and checking your hosts file can uncover potential infection.

Where is the hosts file located?

The most common question that many people have around the host file is how do you find it? What is the path and location of the hosts file?

Depending on your operating system, the hosts file is located in different locations and requires different methods for editing as it is a protected system file.

Windows host file location

The hosts file for all recent versions of Windows including Windows 7, 8, 10, 11 & Windows Server is located in C:\Windows\System32\Drivers\etc\hosts.

There are a few important notes to remember when opening the hosts file on Windows:

  • You will need to use a plain text editor like Notepad, not a word processor like Microsoft Word.
  • You will need to open the file as the local system administrator. This can be done by right clicking on for example Notepad and choosing "Run as administrator".
  • When navigating to the directory containing the hosts file, they may be hidden depending on your setup. Additionally you may need to select to show "All Files (*.*)" instead of the default "Text Documents (*.txt)".

Linux host file location

The hosts file for Unix, Linux and other Unix like operating systems is located in /etc/hosts

As this is a protected system file, you will need to make sure that you edit it as the root user. This can be done using sudo for example sudo vim /etc/hosts from the terminal.

Apple MacOS host file location

The hosts file for MacOS is located in /etc/hosts as MacOS is a Unix based system and follows many of the common file location conventions.

As this is a protected system file, you will need to make sure that you edit it as the root user. This can be done using sudo for example sudo vim /etc/hosts from the terminal.

How do you edit your hosts file?

Once you’ve found the location of your hosts file for your specific operating system, all you need to edit it is a simple plaintext text editor. You can use Windows Notepad, TextEdit on the Mac, as well as command line text editors like vim or nano on Unix based systems.

You will need to make sure that when you’re editing this file that you do so with an account which has administrator privileges as this is a protected system file.

Hosts file format

Once you have located the hosts file, the format is very simple. On a new blank line you need to enter the desired IP address followed by one or more spaces (or tabs) and then the hostname you would like to assign to this. You can add additional hostnames separated by one or more spaces (or tabs) as well.

IP address considerations:

You can use either IPv4 A Records or IPv6 AAAA Records for the IP address which you would like to assign.

If using an IPv6 address, you may want to consider using either the IPv6 expanded format or IPv6 compressed format for all IPv6 entries to ensure consistency and making searching for entries easier if you have large number of them.

Hostname considerations:

When specifying the host name for a hosts file entry, make sure to only include the domain name itself. Do not enter the protocol (http://), or document path (/some-page.html). For example for the webpage http://www.example.com/some-page.html the hostname would be simply www.example.com. If you would also like to include both example.com and www.example.com then you will need to specify both individually.

If using an International Domain Name (IDN), then it will first need to be converted to Punycode format as the host file is plain text and does not accept UTF-8 characters.

Wildcard domains (*.example.com) are not supported in the hosts file.

It is important to make sure that you use a plain text editor like Notepad rather than a word processor like Microsoft Word to edit the hosts file as advanced word processors can introduce unrecognized data like font information.

Example hosts file:

An example hosts file entry may look like the following:

127.0.0.1 example.com

or with multiple entries:

127.0.0.1 example.com
127.0.0.1 www.example.com

or multiple entries on a single line

127.0.0.1 example.com www.example.com

Hosts file comments:

It can sometimes be useful to add comments to your hosts file, especially if you have many entries or entries that you switch between from time to time.

To add a comment, you simply need to use the # character, and anything after this will be ignored.

# this entire line is a comment
127.0.0.1 example.com # this text at the end of a host file entry is a comment
# 127.0.0.2 example.com this entire line a comment and not a valid hosts file entry

The above only includes a single valid hosts file entry for example.com pointing to 127.0.0.1.

How to test changes made to the hosts file?

When you have made changes to your hosts file, you will not be able to use any of the Online DNS Tools to check your changes, instead you will need to rely on your local device for testing.

Using built in DNS tools like nslookup on Windows or dig on Linux and MacOS will query your devices locally configured nameservers directly, skipping reading the hosts file so is not an ideal way of testing changes.

One of the best methods is to use the ping command-line tool, simply run ping example.com and observe which IP address is resolved for the given hostname.

Which hosts file to edit when using Windows Subsystem for Linux (WSL)?

You may wonder which hosts file you need to edit when running multiple operating systems on Windows by taking advantage of WSL.

Making changes to the Linux guest operating systems hosts file at /etc/hosts file will only have changes reflected within the Linux instance.

Editing the Windows hosts file at C:\Windows\System32\Drivers\etc\hosts will have the changes reflected on both Windows and within the Linux instance.