The Windows Hosts file allows you to define which domain names (websites) are linked to which IP addresses. It takes precedence over your DNS servers, so your DNS servers may say facebook.com is linked to a specific IP address, but you can have facebook.com go anywhere you want. The Windows Hosts file can be used to block websites, redirect them, create shortcuts to websites, create your local domains, and more.
What Is the Hosts File Used For?
The HOSTS file is the virtual equivalent of the phone company’s directory assistance. Where directory assistance matches a person’s name to a phone number, the HOSTS file maps domain names to IP addresses. Entries in the HOSTS file override DNS entries maintained by the ISP.
While this might come in handy for regular use, like to block ads or specific malicious IP addresses, its functions also make this file a frequent target of malware. By modifying it, malware can prevent access to antivirus updates or force you to a malicious website. It’s a good idea to check the HOSTS file periodically or at least know how to remove false entries.
The Hosts file is used by the operating system to map human-friendly hostnames to numerical Internet Protocol (IP) addresses which identify and locate a host in an IP network. The host’s file is one of several system resources that address network nodes in a computer network and is a standard part of an operating system’s IP implementation. The Hosts file contains lines of text consisting of an IP address in the first text field followed by one or more hostnames.
Editing the Windows Hosts File
To edit your Hosts file, you’ll need to open Notepad (or any other text editor you like, such as Notepad++) as Administrator. To do so, click Start, type Notepad into the Start menu, right-click the Notepad shortcut and select Run as Administrator.
After you have, click File and Open in the Notepad window, then browse to the C: \ Windows \ System32 \ drivers \ etc folder. Click the Text Files box at the bottom right corner of the window, select All Files, and double-click the host’s file.
Changes you make to the host’s file will take effect immediately after you save the file– you don’t have to reboot. Ensure each is on its own line if you’re adding multiple entries to your host’s file.
Redirect a Website
You can also use this trick to redirect a website to another website. Let’s say we want to redirect facebook.com to twitter.com– so when we type facebook.com into our address bars, we’ll end up at Twitter.
First, we’ll need Twitter’s IP address. To find it, we can use the ping command in a Command Prompt window (click Start, type Command Prompt, and press Enter to open one). Type ping twitter.com into the command prompt window, and you’ll see Twitter’s numerical IP address.
Now we can add the following line to our host’s file:
This line tells our computer to connect facebook.com to Twitter’s IP address. Type facebook.com, and you’ll end up at Twitter!
Block a Website
To block a website, add a line like the following one to the bottom of the host’s file:
What this does is simple– 127.0.0.1 is the IP address’ of your local computer. Your computer will try to connect to itself (127.0.0.1) when you navigate to example.com. Your computer probably won’t find a web server running on itself, so the connection will fail immediately, effectively blocking the website from loading.
What If I Can’t Save the HOSTS File?
In some versions of Windows, you don’t have permission to save directly to the \ etc \ folder and are instead told that you must save the file elsewhere, like to the Documents or Desktop folder.
You might instead see errors …
Access to C: \ Windows \ System32 \ drivers \ etc \ hosts were denied
Can not create the C: \ Windows \ System32 \ drivers \ etc \ hosts file. Make sure that the path and file name are correct.
To still use the file you’ve edited, go ahead and save it to your Desktop or some other folder, and then go to that folder, copy the HOSTS file, and paste it directly into the location where the HOSTS file should be, as described above. You’ll be prompted with permission validation and will have to confirm overwriting the file.
Another option is to open your text editor program as an administrator so that the permissions are already applied to the editor. Saving the HOSTS file over the original can be performed without having to verify your admin credentials.
You probably don’t have the correct permissions to be editing files in that folder if you still can’t save to the HOSTS file location. You should be logged in under an account that has administrative rights over the HOSTS file, which you can check by going and right-clicking the file to the Security tab.