Overview
The hosts file is a file on your PC that can override the default DNS of the network you're on - essentially allowing you to designate what server the hostname (or domain) will point to. For example, you could set a hostname that points Google.com to 1.1.1.1 and see what happened (it would break! But sometimes it can be useful, like if an ISP DNS cache has broken a hostname's connection to it's IP address).
- How To Recognise A Hosts File
- Why Change The Hosts File?
- Editing Your Hosts File
- How To Flush Your DNS
How to Recognise a Hosts File?
The hosts file is a regular text file, usually called “hosts”. Regularly, a host file will reference your local computer “localhost” and will look like below;
127.0.0.1 localhost
123.45.67.89 www.example.com
#98.76.54.32 www.another-example.com
The hosts file can be edited (as it’s a regular text file) and allow your local machine to delegate a domain name to a specific IP address, regardless of whatever the network might delegate it to.
What’s The Point In Changing It?
You can change the hosts file to point your domain to a preview server to work on a new version of the site, or you can use it to override the default DNS cache on your network/ISP due to an incorrect cache or other issue.
Editing Your Hosts File
In order to change the hosts file, you’ll need to know the IP address of the server (you can use What’s My DNS or a Google Dig to find out) or by checking in your cPanel console.
Once you know what your IP address is, you can either use a browser plugin to edit the file or you can simply edit it yourself in the system file.
Using A Browser Plugin
There are multiple plugins that can do this for both Firefox and Chrome. A quick Google search should be able to uncover them, but as a quick guide the most popular ones are listed below by browser;
Chrome:
Firefox:
It’s important to keep in mind that each plugin has a different method for use, and you should consult their individual documentation for best results.
Editing System Files
Alternately, you can alter the host your PC loads the website from by editing the hosts file yourself.
This might be easier for you depending on your level of comfort with your operating system and altering system files.
The hosts file looks like the example below;
127.0.0.1 localhost
123.456.679.10 www.domain.com
#12.34.56.789 www.domain2.com
The second line in the example is the one you’ll need to add. Simply replace “123.456.679.10” with the IP address of the designated server, and replace “domain.com” with the correct domain name.
You can de-activate a line by adding the hash symbol (#), which causes the line to be ignored. Specific instructions for popular OS are below.
Windows
Windows 10/8
- Open the File Explorer
- Click "This PC" in the sidebar, then navigate to c:\Windows\System32\Drivers\etc\hosts
- Open the "hosts" file with Notepad (you may have to open Notepad as an administrator to make changes)
Windows 7 or Vista
- Select Start > All Programs > Accessories
- Right click Notepad and select Run As Administrator
- Click Continue on the permission window
- In Notepad, select File > Open
- In the file name text box, type (or paste) C:\Windows\System32\Drivers\etc\hosts
- Select Open
Once you've made it to the host file, make the necessary changes and save the document. Once you've done that, be sure to clear your browser's cache (Instructions for Chrome/Instructions for Firefox) and then reboot your computer.
Mac OS X
- Open a Finder window
- Select Applications from the sidebar
- Double-click on Utilities
- Double-click on Terminal
In the Terminal window, you will need to enter a command to open the Nano text editor. You will need your administrator password.
5. type sudo nano /etc/hosts and then hit return.
6. Enter your administrator password and then hit return.
If you want to add a new device or domain, move the cursor using the arrow keys and position it after the text in the window. Then, begin typing.
If you're mapping a particular IP address on your local network to a domain, you can type the IP address, hit tab, then type the domain name.
Conversely, if you'd like to make sure a web URL doesn't go to its intended site — if you're trying to keep your Mac away from certain sites, use "127.0.0.1." That'll map it back to your Mac. Even if your Mac is assigned a different IP address by its router, 127.0.0.1 defaults to the local machine thanks to the default settings in that hose file.
Once you're done, hold down the control and O keys to save the file, then control and X to exit.
Back at the command line, type sudo killall -HUP mDNSResponder then type return. That will flush your Mac's DNS cache, so it doesn't get confused by any changes you've made to the Hosts file.
(Thanks to imore.com for this information - http://www.imore.com/how-edit-your-macs-hosts-file-and-why-you-would-want)
UNIX
On Unix-based systems, you can find the hosts file at /etc/hosts. Most distributions of Unix will have terminal located in the same location.
To open the terminal:
-
Go to Menu.
-
Select Applications.
-
Choose Accessories.
-
Select Terminal.
To edit the hosts file:
Unix users can edit hosts files in the same process as Mac users. Please refer to the above steps to make the necessary changes.
DNS Flush
Once you have modified your hosts file, it is recommended that you flush your DNS so that the new changes can be implemented more swiftly. DNS flushing must be done using command line. Depending on your computer's OS, there will be different commands. For more detailed instructions, please check the flush DNS page at WhatsMyDNS.