Modifying the hosts file in Windows:
What is a hosts file? And why do I need to modify it??
Put simply, the hosts file is a way of tricking your Operating System (OS) to look at a specific IP address for a URL, instead of looking at the configured DNS server your machine would normally use.
There are many reasons you would want the ability to do this - two common reasons are:
Security – a handy way to stop someone browsing to sites they shouldn’t on your computer is to edit the hosts file and point the URL to your localhost IP address (127.0.0.1). This effectively stops access to the URL and will throw errors if someone tries to browse to the affected address.
In fact many anti malware applications use this method to stop access to known bad sites.
While redesigning a live site: You don’t wont to impact the day to day running of a site while your devs are busy redesigning. To overcome this – you set aside a new server as a dev environment – you set your hosts file to point to the new server and work away without taking down the current site.
So onto modifying the file itself, there are many ways this can be achieved in Windows. This article covers two of the common methods - either will work – it’s a case of how comfortable you feel doing it and how often you’ll need to make changes to the file.
If you’re going to be designing and/or migrating sites constantly – it’s recommended you take option two of this guide – if however you’re only going to be doing this once or twice to test a particular site before it goes live and you just want to quickly get it working – use option 1.
Now before we get into the article – it may be a good time to suggest a handy little tool that will make working with the hosts file much easier.
The standard text editor that ships with Windows is great – it does want you need and is economical in size… but it lacks any real tools.
To overcome this, it’s his highly recommended you download the latest version of notepad++ from https://notepad-plus-plus.org/download - this free app will help with any text file you need to work on. It will also add a handy little option to any context menu (the right click option) in Windows that will allow you open any file with notepad++ directly from the context menu – without having to set or change default system applications. Download and install it – trust us – it will help.
Option 1: Run as Administrator
For Windows 7 and Windows 8:
1) Click the Windows button on the keyboard
2) Type in: notepad (Just start typing - windows will automatically search for what you type in)
3) Right click on the program you would like to use and select “Run as Administrator” – this will run the application in an elevated privilege mode – that will overcome the security placed on the hosts file.
4) Click on the File menu and select Open and change the file type to All Files *.*
5) Browse to: c:\windows\system32\drivers\etc – select hosts and click open.
6) The file comes with information on how to format the data you need to enter. In this example we've used 184.108.40.206 and test.com.au as the IP and URL we need to work on.
# 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
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
# For example:
# 220.127.116.11 rhino.acme.com # source server
# 18.104.22.168 x.acme.com # x client host
22.214.171.124 test.com.au www.test.com.au
Once you have made the changes save the file and perform a tracert from the command prompt and you will see your computer is now looking at the new IP address for the domain you have added to the HOSTS file.
Option 2: Modify HOSTS file security permissions.
1) Open Windows Explorer/My Computer (Windows key +E)
2) Browse to C:\Windows\System32\drivers\etc
3) Select the HOSTS file
4) Right click on the file and select “Properties”
5) Select the “Security” tab and click Edit
6) Click the Add button and enter your Windows username and click “Check names”:
7) Click OK. Then select your name from the list and place a tick in the Allow column next to Full control.
8) Click Ok to exit out of the properties dialog boxes.
9) Run the tracert command from the command prompt (as above) to test the new settings.
If you make a shortcut of the hosts file on your desktop – you will then be able to open the file directly from the desktop with notepad++ and save any changes without those annoying security messages.