
Editing your \etc\hosts file is a common task for troubleshooting issues or for developing a site that you aren’t quite ready to launch. By editing this file, you’ll be able to bypass DNS to view a site at a declared IP address.
What is a Hosts File?
The hosts file is a local plain text file that maps servers or hostnames to IP addresses. This file has been in use since the time of ARPANET. It was the original method to resolve hostnames to a specific IP address. The hosts file is usually the first process in the domain name resolution procedure. Here is an example of a hosts file entry.
127.0.0.1 localhosts #loopback
We use the above code as an example since it almost always included in every hosts file. Another example would be similar to the following entry.
{67.225.187.61} {coderog.com} {#Liquid Web}
- The first section denotes the IP address where a request will be redirected to.
- The second section designates the location that we want to redirect a request from.
- The third section specifies a comment for the entry.
- Each entry is usually separated by a space or a tab.
We would add the above entry to our hosts file to associate the hostname (or domain name) coderog.com with the IP address 67.225.187.61. To provide a working example, let’s move on to the next section.
Why is a Hosts File Useful?
Let’s say we have just completed a migration to a new server. In order to see how our domains will look and operate on our new server before we move our DNS settings, we cannot just type in one of our current domain names as it will resolve to the old server we are moving away from (since we never move our DNS A records from the old server until we are sure the new site is working as expected).
So, to see how our domain will look on the new server, (without having to change our DNS records) we can simply modify our local hosts file to point the domain to the new servers IP. If our domain is coderog.com and the old IP is 67.225.187.61 and our new IP will be 72.30.35.10, we can temporarily comment out the current coderog.com entry (using a ‘#‘ symbol) and add an alternate entry which points to the new servers IP.
#67.225.187.61 coderog.com #Liquid Web
72.30.35.10 coderog.com #this is the new coderog.com
If we added the above entry to our hosts file, every time we open coderog.com in our browser, we will now be redirected to the 72.30.35.10 IP.
Where is the Hosts File Located?
The location of the hosts file will differ by operating system. The typical locations are noted below.
- Windows 10 – “C:\Windows\System32\drivers\etc\hosts”
- Linux – “/etc/hosts”
- Mac OS X – “/private/etc/hosts”
What Does a Hosts File Contain?
A hosts file contains entries similar to the following information. Your entries may differ significantly.
# This file is automatically generated by WSL based on the Windows hosts file:
# %WINDIR%\System32\drivers\etc\hosts. Modifications to this file will be overwritten.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
127.0.0.1 localhost
127.0.1.1 mycomputer.localdomain mycomputer
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
How Do I Edit my Hosts File?
Our \etc\hosts file is only editable as a Windows administrator. Here is a short video on how to edit a hosts file.
Here are the directions on how to modify your hosts file.
Step 1.
Click the Windows button and type Notepad in the search bar.
Step 2.
Right click on Notepad and then Run as Administrator.
Step 3.
You’ll be asked, “Do you want to allow this app to make changes to your device?”. Choose Yes.
Step 4.
In Notepad, choose File then Open.
Step 5.
Navigate to C:\Windows\System32\drivers\etc\hosts or click the address bar at the top and paste in the path and choose Enter. If you don’t readily see the host file in the /etc directory then select All files from the File name: drop-down list, then click on the hosts file.
Step 6.
Add the appropriate IP and hostname at the end of your hosts’ file, select Save, and then close the file.
Step 7.
Finally, you will want to flush your DNS cache for your computer to recognize changes to the file. Click the Windows button and search command prompt.
Step 8.
Type the following command in the terminal and press Enter
ipconfig /flushdns
Conclusion
Modifying a hosts file has many benefits which can significantly impact how you view your new domain or website. Using this method to view a site allows us to see how any new changes affect the layout and functionality of a domain.